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

基本的可以看这里:https://blog.csdn.net/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 = "";
            htmlSaveText += htmlBodyText;
            htmlSaveText += "";
            //建立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);
        }

 

你可能感兴趣的:(C#)