本方法是针对word导出操作,需要制作好的模板文件 模板.doc

引入应用Microsoft.Office.Interop.Word 11.0  (office2003)

导出文件注意:有时候迅雷会在浏览器中安装插件,下载时会默认使用迅雷下载,导致下载的文档格式丢失,这时为了避免错误,可以将下载方法DownFile()写在另一个页面中,通过session将参数传递过去就可以

 /// <summary>
/// 导出按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
{ string id;
id = Session["ObjChangeID"].ToString();
try
{ if (id!=null)
{ string _filePath = PrintDoc(id);
if (_filePath != "")
{
DownloadFile(_filePath, Path.GetFileName(_filePath));//下载文件
}
}
}
catch (Exception ex)
{
ZWL.Common.PublicMethod.errorLog("ibtnExport_Click", ex);
}
}
/// <summary>
/// 打印操作,传入车辆Carcod或者变更ID
/// </summary>
/// <param name="id"></param>
protected string PrintDoc(string id)
{
Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document doc = new Microsoft.Office.Interop.Word.Document();
try
{ if (id.Length != )
{
objCarInfor.OpenCar(id);
string templeteName = "模版.doc", downName = "";///模板文件名称
downName = objCarInfor.ORG_NAME + "新文件.doc";//导出文件名 string templeteFile = System.Web.HttpContext.Current.Server.MapPath("~/") + "DocTempalete\\" + templeteName;//模板文件全路径
string downFile = System.Web.HttpContext.Current.Server.MapPath("~/") + "ReportFile\\gonghan\\" + downName;///导出文件全路径
try
{
File.Delete(downFile);//删除原有的同名文件
}
catch
{
}
File.Copy(templeteFile, downFile);//复制模板文件到导出文件对应的文件夹下存档
object Obj_FileName = downFile;
object Visible = false;
object ReadOnly = false;
object missing = System.Reflection.Missing.Value;
//打开文件
doc = app.Documents.Open(ref Obj_FileName, ref missing, ref ReadOnly, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref Visible,
ref missing, ref missing, ref missing,
ref missing);
doc.Activate();
#region 给模板填入类容
//光标转到书签
object BookMarkName = "函号";
object what = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToBookmark; if (app.ActiveDocument.Bookmarks.Exists(BookMarkName.ToString()) == true)
{
doc.ActiveWindow.Selection.GoTo(ref what, ref missing, ref missing, ref BookMarkName);
doc.ActiveWindow.Selection.TypeText(System.DateTime.Now.ToString("yyMMdd"));
}
BookMarkName = "年";
what = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToBookmark; if (app.ActiveDocument.Bookmarks.Exists(BookMarkName.ToString()) == true)
{
doc.ActiveWindow.Selection.GoTo(ref what, ref missing, ref missing, ref BookMarkName);
doc.ActiveWindow.Selection.TypeText(DateTime.Now.ToString("yy"));
}
BookMarkName = "月";
what = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToBookmark; if (app.ActiveDocument.Bookmarks.Exists(BookMarkName.ToString()) == true)
{
doc.ActiveWindow.Selection.GoTo(ref what, ref missing, ref missing, ref BookMarkName);
doc.ActiveWindow.Selection.TypeText(DateTime.Now.ToString("MM"));
}
BookMarkName = "日";
what = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToBookmark; if (app.ActiveDocument.Bookmarks.Exists(BookMarkName.ToString()) == true)
{
doc.ActiveWindow.Selection.GoTo(ref what, ref missing, ref missing, ref BookMarkName);
doc.ActiveWindow.Selection.TypeText(DateTime.Now.ToString("dd"));
}
///注意:书签必须不一样才能真确绑定,如果说模板中有需要出现两次的的内容,必须设置成两个标签:
/// 如:姓名显示两次,则必须给两个位置都加上标签,以示区别
259 #endregion
object IsSave = true;
doc.Close(ref IsSave, ref missing, ref missing);///关闭doc文档对象
System.Runtime.InteropServices.Marshal.ReleaseComObject(doc); doc = null; object IsSave1 = false;
app.Quit(ref IsSave1, ref missing, ref missing);
System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
app = null;
GC.Collect();
return downFile;
}
else
{
return "";
} }
catch (Exception ex)
{
app = null;
GC.Collect();
return "";
} }
///
/// <summary>
/// 下载文件
/// </summary>
/// <param name="filename">文件名(全路径)</param>
/// <param name="downname">文件下载名</param>
protected void DownloadFile(string filename, string downname)
{
FileStream f;
byte[] buffer = new byte[];
try
{
f = new FileStream(filename, FileMode.Open);
buffer = new byte[f.Length];
f.Read(buffer, , buffer.Length);
f.Close();
}
catch
{
ZWL.Common.MessageBox.Show(this, "文件不存在!");
return;
} filename = filename.Replace(@"/", @"\");
//20121023wangyj string saveFileName = "";
int intStart = filename.LastIndexOf("\\") + ;
saveFileName = filename.Substring(intStart, filename.Length - intStart); Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312"; string fileType = Path.GetExtension(filename).ToLower(); switch (fileType)
{
case ".asf":
System.Web.HttpContext.Current.Response.ContentType = "video/x-ms-asf";
break;
case ".jpg":
case ".jpeg":
System.Web.HttpContext.Current.Response.ContentType = "image/jpeg";
break;
case ".gif":
System.Web.HttpContext.Current.Response.ContentType = "image/gif";
break;
case ".pdf":
System.Web.HttpContext.Current.Response.ContentType = "application/pdf";
break;
case ".avi":
System.Web.HttpContext.Current.Response.ContentType = "video/avi";
break;
case ".doc":
System.Web.HttpContext.Current.Response.ContentType = "application/msword";
break;
case ".zip":
System.Web.HttpContext.Current.Response.ContentType = "application/zip";
break;
case ".rar":
System.Web.HttpContext.Current.Response.ContentType = "application/rar";
break;
case ".xls":
System.Web.HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
break;
case ".wav":
System.Web.HttpContext.Current.Response.ContentType = "audio/wav";
break;
case ".mp3":
System.Web.HttpContext.Current.Response.ContentType = "audio/mpeg3";
break;
case ".mpg":
System.Web.HttpContext.Current.Response.ContentType = "audio/mpeg";
break;
case ".rtf":
System.Web.HttpContext.Current.Response.ContentType = "application/rtf";
break;
case ".htm":
case ".html":
System.Web.HttpContext.Current.Response.ContentType = "text/html";
break;
case ".asp":
System.Web.HttpContext.Current.Response.ContentType = "text/asp";
break;
default:
System.Web.HttpContext.Current.Response.ContentType = "application/octet-stream";
break;
} Response.HeaderEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.AppendHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(downname));
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); //Response.WriteFile(System.Configuration.ConfigurationSettings.AppSettings["TemplatePhysicalPath"].ToString() + pt.Path);
Response.BinaryWrite(buffer);
Response.GetHashCode();
Response.End();
}

