利用 Aspose.Words 组件,在不依赖与 Office 组件的情况下把 Word 文件转换成 HTML 代码。
2013-06-25 15:55 音乐让我说 阅读(1253) 评论(0) 收藏 举报首先利用 Nuget 获取 Aspose.Words,当前最新版本是:Aspose.Words.13.4.0
测试代码如下:
public ActionResult AsposeWordsDemo() { string srcFileName = Server.MapPath("~/Data/a.doc"); Document doc = new Document(srcFileName); string basicDirVirtualPath = "/UploadFiles/"; string tempDir = Server.MapPath(basicDirVirtualPath); HtmlSaveOptions saveOptions = new HtmlSaveOptions(); // Specify folder where images will be saved. saveOptions.ImagesFolder = tempDir; // We want the images in the HTML to be referenced in the e-mail as attachments so add the cid prefix to the image file name. // This replaces what would be the path to the image with the "cid" prefix. saveOptions.ImagesFolderAlias = basicDirVirtualPath; // Header footers don't normally export well in HTML format so remove them. saveOptions.ExportHeadersFootersMode = ExportHeadersFootersMode.None; // saveOptions.ExportHeadersFooters = false; // 老版本用这个 // Save the document to stream in HTML format. MemoryStream htmlStream = new MemoryStream(); doc.Save(htmlStream, saveOptions); // Read the HTML from the stream as plain text. string htmlText = Encoding.UTF8.GetString(htmlStream.ToArray()); htmlStream.Close(); // Save the HTML into the temporary folder. string htmlFileNameWithoutPath = "Message.html"; Stream htmlFile = new FileStream(Path.Combine(tempDir, htmlFileNameWithoutPath), FileMode.Create); StreamWriter htmlWriter = new StreamWriter(htmlFile); htmlWriter.Write(htmlText); htmlWriter.Close(); htmlFile.Close(); return Redirect(basicDirVirtualPath + htmlFileNameWithoutPath); }
另外 ewebeditor 在线编程器也支持导入 Word,自动提取 HTML 代码,不过要收费。
谢谢浏览!
作者:音乐让我说(音乐让我说 - 博客园)
出处:http://music.cnblogs.com/
文章版权归本人所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。