WordAnalyze.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 (splitChar == text)
  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. insert = false;
  52. continue;
  53. }
  54. garapraph[index].Range.Select();
  55. app.Selection.Copy();
  56. app.Documents[1].Activate();
  57. app.Selection.Paste();
  58. insert = true;
  59. }
  60. if (insert)
  61. {
  62. object file = Rename(fileName, fileIndex++);
  63. newDoc.SaveAs2(file);
  64. }
  65. newDoc.Close();
  66. insert = false;
  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.Undo();
  77. doc.Close();
  78. }
  79. if (app != null)
  80. {
  81. app.Quit();
  82. }
  83. }
  84. return "";
  85. }
  86. // 判断文件后缀名是否是.doc 或者.docx
  87. public bool ValidFileName(string fileName)
  88. {
  89. if (fileName == null || fileName.Length < 1)
  90. {
  91. return false;
  92. }
  93. if (!fileName.EndsWith(".doc") && !fileName.EndsWith(".docx"))
  94. {
  95. return false;
  96. }
  97. return true;
  98. }
  99. // 用设定的目录当做子word的目录,默认为选择文件的目录
  100. public string Rename(string filename, int index)
  101. {
  102. var filePathIndex = filename.LastIndexOf('\\');
  103. string name = "";
  104. string tempPath = "";
  105. if (-1 != filePathIndex)
  106. {
  107. name = filename.Substring(filePathIndex);
  108. if (name.Length > 0)
  109. {
  110. name = name.Substring(1);
  111. }
  112. tempPath = filename.Substring(0, filePathIndex);
  113. }
  114. if ("" == path)
  115. {
  116. path = tempPath;
  117. }
  118. string newFilename = name;
  119. newFilename = path + "\\" + name;
  120. int lastIndex = newFilename.LastIndexOf('.');
  121. string newName = "";
  122. if (-1 != lastIndex)
  123. {
  124. newName = string.Format("{0}{1}{2}", newFilename.Substring(0, lastIndex), index, newFilename.Substring(lastIndex));
  125. }
  126. else
  127. {
  128. newName = string.Format("{0}{1}", newFilename, index);
  129. }
  130. return newName;
  131. }
  132. }
  133. }