C#实现按Word模板导出Word(加书签bookMark)的更多相关文章

  1. 利用模板导出文件(二)之jacob利用word模板导出word文件(Java2word)

    https://blog.csdn.net/Fishroad/article/details/47951061?locationNum=2&fps=1 先下载jacob.jar包.解压后将ja ...

  2. .net core 使用NPOI填充Word模板导出Word

    最近工作用到在Word模板插入数据库数据,导出一个带数据的Word文件,想起来之前操作Word都是用微软提供的Microsoft.Office.Interop.Word,而在最新的..NET CORE ...

  3. java根据word模板导出word文件

    1.word模板文件处理,如下图所示在word 文档中填值的地方写入占位变量 2.将word文档另存为xml文件.编辑如下图,找到填写的占位,修改为${bcrxm}格式 3.将文件后缀名改为.ftl文 ...

  4. SpringBoot集成文件 - 如何基于POI-tl和word模板导出庞大的Word文件?

    前文我们介绍了通过Apache POI通过来导出word的例子:那如果是word模板方式,有没有开源库通过模板方式导出word呢?poi-tl是一个基于Apache POI的Word模板引擎,也是一个 ...

  5. C#通过模板导出Word(文字,表格,图片)

    C#通过模板导出Word(文字,表格,图片)   C#导出Word,Excel的方法有很多,这次因为公司的业务需求,需要导出内容丰富(文字,表格,图片)的报告,以前的方法不好使,所以寻找新的导出方法, ...

  6. Net Core DocXCore 实现word模板导出

    实际工作中,往往有这样的需求,需要导出word,还有各种各样的样式,于是有了word模板导出. 实现以下几个需求: 1.表单导出 2.表格导出 3.表单表格混合导出 4.实际用例测试 解决方案: 实现 ...

  7. OpenXml Sdk 根据Word模板导出到word

    一:OpenXml Sdk 简介 Open XML标准的简单介绍:Ecma Office Open XML(“Open XML”)是针对字处理文档.演示文稿和电子表格的国际化开放标准,可免费供多个应用 ...

  8. JSP利用freemarker生成基于word模板的word文档

    利用freemarker生成基于word模板的word文档 freemarker简介 FreeMarker是一个用Java语言编写的模板引擎,它基于模板来生成文本输出.FreeMarker与Web容器 ...

  9. 使用Spire.Doc组件利用模板导出Word文档

    以前一直是用Office的组件实现Word文档导出,但是让客户在服务器安装Office,涉及到版权:而且Office安装,包括权限配置也是比较麻烦. 现在流行使用第三方组件来实现对Office的操作, ...

