WordAnalyze.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. 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. }
  67. catch (System.Exception e)
  68. {
  69. Logger.E("analyze with file name({0}) error ->({1})", fileName, e.Message.ToString());
  70. return e.Message.ToString();
  71. }
  72. finally
  73. {
  74. if (doc != null) {
  75. doc.Close();
  76. }
  77. if (app != null)
  78. {
  79. app.Quit();
  80. }
  81. }
  82. return "";
  83. }
  84. // 判断文件后缀名是否是.doc 或者.docx
  85. public bool ValidFileName(string fileName)
  86. {
  87. if (fileName == null || fileName.Length < 1)
  88. {
  89. return false;
  90. }
  91. if (!fileName.EndsWith(".doc") && !fileName.EndsWith(".docx"))
  92. {
  93. return false;
  94. }
  95. return true;
  96. }
  97. // 用设定的目录当做子word的目录,默认为选择文件的目录
  98. public string Rename(string filename, int index)
  99. {
  100. var filePathIndex = filename.LastIndexOf('\\');
  101. string name = "";
  102. string tempPath = "";
  103. if (-1 != filePathIndex)
  104. {
  105. name = filename.Substring(filePathIndex);
  106. if (name.Length > 0)
  107. {
  108. name = name.Substring(1);
  109. }
  110. tempPath = filename.Substring(0, filePathIndex);
  111. }
  112. if ("" == path)
  113. {
  114. path = tempPath;
  115. }
  116. string newFilename = name;
  117. newFilename = path + "\\" + name;
  118. int lastIndex = newFilename.LastIndexOf('.');
  119. string newName = "";
  120. if (-1 != lastIndex)
  121. {
  122. newName = string.Format("{0}{1}{2}", newFilename.Substring(0, lastIndex), index, newFilename.Substring(lastIndex));
  123. }
  124. else
  125. {
  126. newName = string.Format("{0}{1}", newFilename, index);
  127. }
  128. return newName;
  129. }
  130. }
  131. }