Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在不使用任何API和doc.spire的情况下将HTML文件转换为C#中的Word文档_C#_Asp.net_Html_Ms Word_Vsto - Fatal编程技术网

如何在不使用任何API和doc.spire的情况下将HTML文件转换为C#中的Word文档

如何在不使用任何API和doc.spire的情况下将HTML文件转换为C#中的Word文档,c#,asp.net,html,ms-word,vsto,C#,Asp.net,Html,Ms Word,Vsto,我已经用这段代码创建了word文件 System.Text.StringBuilder strHTMLContent = new System.Text.StringBuilder(); strHTMLContent.Append("<html xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-

我已经用这段代码创建了word文件

System.Text.StringBuilder strHTMLContent = new System.Text.StringBuilder();
        strHTMLContent.Append("<html xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:word\" xmlns=\"http://www.w3.org/TR/REC-html40\"><head></head><body>");
        strHTMLContent.Append(var);
        strHTMLContent.Append("</body></html>");
        File.WriteAllText(@"E:\doc2.html", strHTMLContent.ToString());
但问题是它直接显示下载选项。 但我想把它存储在服务器上


如何在不使用任何API的情况下执行此操作?

我不确定这是否适用于每种类型的网页,但如果您只想存储文本部分(我刚刚测试过),可以执行以下操作:

  • 创建该HTML文件的副本。(如果需要原始HTML,则为可选步骤)
  • 将副本的扩展名更改为
    .doc
    ,这将自动将其转换为文档文件

  • 我不确定这是否适用于每种类型的网页,但如果您只想存储文本部分(我刚刚测试过),您可以这样做:

  • 创建该HTML文件的副本。(如果需要原始HTML,则为可选步骤)
  • 将副本的扩展名更改为
    .doc
    ,这将自动将其转换为文档文件

  • 创建一个将html文件转换为word文件的函数

    私有字符串generateWordDoc() {

            object filename1 = @"E:\doc3.html";//path of html file
            object oMissing = System.Reflection.Missing.Value;
            object readOnly = false;
            object oFalse = false;
    
            Microsoft.Office.Interop.Word.Application oWord = new Microsoft.Office.Interop.Word.Application();
            Microsoft.Office.Interop.Word.Document oDoc = new Microsoft.Office.Interop.Word.Document();
            oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
            oWord.Visible = false;
    
            oDoc = oWord.Documents.Open(ref filename1, ref oMissing, ref readOnly, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
    
    
            filename1 = @"E:\new.doc"; // new document path
            object fileFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocument;
            oDoc.SaveAs(ref filename1, ref fileFormat, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
    
            oDoc.Close(ref oFalse, ref oMissing, ref oMissing);
            oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
    
    
            return "Success";
    
        }
    

    创建一个将html文件转换为word文件的函数

    私有字符串generateWordDoc() {

            object filename1 = @"E:\doc3.html";//path of html file
            object oMissing = System.Reflection.Missing.Value;
            object readOnly = false;
            object oFalse = false;
    
            Microsoft.Office.Interop.Word.Application oWord = new Microsoft.Office.Interop.Word.Application();
            Microsoft.Office.Interop.Word.Document oDoc = new Microsoft.Office.Interop.Word.Document();
            oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
            oWord.Visible = false;
    
            oDoc = oWord.Documents.Open(ref filename1, ref oMissing, ref readOnly, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
    
    
            filename1 = @"E:\new.doc"; // new document path
            object fileFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocument;
            oDoc.SaveAs(ref filename1, ref fileFormat, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
    
            oDoc.Close(ref oFalse, ref oMissing, ref oMissing);
            oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
    
    
            return "Success";
    
        }
    

    您可以尝试包括图像、表格等。我80%确信它会起作用。在word文件中,它是否支持html格式的应用,如粗体、斜体等?是的,它支持这两种格式,加上
    ,标题标记。您可以自己尝试更多标记,看看支持哪些标记。如果答案解决了您的问题,您可以接受answ吗呃?它不支持分页符样式(
    )。你能建议另一种方法吗?@Akashagarwal你可以尝试包括图像、表格等。我80%确信它会起作用。在word文件中,它是否支持应用html格式,如粗体、斜体等?是的,它应该支持这两种格式,加上
    ,标题标记。你可以自己尝试更多的标记,看看支持哪些标记。如果答案为你的问题已经解决了,你能接受答案吗?它不支持分页符样式(
    )。你能建议另一种方式吗?@AkashAgarwal