使用C#调用Word的接口生成doc文件与html文件

作为从C#开始并只使用C#,没有接触过VB,只使用.net,没使用过COM的开发人员,一直不太明白COM是什么鬼。

现看到书上的一个粟子:一个典型的COM就是word操作的API(个人理解)。

     public static void WordTest()
        {
            Application app = new Application { Visible = true };
            app.Documents.Add();
            Document doc = app.ActiveDocument;
            Paragraph par = doc.Paragraphs.Add();
            par.Range.Text = "hehe C# hellop";

            string filename = AppDomain.CurrentDomain.BaseDirectory;
            doc.SaveAs2(FileName: Path.Combine(filename,"demo.html"), FileFormat: WdSaveFormat.wdFormatFilteredHTML);
       doc.SaveAs2(FileName: Path.Combine(filename, "demo.doc"), FileFormat: WdSaveFormat.wdFormatDocument97); doc.Close(); app.Application.Quit(); }

以上是一个静态方法,可以使用控制台调用,并在程序当前路径下生成一个包含文字的doc文件与html文件。

环境:VS2013,添加了Microsoft.Office.Interop.Word的引用,机器上安装了office2010。

原文地址:https://www.cnblogs.com/lihan829/p/5257032.html