c# – 使用Visual Studio 2010 Express将.doc保存/转换为.html

前端之家收集整理的这篇文章主要介绍了c# – 使用Visual Studio 2010 Express将.doc保存/转换为.html前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个word文档的文件夹,我想转换为html进行进一步处理.我只有Visual Studio 2010 Express版.可以使用快递版吗?我找到了如何进行转换的示例,但它们需要Microsoft.Office.Tools.Word库,它不附带Express.

编辑:我发现它,它实际上在COM对象中称为Microsoft Word 12.0对象库,它是Microsoft.Office.Interop.Word命名空间.

解决方法

您应该能够使用快速版本.我改编了 this question的答案.改编的代码如下.您需要添加对Microsoft.Office.Interop.Word的引用才能使其正常工作.如果您错过了这个库,请查看 this article on MSDN.

查看WdSaveFormat,您还可以将其另存为格式过滤HTML(wdFormatFilteredHTML).

  1. namespace Sample {
  2. using Microsoft.Office.Interop.Word;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8.  
  9. class Program {
  10.  
  11. public static void Main()
  12. {
  13. Convert("C:\\Documents",WdSaveFormat.wdFormatHTML);
  14. }
  15.  
  16. private static void Convert(string path,WdSaveFormat format)
  17. {
  18.  
  19. DirectoryInfo dirInfo = new DirectoryInfo(path);
  20. FileInfo[] wordFiles = dirInfo.GetFiles("*.doc");
  21. if (wordFiles.Length == 0) {
  22. return;
  23. }
  24.  
  25. object oMissing = System.Reflection.Missing.Value;
  26. Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
  27. try {
  28. word.Visible = false;
  29. word.ScreenUpdating = false;
  30. foreach (FileInfo wordFile in wordFiles) {
  31. Object filename = (Object)wordFile.FullName;
  32. Document doc = word.Documents.Open(ref filename,ref oMissing,ref oMissing);
  33. try {
  34. doc.Activate();
  35. object outputFileName = wordFile.FullName.Replace(".doc",".html");
  36. object fileFormat = format;
  37. doc.SaveAs(ref outputFileName,ref fileFormat,ref oMissing);
  38.  
  39. }
  40. finally {
  41. object saveChanges = WdSaveOptions.wdDoNotSaveChanges;
  42. ((_Document)doc).Close(ref saveChanges,ref oMissing);
  43. doc = null;
  44. }
  45. }
  46.  
  47. }
  48. finally {
  49. ((_Application)word).Quit(ref oMissing,ref oMissing);
  50. word = null;
  51. }
  52. }
  53. }
  54. }

猜你在找的C#相关文章