码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2014-07-30 11:55:43      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   文件   io   数据   

需要引用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,布布扣,bubuko.com

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

标签:style   blog   http   color   os   文件   io   数据   

原文地址:http://www.cnblogs.com/likeli/p/3877287.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!