Asp.net通过模板(.dot/Html)导出Word,同时导出图片

一、Office组件导出Word(服务器配置麻烦)

需要引用Office的DLL,在下文的附件中,不同的Offic版本用的不一样,虽然高级版本可以兼容低级的,不过,还是统一版本最好

贴上核心代码(转载):

 1         Microsoft.Office.Interop.Word._Application appWord = new Microsoft.Office.Interop.Word.ApplicationClass();
 2         Microsoft.Office.Interop.Word._Document docFile = null;
 3         try
 4         {
 5             appWord.Visible = false;
 6             object objTrue = true;
 7             object objFalse = false;
 8             object objTemplate = Server.MapPath(@"dot//123.dot");//模板路径
 9             object objDocType = Microsoft.Office.Interop.Word.WdDocumentType.wdTypeDocument;
10             docFile = appWord.Documents.Add(ref objTemplate, ref objFalse, ref objDocType, ref objTrue);
11             //第一步生成word文档
12             //定义书签变量
13             object obDD_Name = "DD_Name";//姓 名
14             object obDD_Sex = "DD_Sex";//性 别
15             object obDD_Age = "DD_Age";//年龄
16             object obDD_Birthday = "DD_Birthday"; //出生年月
17             object obDD_Nation = "DD_Nation"; //民 族
18             object obDD_Native = "DD_Native"; //籍 贯
19 
20             //第二步 读取数据,填充数据集
21             //SqlDataReader dr = XXXXX;//读取出来的数据集
22             //第三步 给书签赋值
23             //给书签赋值
24             docFile.Bookmarks.get_Item(ref obDD_Name).Range.Text = "姓 名"; //姓 名
25             docFile.Bookmarks.get_Item(ref obDD_Sex).Range.Text = "性 别";
26             docFile.Bookmarks.get_Item(ref obDD_Age).Range.Text = "年龄";
27 
28        //存入图片
29             object objTrue = true;
30             docFile.Bookmarks.get_Item("Photo").Range.InlineShapes.AddPicture(HttpContext.Current.Server.MapPath(@"" + imgUrl), ref miss, ref objTrue, ref miss);
31             //第四步 生成word
32             DateTime dt = DateTime.Now;
33             object filename = Server.MapPath("dot//") + "" + dt.Ticks.ToString() + ".doc";
34             object miss = System.Reflection.Missing.Value;
35             docFile.SaveAs(ref filename, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss);
36             object missingValue = Type.Missing;
37             object doNotSaveChanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;
38             docFile.Close(ref doNotSaveChanges, ref missingValue, ref missingValue);
39             appWord.Quit(ref miss, ref miss, ref miss);
40             docFile = null;
41             appWord = null;
42         }
43         catch (Exception ex)
44         {
45             //捕捉异常,如果出现异常则清空实例,退出word,同时释放资源
46             string aa = e.ToString();
47             object miss = System.Reflection.Missing.Value;
48             object missingValue = Type.Missing;
49             object doNotSaveChanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;
50             docFile.Close(ref doNotSaveChanges, ref missingValue, ref missingValue);
51             appWord.Quit(ref miss, ref miss, ref miss);
52             docFile = null;
53             appWord = null;
54             throw ex;
55         }

 

服务端生成后,附加简单的流下载代码:

 1                 string fileName = "表.doc";//客户端保存的文件名
 2                 string filePath = Server.MapPath("~/Download//Word//Table1.doc");//路径
 3 
 4                 //以字符流的形式下载文件
 5                 FileStream fs = new FileStream(filePath, FileMode.Open);
 6                 byte[] bytes = new byte[(int)fs.Length];
 7                 fs.Read(bytes, 0, bytes.Length);
 8                 fs.Close();
 9                 Response.ContentType = "application/octet-stream";
10                 //通知浏览器下载文件而不是打开
11                 Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
12                 Response.BinaryWrite(bytes);
13                 Response.Flush();
14                 Response.End();

附上各个Office版本Dll库:

各个版本的microsoft.office.interop.word库.rar

 

二、通过htm模板导出(此方法简单,无需word组件支持!样式标准,不过有些缺陷,导出图片比较麻烦,而且,效果不好)

1、本地新建一个Word文档,然后编写文档格式,文字填充部分用{Title}等特殊标记表示,方便替换。

2、另存文档为网页格式(Htm、Html)。

3、将生成的模板网页放在网站目录下。

4、上代码:

 1 protected void Button1_Click(object sender, EventArgs e)
 2         {
 3             string strWord = ExprotMissionToWord(Server.MapPath("Temp.htm"));
 4             Response.ContentEncoding = System.Text.Encoding.UTF7;
 5             Response.ClearContent();
 6             Response.ClearHeaders();
 7             Response.AddHeader("content-disposition", "attachment;filename=magazineturn.doc"); 
 8             Response.AddHeader("Content-type", "application");
 9             Response.ContentType = "application/ms-html";
10             Response.ContentEncoding = System.Text.Encoding.Default; 
11             Response.Write(strWord);
12             Response.Flush();
13             Response.Close();
14         }
15 
16         public string ExprotMissionToWord(string templatePath)
17         {
18             StringBuilder sb = new StringBuilder(1024);
19             StreamReader sr = new StreamReader(templatePath, Encoding.Default);
20             sb.Append(sr.ReadToEnd());
21             sr.Close();
22             sb.Replace("{title}", "我是标题");
23             sb.Replace("{content} ", "我是正文");
24             return sb.ToString();
25         }

内容应该通俗易懂了,全部都是基础方法,就不细说了。

简单的说明一下,原理,是IO读取这个模板,然后替换掉设置的特殊字符,然后输出,ok!完成。

你可能感兴趣的:(Asp.net通过模板(.dot/Html)导出Word,同时导出图片)