`
李科笠
  • 浏览: 64544 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

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;
        }

 

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

              string fileName = "表.doc";//客户端保存的文件名
                string filePath = Server.MapPath("~/Download//Word//Table1.doc");//路径

                //以字符流的形式下载文件
                FileStream fs = new FileStream(filePath, FileMode.Open);
                byte[] bytes = new byte[(int)fs.Length];
                fs.Read(bytes, 0, bytes.Length);
                fs.Close();
                Response.ContentType = "application/octet-stream";
                //通知浏览器下载文件而不是打开
                Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
                Response.BinaryWrite(bytes);
                Response.Flush();
                Response.End();

 

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics