WordAnalyze.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. // 将word根据指定的值切割成多个word
  12. public string AnalyzeAndCut()
  13. {
  14. return "";
  15. }
  16. public string AnalyzeFile(string fileName)
  17. {
  18. if (!ValidFileName(fileName))
  19. {
  20. Logger.D("AnalyzeFile with invalid file name {0}", fileName);
  21. return "无效的文件名";
  22. }
  23. object fn = fileName;
  24. Word.ApplicationClass app = new Word.ApplicationClass();
  25. Word.Document doc = null;
  26. try
  27. {
  28. doc = app.Documents.Open(ref fn);
  29. var oDoc = app.Documents.Add();
  30. //Insert a paragraph at the beginning of the document.
  31. var paragraph1 = oDoc.Content.Paragraphs.Add();
  32. Word.Paragraphs garapraph = doc.Paragraphs;
  33. for (int i = 1; i < garapraph.Count; i++)
  34. {
  35. Logger.D("-- {0}", garapraph[i].Range.Text.ToString());
  36. garapraph[i].Range.Select();
  37. app.Selection.Copy();
  38. app.Selection.Paste();
  39. //var tempDoc = app.Documents.Add(oDoc);
  40. //tempDoc.Content.Paragraphs.Add();
  41. app.Documents[1].Activate();
  42. app.Selection.Paste();
  43. //tempDoc.SaveAs2(Rename(fileName, i));
  44. //tempDoc.Close();
  45. Logger.D("selection --- {0}", app.Selection.ToString());
  46. }
  47. Logger.D("documents count is {0}", app.Documents.Count);
  48. doc.Save();
  49. doc.SaveAs2(@"E:\test\test2222.doc");
  50. var file = Rename(fileName, 1000);
  51. object objectFilename = file;
  52. //paragraph1.Range.Text = "Heading 1";
  53. //paragraph1.Range.Font.Bold = 1;
  54. //paragraph1.Format.SpaceAfter = 24; //24 pt spacing after paragraph.
  55. oDoc.SaveAs2(objectFilename);
  56. }
  57. catch (System.Exception e)
  58. {
  59. Logger.E("analyze with file name({0}) error ->({1})", fileName, e.Message.ToString());
  60. return e.Message.ToString();
  61. }
  62. finally
  63. {
  64. if (doc != null) {
  65. doc.Close();
  66. }
  67. if (app != null)
  68. {
  69. app.Quit();
  70. }
  71. }
  72. return "";
  73. }
  74. // 判断文件后缀名是否是.doc 或者.docx
  75. public bool ValidFileName(string fileName)
  76. {
  77. if (fileName == null || fileName.Length < 1)
  78. {
  79. return false;
  80. }
  81. if (!fileName.EndsWith(".doc") && !fileName.EndsWith(".docx"))
  82. {
  83. return false;
  84. }
  85. return true;
  86. }
  87. public string WriteFile(string filename)
  88. {
  89. return "";
  90. }
  91. public string Rename(string filename, int index)
  92. {
  93. int lastIndex = filename.LastIndexOf('.');
  94. string newFilename = string.Format("{0}{1}{2}", filename.Substring(0, lastIndex), index, filename.Substring(lastIndex));
  95. return newFilename;
  96. }
  97. }
  98. }