Browse Source

基本完成,有bug

tangs 8 years ago
parent
commit
a8e17a1198
3 changed files with 38 additions and 32 deletions
  1. 2 0
      .gitignore
  2. BIN
      .vs/WindowsFormsApp1/v15/.suo
  3. 36 32
      WindowsFormsApp1/WordAnalyze.cs

+ 2 - 0
.gitignore

@@ -0,0 +1,2 @@
+WindowsFormsApp1/bin/*
+WindowsFormsApp1/obj/*

BIN
.vs/WindowsFormsApp1/v15/.suo


+ 36 - 32
WindowsFormsApp1/WordAnalyze.cs

@@ -11,6 +11,8 @@ namespace WordAnalyze
 
         }
 
+        public static string splitChar = "---";
+
         // 将word根据指定的值切割成多个word
         public string AnalyzeAndCut()
         {
@@ -27,51 +29,53 @@ namespace WordAnalyze
 
             object fn = fileName;
 
-            Word.ApplicationClass app = new Word.ApplicationClass();
-            Word.Document doc = null;
+            Word.ApplicationClass app = new Word.ApplicationClass(); // 打开word应用,只会打开一个,即单例模式
+            Word.Document doc = null; // 源word文件对象
             try
             {
                 doc = app.Documents.Open(ref fn);
 
-                var oDoc = app.Documents.Add();
-                //Insert a paragraph at the beginning of the document.
-                var paragraph1 = oDoc.Content.Paragraphs.Add();
-
-                Word.Paragraphs garapraph = doc.Paragraphs;
+                Word.Paragraphs garapraph = doc.Paragraphs; // 源文件的内容
+                bool insert = false; //判断是否有插入数据到新文档中,如果有则保存新word,否则直接关掉新word
+                int fileIndex = 1; // 切割后word的文件序号,例子: test1.doc, test2.doc
 
-                for (int i = 1; i < garapraph.Count; i++)
+                Word.Document newDoc = null;
+                newDoc = app.Documents.Add();
+                // 遍历word,通过 --- 三个横线分割
+                for (int index = 1; index < garapraph.Count; index ++)
                 {
-                    Logger.D("-- {0}", garapraph[i].Range.Text.ToString());
-                    garapraph[i].Range.Select();
-
+                    string text = garapraph[index].Range.Text.ToString();
+                    Logger.D("document parapraph with index({0}) get message ({1})", index, garapraph[index].Range.Text.ToString());
+                    text = text.Trim();
+                    if (splitChar == text)
+                    {
+                        if (insert)
+                        {
+                            object file = Rename(fileName, fileIndex++);
+                            newDoc.SaveAs2(file);
+                        }
+                        newDoc.Close();
+                        newDoc = app.Documents.Add();
+                        insert = false;
+                        continue;
+                    }
+
+                    garapraph[index].Range.Select();
                     app.Selection.Copy();
-                    app.Selection.Paste();
-
-                    //var tempDoc = app.Documents.Add(oDoc);
-                    //tempDoc.Content.Paragraphs.Add();
-
                     app.Documents[1].Activate();
                     app.Selection.Paste();
-                    //tempDoc.SaveAs2(Rename(fileName, i));
-
-                    //tempDoc.Close();
+                    insert = true;
+                }
 
-                    Logger.D("selection --- {0}", app.Selection.ToString());
+                if (insert)
+                {
+                    object file = Rename(fileName, fileIndex++);
+                    newDoc.SaveAs2(file);
                 }
+                newDoc.Close();
+                insert = false;
 
                 Logger.D("documents count is {0}", app.Documents.Count);
-
-                doc.Save();
-                doc.SaveAs2(@"E:\test\test2222.doc");
-
-                var file = Rename(fileName, 1000);
-                object objectFilename = file;
-
-                //paragraph1.Range.Text = "Heading 1";
-                //paragraph1.Range.Font.Bold = 1;
-                //paragraph1.Format.SpaceAfter = 24;    //24 pt spacing after paragraph.
-
-                oDoc.SaveAs2(objectFilename);
             }
             catch (System.Exception e)
             {