C# Html代码生成Word

首先引入 Microsoft.Office.Interop.Word
在这里插入图片描述
其次要先说一下,把一大段html代码直接变成word文件,只能生成doc文件,docx文件应该是不行的!

首先我们用IO生成一个doc文件

FileStream fs = new FileStream(路径+文件名+.doc, FileMode.Create);
            StreamWriter sw = new StreamWriter(fs);
            sw.Write("<html>" + html代码+ "</html>");
            sw.Close();
            sw.Dispose();
            fs.Close();
            fs.Dispose();
            createWord(FileName, SavaPath);

这时就会生成一个doc文件,里面内容也是我们想要的样子,所以这就完成了吗?当然不是

你把这个doc文件另存为一下,会发现它默认选中的格式是html

这是因为它本质还是一个html文件,只不过后缀名为doc而已,所以我们需要再把他变成真正的word文件。

private string createWord(string filename,string savepath)
        {
            string file = ""; //路径1(我们之前生成的文件路径)
            string file2 = "";//路径2
            file = savepath + filename + ".doc";
            string demo = System.Web.Hosting.HostingEnvironment.MapPath("路径2");
            file2 = demo + filename + "_.doc";

            Object path = file as Object;
            Object path2 = file2 as Object;
            Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
            Object Nothing = Missing.Value;
            Object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocument;
            Microsoft.Office.Interop.Word.Document wordDoc = wordApp.Documents.Open(ref path, false); //打开之前生成的文件

            wordDoc.Activate();//设为当前操作的文件
            //指定要在页面视图中显示的文档元素
            wordApp.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument;//设为主文档



            //设置文档为页面视图模式
            wordApp.ActiveWindow.View.Type = Microsoft.Office.Interop.Word.WdViewType.wdNormalView;



            wordApp.ActiveWindow.ActivePane.Selection.WholeStory();
            //指定要应用于段落的行距格式
            wordApp.ActiveWindow.ActivePane.Selection.ParagraphFormat.LineSpacingRule = Microsoft.Office.Interop.Word.WdLineSpacing.wdLineSpace1pt5;//1.5 倍行距。该行距相当于当前字号加 6 磅。
            //设置指定段落的段后间距
            wordApp.ActiveWindow.ActivePane.Selection.ParagraphFormat.LineUnitAfter = 0.5f;

            //把操作后的文件保存到路径2
            wordDoc.SaveAs(ref path2, 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);
            //删除原本生成的文件
            File.Delete(file);
            //把路径2的文件剪切到路径1
            File.Move(file2, file);
            //返回路径
            return file;
        }

经过createWord方法后生成的文件就是真正的word文件了,而且展现的内容以及样式也和html时的一样,这样就完成了

顺便加个小知识点

如果要在文件中加入分页符,就在html对应的地方加上这段代码

<br clear=all style='page-break-before:always'>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值