WordAnalyze.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using Word = Microsoft.Office.Interop.Word;
  2. using System.Diagnostics;
  3. using Logger = Log.Log;
  4. namespace WordAnalyze
  5. {
  6. public class Analyze
  7. {
  8. public Analyze()
  9. {
  10. }
  11. public static string splitChar = "---";
  12. // 将word根据指定的值切割成多个word
  13. public string AnalyzeAndCut()
  14. {
  15. return "";
  16. }
  17. public string AnalyzeFile(string fileName)
  18. {
  19. if (!ValidFileName(fileName))
  20. {
  21. Logger.D("AnalyzeFile with invalid file name {0}", fileName);
  22. return "无效的文件名";
  23. }
  24. object fn = fileName;
  25. Word.ApplicationClass app = new Word.ApplicationClass(); // 打开word应用,只会打开一个,即单例模式
  26. Word.Document doc = null; // 源word文件对象
  27. try
  28. {
  29. doc = app.Documents.Open(ref fn);
  30. Word.Paragraphs garapraph = doc.Paragraphs; // 源文件的内容
  31. bool insert = false; //判断是否有插入数据到新文档中,如果有则保存新word,否则直接关掉新word
  32. int fileIndex = 1; // 切割后word的文件序号,例子: test1.doc, test2.doc
  33. Word.Document newDoc = null;
  34. newDoc = app.Documents.Add();
  35. // 遍历word,通过 --- 三个横线分割
  36. for (int index = 1; index < garapraph.Count; index ++)
  37. {
  38. string text = garapraph[index].Range.Text.ToString();
  39. Logger.D("document parapraph with index({0}) get message ({1})", index, garapraph[index].Range.Text.ToString());
  40. text = text.Trim();
  41. if (splitChar == text)
  42. {
  43. if (insert)
  44. {
  45. object file = Rename(fileName, fileIndex++);
  46. newDoc.SaveAs2(file);
  47. }
  48. newDoc.Close();
  49. newDoc = app.Documents.Add();
  50. insert = false;
  51. continue;
  52. }
  53. garapraph[index].Range.Select();
  54. app.Selection.Copy();
  55. app.Documents[1].Activate();
  56. app.Selection.Paste();
  57. insert = true;
  58. }
  59. if (insert)
  60. {
  61. object file = Rename(fileName, fileIndex++);
  62. newDoc.SaveAs2(file);
  63. }
  64. newDoc.Close();
  65. insert = false;
  66. Logger.D("documents count is {0}", app.Documents.Count);
  67. }
  68. catch (System.Exception e)
  69. {
  70. Logger.E("analyze with file name({0}) error ->({1})", fileName, e.Message.ToString());
  71. return e.Message.ToString();
  72. }
  73. finally
  74. {
  75. if (doc != null) {
  76. doc.Close();
  77. }
  78. if (app != null)
  79. {
  80. app.Quit();
  81. }
  82. }
  83. return "";
  84. }
  85. // 判断文件后缀名是否是.doc 或者.docx
  86. public bool ValidFileName(string fileName)
  87. {
  88. if (fileName == null || fileName.Length < 1)
  89. {
  90. return false;
  91. }
  92. if (!fileName.EndsWith(".doc") && !fileName.EndsWith(".docx"))
  93. {
  94. return false;
  95. }
  96. return true;
  97. }
  98. public string WriteFile(string filename)
  99. {
  100. return "";
  101. }
  102. public string Rename(string filename, int index)
  103. {
  104. int lastIndex = filename.LastIndexOf('.');
  105. string newFilename = string.Format("{0}{1}{2}", filename.Substring(0, lastIndex), index, filename.Substring(lastIndex));
  106. return newFilename;
  107. }
  108. }
  109. }