我想在C#中将一个HTML页面转换为DOCX文件,我该怎么做?
我的解决方案使用Html2OpenXml和DocumentFormat.OpenXml(Html2OpenXml的NuGet包在此处)为ASP.NET MVC提供了一种优雅的解决方案。
public static class WordHelper
{
public static byte[] HtmlToWord(String html)
{
const string filename = "test.docx";
if (File.Exists(filename)) File.Delete(filename);
using (MemoryStream generatedDocument = new MemoryStream())
{
using (WordprocessingDocument package = WordprocessingDocument.Create(
generatedDocument, WordprocessingDocumentType.Document))
{
MainDocumentPart mainPart = package.MainDocumentPart;
if (mainPart == null)
{
mainPart = package.AddMainDocumentPart();
new Document(new Body()).Save(mainPart);
}
HtmlConverter converter = new HtmlConverter(mainPart);
Body body = mainPart.Document.Body;
var paragraphs = converter.Parse(html);
for (int i = 0; i < paragraphs.Count; i++)
{
body.Append(paragraphs[i]);
}
mainPart.Document.Save();
}
return generatedDocument.ToArray();
}
}
}
[HttpPost]
[ValidateInput(false)]
public FileResult Demo(CkEditorViewModel viewModel)
{
return File(WordHelper.HtmlToWord(viewModel.CkEditorContent),
"application/vnd.openxmlformats-officedocument.wordprocessingml.document");
}
我正在使用CKEditor来生成此示例的HTML。
以下代码做的事情与Luis代码相同,但更易读,并应用于一个ASP.NET MVC应用程序:
var word = new Microsoft.Office.Interop.Word.Application();
word.Visible = false;
var filePath = Server.MapPath("~/MyFiles/Html2PdfTest.html");
var savePathPdf = Server.MapPath("~/MyFiles/Html2PdfTest.pdf");
var wordDoc = word.Documents.Open(FileName: filePath, ReadOnly: false);
wordDoc.SaveAs2(FileName: savePathPdf, FileFormat: WdSaveFormat.wdFormatPDF);
您还可以以其他格式保存,例如像这样的docx:
var savePathDocx = Server.MapPath("~/MyFiles/Html2PdfTest.docx");
var wordDoc = word.Documents.Open(FileName: filePath, ReadOnly: false);
wordDoc.SaveAs2(FileName: savePathDocx, FileFormat: WdSaveFormat.wdFormatXMLDocument);
wordDoc.Close()
和 wordDoc.Quit()
方法以释放对象,否则你将会有许多 word 进程在后台运行。 - Dan DiploInterop.Word.Application
,并且不建议这样做。参考链接:https://stackoverflow.com/a/8709255/87698 - Heinzi使用该代码进行转换
Microsoft.Office.Interop.Word.Application word =
new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document wordDoc =
new Microsoft.Office.Interop.Word.Document();
Object oMissing = System.Reflection.Missing.Value;
wordDoc = word.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
word.Visible = false;
Object filepath = "c:\\page.html";
Object confirmconversion = System.Reflection.Missing.Value;
Object readOnly = false;
Object saveto = "c:\\doc.pdf";
Object oallowsubstitution = System.Reflection.Missing.Value;
wordDoc = word.Documents.Open(ref filepath, ref confirmconversion,
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);
object fileFormat = WdSaveFormat.wdFormatPDF;
wordDoc.SaveAs(ref saveto, 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 oallowsubstitution, ref oMissing,
ref oMissing);
你可以考虑使用altChunk。请参见从altchunk创建的openxml文档中添加图像等文章。
如果你不想依赖Word来转换HTML,你可以尝试使用docx4j-ImportXHTML for .NET;请参见这个演示。
Aspose.Words for .NET 是一款商业组件,可以帮助您实现此功能。
MigraDoc可以帮助您。 或者使用VS工具进行Office开发。 或通过COM连接到Office。
微软不建议在 Web 服务器上使用办公应用程序。但是,使用 OpenXML 2.5 可以相对容易地实现。
你只需要通过 ("<", ">") 分割 HTML,然后将每个部分放入 switch 中,并确定它是否为 HTML 标记即可。
然后,你可以开始将每个部分转换为 "Run" 和 "RunProperties" 的 HTML,而非 HTML 文本则简单地放置在 "Text" 中。
听起来比实际要难...是的,我也不知道为什么没有可用的代码可以完全做到这一点。
需要注意的事项。 两种格式不能干净地相互转换,因此,如果你专注于编写最干净的代码,则会遇到格式本身变得混乱的问题。