WordAnalyze.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. public string path = "";
  13. public void SetPath(string path)
  14. {
  15. this.path = path;
  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. doc.Paragraphs.Add();
  31. Word.Paragraphs garapraph = doc.Paragraphs; // 源文件的内容
  32. bool insert = false; //判断是否有插入数据到新文档中,如果有则保存新word,否则直接关掉新word
  33. int fileIndex = 1; // 切割后word的文件序号,例子: test1.doc, test2.doc
  34. Word.Document newDoc = null;
  35. newDoc = app.Documents.Add();
  36. // 遍历word,通过 每行开头 # 横线分割
  37. for (int index = 1; index < garapraph.Count; index ++)
  38. {
  39. string text = garapraph[index].Range.Text.ToString();
  40. // Logger.D("document parapraph with index({0}) get message ({1})", index, garapraph[index].Range.Text.ToString());
  41. text = text.Trim();
  42. if (text.StartsWith(splitChar))
  43. {
  44. if (insert)
  45. {
  46. object file = Rename(fileName, fileIndex++);
  47. newDoc.SaveAs2(file);
  48. }
  49. newDoc.Close();
  50. newDoc = app.Documents.Add();
  51. object findText = splitChar;
  52. object replace = "";
  53. object missing = null;
  54. object replaceOne = Word.WdReplace.wdReplaceOne;
  55. try
  56. {
  57. var findObject = garapraph[index].Range.Find;
  58. findObject.ClearFormatting();
  59. findObject.Text = splitChar;
  60. findObject.Replacement.ClearFormatting();
  61. findObject.Replacement.Text = "";
  62. findObject.Execute(findText, ref missing, ref missing, ref missing, ref missing,
  63. ref missing, ref missing, ref missing, ref missing, ref missing,
  64. ref replaceOne, ref missing, ref missing, ref missing, ref missing);
  65. }
  66. catch (System.Exception err)
  67. {
  68. Logger.E("analyze with file name({0}) replace {1} error ->({2})", fileName, splitChar, err.Message.ToString());
  69. return err.Message.ToString();
  70. }
  71. }
  72. garapraph[index].Range.Select();
  73. app.Selection.Copy();
  74. app.Documents[1].Activate();
  75. app.Selection.Paste();
  76. insert = true;
  77. }
  78. if (insert)
  79. {
  80. object file = Rename(fileName, fileIndex++);
  81. newDoc.SaveAs2(file);
  82. }
  83. newDoc.Close();
  84. insert = false;
  85. }
  86. catch (System.Exception e)
  87. {
  88. Logger.E("analyze with file name({0}) error ->({1})", fileName, e.Message.ToString());
  89. return e.Message.ToString();
  90. }
  91. finally
  92. {
  93. if (doc != null) {
  94. while (true)
  95. {
  96. bool undo = doc.Undo();
  97. if(!undo)
  98. {
  99. break;
  100. }
  101. }
  102. doc.Save();
  103. doc.Close();
  104. }
  105. if (app != null)
  106. {
  107. app.Quit();
  108. }
  109. }
  110. return "";
  111. }
  112. // 判断文件后缀名是否是.doc 或者.docx
  113. public bool ValidFileName(string fileName)
  114. {
  115. if (fileName == null || fileName.Length < 1)
  116. {
  117. return false;
  118. }
  119. if (!fileName.EndsWith(".doc") && !fileName.EndsWith(".docx"))
  120. {
  121. return false;
  122. }
  123. return true;
  124. }
  125. // 用设定的目录当做子word的目录,默认为选择文件的目录
  126. public string Rename(string filename, int index)
  127. {
  128. var filePathIndex = filename.LastIndexOf('\\');
  129. string name = "";
  130. string tempPath = "";
  131. if (-1 != filePathIndex)
  132. {
  133. name = filename.Substring(filePathIndex);
  134. if (name.Length > 0)
  135. {
  136. name = name.Substring(1);
  137. }
  138. tempPath = filename.Substring(0, filePathIndex);
  139. }
  140. if ("" == path)
  141. {
  142. path = tempPath;
  143. }
  144. string newFilename = name;
  145. newFilename = path + "\\" + name;
  146. int lastIndex = newFilename.LastIndexOf('.');
  147. string newName = "";
  148. if (-1 != lastIndex)
  149. {
  150. newName = string.Format("{0}{1}{2}", newFilename.Substring(0, lastIndex), index, newFilename.Substring(lastIndex));
  151. }
  152. else
  153. {
  154. newName = string.Format("{0}{1}", newFilename, index);
  155. }
  156. return newName;
  157. }
  158. }
  159. }