C#使用微软自带控件导出Word要点(含html嵌入word方法)

本文介绍了使用C#结合微软Word控件导出Word文档的关键步骤,包括文件缓存路径设定、初始设置、内容插入、文件输出到浏览器下载的流程。同时,文章还讲解了如何将HTML数据嵌入到Word中,提供了详细的实现方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

基本的可以看这里:https://blog.youkuaiyun.com/luthreestone/article/details/78230777

和这里:https://docs.microsoft.com/zh-cn/dotnet/api/microsoft.office.interop.word?view=word-pia

1、首先是文件缓存路径:object path = HttpContext.Current.Server.MapPath("Export/");这次是object类型。

2、初始设置:

//应用程序
                Application wordApp = new Application();
                //文档
                Document wordDoc;
                wordApp.Visible = false;

                object Nothing = Missing.Value;
                wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
                //设置纸张样式为A4
                wordDoc.PageSetup.PaperSize = WdPaperSize.wdPaperA4;
                //排列方式为垂直方向
                wordDoc.PageSetup.Orientation = WdOrientation.wdOrientPortrait;

3、内容设置:时刻注意光标位置防止内容重叠;

            object unite = WdUnits.wdStory;//常用的度量单位
            object Nothing = Missing.Value;
            wordDoc.Content.InsertAfter("\n");//在内容后新开一行
            wordApp.Selection.EndKey(ref unite, ref Nothing);//将光标移到文本末尾,重要!

4、文件输出至浏览器下载:先保存缓存文件,之后关闭文档解除占用,放到输出流中输出到浏览器下载,再删除缓存。

            // 创建文件
            System.IO.FileStream file = new System.IO.FileStream(path.ToString(), System.IO.FileMode.CreateNew);

            // 关闭释放流,不然没办法写入数据
            file.Close();
            file.Dispose();

            context.Response.Clear();
            context.Response.Buffer = true;
            context.Response.Charset = "utf-8";
            context.Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.docx", productName + "-数据字典"));
            context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
            context.Response.ContentType = "application/ms-word";
            ////保存到指定路径
            object Nothing = Missing.Value;
            object format = WdSaveFormat.wdFormatDocument;
            wordDoc.SaveAs(ref path, ref format, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing,
                ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
            wordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
            wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);

            System.IO.FileInfo fileInfo = new System.IO.FileInfo(path.ToString());
            context.Response.WriteFile(fileInfo.FullName);
            context.Response.Flush();

            File.Delete(path.ToString());
            context.Response.End();

5、html格式数据嵌入word的方法:htmlBodyText即为body里的任意数据,别忘了删除缓存的html文件

 private void InsertHtmlToWord(ref Document wordDoc, ref Application wordApp, string htmlBodyText)
        {
            string htmlSaveText = "<html><head><title></title></head><body>";
            htmlSaveText += htmlBodyText;
            htmlSaveText += "</body></html>";
            //建立html文件
            string path = HttpContext.Current.Server.MapPath("Export/") + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + ".html";
            using (FileStream fs = File.Create(path))
            {
                byte[] info = new UTF8Encoding(true).GetBytes(htmlSaveText);
                fs.Write(info, 0, info.Length);
            }
            //File.AppendAllText(path, htmlSaveText, Encoding.Default);
            object Nothing = Missing.Value;
            wordDoc.Paragraphs.Last.Range.InsertFile(path, ref Nothing, ref Nothing,
                                      ref Nothing, ref Nothing);
        }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值