如何在不使用任何API和doc.spire的情况下将HTML文件转换为C#中的Word文档
我已经用这段代码创建了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-
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的情况下执行此操作?我不确定这是否适用于每种类型的网页,但如果您只想存储文本部分(我刚刚测试过),可以执行以下操作:
.doc
,这将自动将其转换为文档文件我不确定这是否适用于每种类型的网页,但如果您只想存储文本部分(我刚刚测试过),您可以这样做:
.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