利用word模板生成word或pdf文件并输出
2010-12-03 14:55
381 查看
利用模板文件生成临时word文件,保存后,输出到客户端,再删除生成的临时word文件
输出保存的word文档
/// <summary> /// 首先创建文档,插入书签,保存为dot格式的word模版文件 /// </summary> /// <param name="temppath">模版绝对路径2003 dot模版</param> /// <param name="outputfilefullname">输出文件绝对路径名.doc</param> /// <param name="markvalue">书签名及对应的书签值</param> public static void CreateWordDoc(string temppath, string exportFilefullname, List<KeyValuePair<string, string>> markvalues) { Object Nothing = System.Reflection.Missing.Value; object outputfullfilename = exportFilefullname; //文件保存绝对全路径 Microsoft.Office.Interop.Word.ApplicationClass appWord = null; //应用程序 Microsoft.Office.Interop.Word.DocumentClass doc = null; //文档 try { appWord = new ApplicationClass(); appWord.Visible = false; object objTrue = true; object objFalse = false; object objTemplate = temppath; //模板路径 object objDocType = WdDocumentType.wdTypeDocument; doc = (DocumentClass)appWord.Documents.Add(ref objTemplate, ref objFalse, ref objDocType, ref objTrue); //给书签赋值 object mark; foreach (KeyValuePair<string, string> pair in markvalues) { mark = pair.Key; if (doc.Bookmarks.Exists(pair.Key)) { doc.Bookmarks.get_Item(ref mark).Range.Text = pair.Value; } } //生成word并保存 object miss = System.Reflection.Missing.Value; object fileformat = (int)Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocument; //若希望输出pdf格式文件,则更改此处,同时修改相应的输出文件绝对路径名为pdf格式 //object fileformat = (int)Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF; doc.SaveAs(ref outputfullfilename, ref fileformat, 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); //退出模版不保存更改 object missingValue = System.Reflection.Missing.Value; object doNotSaveChanges = WdSaveOptions.wdDoNotSaveChanges; doc.Close(ref doNotSaveChanges, ref missingValue, ref missingValue); appWord.Application.Quit(ref miss, ref miss, ref miss); doc = null; appWord = null; } catch (System.Exception ex) { //捕捉异常,如果出现异常则清空实例,退出word,同时释放资源 object miss = System.Reflection.Missing.Value; object doNotSaveChanges = WdSaveOptions.wdDoNotSaveChanges; doc.Close(ref doNotSaveChanges, ref miss, ref miss); appWord.Application.Quit(ref miss, ref miss, ref miss); doc = null; appWord = null; } }
输出保存的word文档
/// <summary> /// 说明:导出本地word文件类型 /// </summary> /// <param name="page"></param> /// <param name="filefullname">临时word文件地址</param> public static void ExportWordFile(System.Web .UI .Page page, string filefullname,bool deleteAfterExport,bool endResponse) { if (File.Exists(filefullname)) { FileInfo fileinfo = new FileInfo(filefullname); page.Response.Clear(); page.Response.Buffer = true; page.Response.Charset = "utf-8"; page.Response.ContentEncoding = System.Text.Encoding.UTF8; page.Response.ContentType = "application/ms-word"; page.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileinfo.Name, Encoding.UTF8)); byte[] sw = File.ReadAllBytes(Convert.ToString(filefullname)); page.Response.BinaryWrite(sw); page.Response.Flush(); if (deleteAfterExport) { File.Delete(filefullname); } if (endResponse) { page.Response.End(); } } }
相关文章推荐
- aspose利用word模板生成word、PDF
- 利用 Pandoc 将 Markdown 生成 Word/PDF 文件
- 利用 Pandoc 将 Markdown 生成 Word/PDF 文件
- JSP利用freemarker生成基于word模板的word文档
- MFC--利用Haru库生成PDF文件
- 【LaTeX】配置Emacs org-mode利用latex生成pdf文件
- 利用itextsharp生成pdf并生成zip文件及其邮件发送
- WORD文件拼版生成PDF方法
- Java Web项目中利用FreeMarker模板生成的word文档,其它格式文档同理,如pdf,txt,java,xml等等
- Asp.net MVC 利用(aspose+pdfobject.js) 实现在线预览word、excel、ppt、pdf文件
- C#生成word文档 、word转pdf、合并pdf文件等
- 从JAVA直接读取EXCEL、WORD并生成PDF文件
- 根据指定Word模板生成Word文件
- 利用PDFLIB开发库生成PDF文件
- asp 使用word模板生成word文件
- Jacob模板替换生成word文件、word合并、word转pdf文件
- 利用pyhton 生成PDF文件
- Flex中利用AMFPHP开发包通过PHP调用轻松生成输出PDF文档
- C# 利用WORD模板和标签(bookmark) 批量生成WORD
- linux C 生成带有格式的文件 wkhtmltopdf(类似word的效果 有颜色 字体等等)