Asp.net通过模板(.dot)导出Word

需要引用Office的DLL,在附件中

 

贴上核心代码(转载):

Microsoft.Office.Interop.Word._Application appWord = new Microsoft.Office.Interop.Word.ApplicationClass();
        Microsoft.Office.Interop.Word._Document docFile = null;
        try
        {
            appWord.Visible = false;
            object objTrue = true;
            object objFalse = false;
            object objTemplate = Server.MapPath(@"dot//123.dot");//模板路径
            object objDocType = Microsoft.Office.Interop.Word.WdDocumentType.wdTypeDocument;
            docFile = appWord.Documents.Add(ref objTemplate, ref objFalse, ref objDocType, ref objTrue);
            //第一步生成word文档
            //定义书签变量
            object obDD_Name = "DD_Name";//姓 名
            object obDD_Sex = "DD_Sex";//性 别
            object obDD_Age = "DD_Age";//年龄
            object obDD_Birthday = "DD_Birthday"; //出生年月
            object obDD_Nation = "DD_Nation"; //民 族
            object obDD_Native = "DD_Native"; //籍 贯

            //第二步 读取数据,填充数据集
            //SqlDataReader dr = XXXXX;//读取出来的数据集
            //第三步 给书签赋值
            //给书签赋值
            docFile.Bookmarks.get_Item(ref obDD_Name).Range.Text = "姓 名"; //姓 名
            docFile.Bookmarks.get_Item(ref obDD_Sex).Range.Text = "性 别";
            docFile.Bookmarks.get_Item(ref obDD_Age).Range.Text = "年龄";

            //第四步 生成word
            DateTime dt = DateTime.Now;
            object filename = Server.MapPath("dot//") + "表" + dt.Ticks.ToString() + ".doc";
            object miss = System.Reflection.Missing.Value;
            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);
            object missingValue = Type.Missing;
            object doNotSaveChanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;
            docFile.Close(ref doNotSaveChanges, ref missingValue, ref missingValue);
            appWord.Quit(ref miss, ref miss, ref miss);
            docFile = null;
            appWord = null;
        }
        catch (Exception ex)
        {
            //捕捉异常,如果出现异常则清空实例,退出word,同时释放资源
            string aa = e.ToString();
            object miss = System.Reflection.Missing.Value;
            object missingValue = Type.Missing;
            object doNotSaveChanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;
            docFile.Close(ref doNotSaveChanges, ref missingValue, ref missingValue);
            appWord.Quit(ref miss, ref miss, ref miss);
            docFile = null;
            appWord = null;
            throw ex;
        }

  

 

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

 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

 

Asp.net通过模板(.dot)导出Word,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。