zoukankan      html  css  js  c++  java
  • 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'>
  • 相关阅读:
    B端产品经理的价值
    APP场景流程分析
    大众点评
    降低成本
    抽象不变的,可变的
    优惠券统计报表
    JSON WEB TOKEN
    vue开发项目的坑-[Vue warn]: Do not use built-in or reserved HTML elements as component id: MenuItem
    redis分布式锁和消息队列
    thinkphp 面向切面编程-行为拓展
  • 原文地址:https://www.cnblogs.com/nicopoiduang/p/10232060.html
Copyright © 2011-2022 走看看