随机推荐

  1. Qt 中程序自动重启

    参照至 dbzhang老师的博文,记录于此....... 要想理解重启,先得看看Qt程序怎么退出! 1.退出 int main(int argc, char** argv) { QApplicatio ...

  2. WPF学习:绑定

    原文 http://www.cnblogs.com/SouthAurora/archive/2010/06/30/1768464.html 一.绑定到元素对象 1.元素和元素(XAML.代码) 1.1 ...

  3. poj 1401---求N!末尾0的个数,2的个数一定比5多,观察得来,0的产生即为2*5,去找这个阶乘一行里面5的个数即可

    #include<stdio.h> #include<stdlib.h> int main() { int T,N; while(scanf("%d",&a ...

  4. Struts2 三、指定Struts2处理的请求后缀

    Action的请求通常情况下默认为以.action结尾,例如:http://localhost:9000/Struts2/hello/helloAction_sayHello.action    .a ...

  5. 基于Linux 3.0.8 Samsung FIMC(S5PV210) 的摄像头驱动框架解读(一)

    作者:咕唧咕唧liukun321 来自:http://blog.csdn.net/liukun321 FIMC这个名字应该是从S5PC1x0開始出现的.在s5pv210里面的定义是摄像头接口.可是它相 ...

  6. 如风一样,飞翔------Day37

    我一直都觉得自己是个反应没有那么敏锐的人,而不幸的是其实也是,所以,在菜鸟的头衔上,我不得不再背负上一个笨鸟的"光芒".我不苛求一飞冲天的传奇,却也有着不甘寂寞的激昂,我选择先飞, ...

  7. UC浏览器开发者版调试手机页面

    1 关于RI 目前,在手机上使用浏览器访问网页,无法便捷地进行网页语言调试.手机屏幕相对较小且操作不便,直接在手机上进行网页数据调试不太现实. 因此,我们使用技术将手机网页调试信息分离,实现一种能在大 ...

  8. jquery单页网站导航插件One Page Nav

    这是一个轻量级的jQuery的单页网站导航插件.增加了单击后平滑滚动导航和当你浏览不同的部分时自动选择正确的导航项. changeHash: false, 改变当用户单击导航,就改变changeHas ...

  9. Sizzle一步步实现所有功能(一)

    前提: 1.HTML5自带querySelectAll可以完全替代Sizlle,所以我们下面写的Sizzle,是不考虑QSA的. 2.作者考虑了大量兼容情况,比如黑莓4.6系统这样几乎接触不到的bug ...

  10. html标签总结。

    接触了一段时间的html和css,发现了其中的乐趣.在写代码时一定要仔细,很有可能一个细微的错误便会导致页面的错误 总结了一下html标签. <html></html> 创建一 ...