首先引入 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'>

C# html代码生成word的更多相关文章

  1. [java 2019-04-09] 代码生成word文档中的表格嵌套问题

    public static void createContent3(Date adtStart, Date adtEnd, Map<String, Object> aMap,Map< ...

  2. POI 生成 word 文档 简单版(包括文字、表格、图片、字体样式设置等)

      POI 生成word 文档 一般有两种方法: ① word模板 生成word 文档 : ② 写代码直接生成 word 文档: 我这里演示的是第二种方法,即写代码生成 word文档,不多说废话,直接 ...

  3. Java使用freemarker导出word文档

    通过freemarker,以及JAVA,导出word文档. 共分为三步: 第一步:创建模板文件 第二步:通过JAVA创建返回值. 第三步:执行 分别介绍如下: 第一步: 首先创建word文档,按照想要 ...

  4. 开源API文档工具- swagger2 与 smart-doc 比较 与 使用

    工具开源地址 swagger2 : https://swagger.io/ smart-doc: https://www.oschina.net/p/smart-doc  国产 两者的比较 swagg ...

  5. atitit..代码生成流程图 流程图绘制解决方案 java  c#.net  php v2

    atitit..代码生成流程图 流程图绘制解决方案 java  c#.net  php v2 1.1. Markdown 推荐,就是代码和flow都不能直接使用.1 1.2. Java code2fl ...

  6. 用 Freemarker 生成 word 文档(包含图片)

    1. 用word写一个需要导出的word模板,然后存为xml格式. 2. 将xml中需要动态修改内容的地方,换成freemarker的标识符,例如: <w:p wsp:rsidR="0 ...

  7. 利用Aspose.Word控件实现Word文档的操作

    Aspose系列的控件,功能都挺好,之前一直在我的Winform开发框架中用Aspose.Cell来做报表输出,可以实现多样化的报表设计及输出,由于一般输出的内容比较正规化或者多数是表格居多,所以一般 ...

  8. 黄聪:利用Aspose.Word控件实现Word文档的操作(转)

    撰写人:伍华聪  http://www.iqidi.com  Aspose系列的控件,功能都挺好,之前一直在我的Winform开发框架中用Aspose.Cell来做报表输出,可以实现多样化的报表设计及 ...

  9. 基于 Eclipse 平台的代码生成技术

    ------------------------------------------------------------------ 转自http://www.ibm.com/developerwor ...

随机推荐

  1. phpcms 电脑手机合并

    电脑手机端 ========================== <script type="text/javascript"> function browserRed ...

  2. python 图片滑动窗口

    METHOD #1: No smooth, just scaling. def pyramid(image, scale=1.5, minSize=(30, 30)): # yield the ori ...

  3. Pavel and barbecue

    Pavel and barbecue time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  4. csu1395模拟

    #include<stdio.h> #include<string.h> #define N  10 char s[N][N][N]={{"***",&qu ...

  5. asp.net-EF-表间关系

    博客推荐 http://www.cnblogs.com/Gyoung/archive/2013/01/17/2864150.html 先学习下这几个方法 Has方法: HasOptional:前者包含 ...

  6. Cocos2d-x3.0RC2 EditBox

    EditBox样例 将例如以下代码拷贝到新建的project中就能看到效果. HelloWorldScene.h #include "cocos2d.h" /*这里要引入头文件*/ ...

  7. &#39;hibernate.dialect&#39; must be set when no Connection available

    今天碰到的这个问题,非常无厘头.网上搜索了非常多.都不靠谱,还是靠自己 解决方法是在hibernate.cfg.xml中加入 <property name="dialect" ...

  8. Tomcat报错合集

    1.java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start c ...

  9. C语言高速入门系列(一)

    C语言高速入门系列(一)  本系列引言: 本教程的宗旨是将C语言入门的内容进行关键知识点的提纯,将一些笼统的废话去除; 再进行压缩,然后将本章的关键知识点做成路线图的,能够更加方便地掌握学习的方向; ...

  10. hdu 1698 Just a Hook 基本线段树

    使用线段树更新每段区间的奖(1,2,3),最后在统计整段区间的数和,基本线段树,果断1A啊 #include<iostream> #include<stdio.h> using ...