asp.net 根据word模板填充数据然后下载

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_27077019/article/details/80854879

我在做这个功能时,参考了好多网上的文章,后面自己摸索调试做出来的

cs:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

using MSWord = Microsoft.Office.Interop.Word;
using Microsoft.Office.Interop.Word;

using System.Reflection;
           string TemplateFile = @"D:/net/test/doc_word/xcbl.doc"; //模版文件的路径,

           string fname = DateTime.Now.ToString("yyyyMMddHHmmssffffff") + ".doc";//新文件名
           string FileName = @"D:/net/test1/test/" + fname;//新文件的路径         
           string path = Server.MapPath("/test1/" + fname);
           if (!File.Exists(TemplateFile))
           {
               // return "error:服务器没有模版";
               this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('服务器没有模版!')</script>");
           }
           else { 
               object missing = System.Reflection.Missing.Value;
               object Visible = false;
               object ReadOnly = false;
            //3)拷贝模版生成新文件

                File.Copy(TemplateFile, FileName);//首先要拷贝一份文件
                object obj_FileName = FileName;//新建一个obj变量用于操作
                 
                  // 4)设置操作类型,操作类型有很多中,这里要打开新文件,修改其中的内容

                      wordDoc = wordApp.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);// wordApp.Documents.Open(ref obj_FileName);//打开新文件,进行操作
     
                      wordDoc.Activate();//设置现在操作的文件

                 //  5)依据书签修改word内容

                    
                          //循环书签,判断书签名称,并为其赋值
                           foreach (MSWord.Bookmark bm in wordDoc.Bookmarks)
                          {
                              //Label1.Text = Label1.Text+","+ bm.Name+",";
                              if (bm.Name == "c_test1")
                                   {
                                   bm.Select();
                                   bm.Range.Text = TextBox1.Text;
                               }
                               else if (bm.Name == "c_test2")
                                  {
                                    bm.Select();
                                    bm.Range.Text = TextBox1.Text;
                                }
                                .....

                           }

                              // 6)关闭文件

                                wordDoc.SaveAs(ref obj_FileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);//保存文件
                                wordDoc.Close(ref missing, ref missing, ref missing);//关闭文档对象
                                wordApp.Quit(ref missing, ref missing, ref missing);//关闭应用程序对象
                       
                           //7)杀进程,有的情况下,关闭Word文件会不成功,会残留很多word进程

                                /* //我这里用的时候注释了,不注释出错
                                     System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcessesByName("WINWORD");
                                     foreach (System.Diagnostics.Process p in processes)
                                    {
                                        bool b = p.MainWindowTitle == "";
                                        if (p.MainWindowTitle == "")
                                         {
                                            p.Kill();
                                          }
                                     } */
                                 
                                //下载
                                        Response.Clear();
                                        Response.Buffer = true;
                                        this.EnableViewState = false;
                                        Response.Charset = "utf-8";
                                        Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fname, System.Text.Encoding.UTF8));
                                        Response.ContentType = "application/octet-stream";
                                        Response.WriteFile(path);
                                        Response.Flush();
                                        Response.Close();
                                        Response.End();          
                                         

         }    

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页