我想将一个html页面转换为c#中的docx,我该怎么做?将Html转换为Docx中的c#
回答
使用的代码转换
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);
Aspose.Words for .NET是一个商业组件,允许您实现这一点。
使用Aspose [将HTML转换为Word文档]的示例(http://www.aspose.com/documentation/.net-components/aspose.words-for-.net/aspose.words.loadformat.html)。 .NET的词语也可以查看。 – 2011-08-23 16:57:35
ASPOSE从html转到docx时遇到了一些麻烦,比如造型和图片格式问题,这些问题对我来说似乎很基本,他们认为它们是产品限制... – Ariel 2012-05-24 16:10:12
同意。缺乏对css的支持,即使是嵌入式css,也意味着您必须自己格式化所有表格,段落和列表。 – nullnvoid 2015-11-18 01:28:57
MigraDoc可以提供帮助。 或使用Office工具VS工具。 或通过COM连接到Office。
的OpenXML的SDK允许你以编程方式生成的docx文件:
您可以考虑使用altChunk。除其他外,请参阅adding images to openxml doc created from altchunk
如果您不想依赖Word来转换HTML,则可以尝试docx4j-ImportXHTML for .NET;见this walkthrough。
您可以考虑使用PHPDocX,它提供了一个非常方便的工具来将HTML文件和/或HTML字符串转换为WordML。
它有大量的选项,其中包括:
- 可以过滤使用其中的HTML块应该 被插入到Word文档中的CSS样式选择。
- 你可以选择下载图像或作为外部链接。
- 它解析HTML表单。
- 您可以将原生Word样式用于覆盖原始CSS的表格和段落。
- 转换Word书签中的HTML锚点。
- 诸如此类
我希望你觉得它有用:-)
下面做同样的事情路易斯代码,但只是有点更具可读性和适用于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()'来处理对象,否则你会留下背景中运行的单词的实例。 – 2016-07-26 13:25:51
我的解决方案使用Html2OpenXml以及DocumentFormat.OpenXml(NuGet package for Html2OpenXml is here)为ASP.NET MVC提供了一个优雅的解决方案。
WordHelper.cs
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。
Microsoft不建议在Web服务器上使用Office应用程序。 然而,这可以很容易使用的OpenXML 2.5
所有你要真正做的是分裂的(“<”,‘>’)的HTML 然后为每个部分它推到一个开关,如果确定要做是否是HTML标记。
然后对于每个部分,你可以开始转换HTML为“运行”和“RunProperties”和非HTML文本被简单地放置到“文本”
这听起来更难那么它是...是的,我不知道为什么没有可用的代码来做到这一点。
需要注意的事项。 这两种格式不会完全相互转换,所以如果您将注意力集中在最干净的代码上,您会遇到自身变得混乱的格式问题。
- 1. 将.doc/docx转换为Html
- 2. 将HTML转换为DOCX
- 3. 将HTML转换为DOCX
- 4. 将.docx转换为html
- 5. 在C++中将.docx转换为.txt
- 6. Docx4j将HTML转换成DOCX
- 7. 将doc/docx转换为语义HTML
- 8. 将HTML(tinyMCE)转换为WORD(.docx)
- 9. 将HTML转换为odt,doc,docx
- 10. 使用JAVA将.docx转换为HTML
- 11. 将.doc转换为.docx与c#
- 12. 将docx转换为pdf()
- 13. 将docx转换为xml
- 14. 将word(.docx)转换为docbook
- 15. 将docx转换为pdf
- 16. 将docx转换为postscript
- 17. 将DOCX转换为Ruby中的DOC
- 18. 转换的docx文件,HTML
- 19. 将Doc或Docx转换为Java中的HTML
- 20. 如何将html页面转换为.net 2.0中的docx
- 21. 将Play Framework中的文件(特别是.doc/.docx转换为.html)
- 22. c#将pdf转换为html
- 23. 将html转换为word c#
- 24. c#将HTML转换为JPG
- 25. 在doc中将docx转换为pdf
- 26. HtmlToOpenXml - HTML到Docx转换 -
- 27. 在ASP.NET中将HTML转换为Docx时保持CSS样式
- 28. 将DOC文件转换为c#或php或.net中的DOCX
- 29. 将PDF,Doc和Docx转换为c#中的rtf
- 30. 将docx转换为带有java的doc
http://stackoverflow.com/questions/32151/best-way-to-ex-port-html-to-word-without-having-ms-word-installed – 2011-03-25 11:11:46
@PranayRana,这篇文章有比这更好的信息比你已经链接了一个。你有没有注意到这一点? – Rahul 2016-09-09 13:14:50