Jelajahi Sumber

基本完成word的切割程序

tangs 7 tahun lalu
induk
melakukan
38b9591e92

TEMPAT SAMPAH
.vs/WindowsFormsApp1/v15/.suo


+ 1 - 1
WindowsFormsApp1/Form1.Designer.cs

@@ -74,7 +74,7 @@
             // 
             this.button3.Location = new System.Drawing.Point(35, 205);
             this.button3.Name = "button3";
-            this.button3.Size = new System.Drawing.Size(96, 23);
+            this.button3.Size = new System.Drawing.Size(121, 23);
             this.button3.TabIndex = 3;
             this.button3.Text = "选择存储文件夹";
             this.button3.UseVisualStyleBackColor = true;

+ 19 - 13
WindowsFormsApp1/Form1.cs

@@ -22,6 +22,7 @@ namespace WindowsFormsApp1
             // 初始化处理文档对象
             Analyzer = new WordAnalyze.Analyze();
 
+            Logger.Init("log.log");
             init_listView();
             init_textBox();
 
@@ -69,21 +70,23 @@ namespace WindowsFormsApp1
             textBox1.Enabled = false;
         }
 
+        private bool start_stop = false;
         // 开始处理word文档
         private void button1_Click(object sender, EventArgs e)
         {
             if (fileNames == null || fileNames.Length < 1)
             {
-
                 Logger.D("event start, but no file to analyse");
                 return;
             }
 
+            button1.Enabled = false;
+            listview_draw(fileNames);
+
             for (int i = 0; i < fileNames.Length; i ++)
-            {
-                string.Format("{0}", "djlf");
-                Logger.D("start analyze with filename({0})", fileNames[i]);
-                var result = Analyzer.AnalyzeFile(fileNames[i]);
+            {               
+                execStatus(i);
+                Logger.D("startanalyze with filename({0})", fileNames[i]);
                 if (listView1.Items.Count < i + 1)
                 {
                     Logger.D("analyze with filename({0}) success but item with index({1}) is unexpected", fileNames[i], i);
@@ -94,17 +97,19 @@ namespace WindowsFormsApp1
                     Logger.D("analyze with filename({0}) success but item with index({1}) contain unexpected subitem length({2})", fileNames[i], i, listView1.Items[i].SubItems.Count);
                     continue;
                 }
+                var result = Analyzer.AnalyzeFile(fileNames[i]);
+
                 string status = "成功";
                 if (result != "")
                 {
                     status = "失败";
                 }
 
-                Logger.D("{0}, {1}", listView1.Items[i].SubItems[0].Text, listView1.Items[i].SubItems[1].Text);
                 listView1.Items[i].SubItems[1].Text = status;
                 listView1.Items[i].SubItems[2].Text = result;
                 Logger.D("analyze with filename({0}) success with result({1})", fileNames[i], result);
             }
+            button1.Enabled = true;
         }
 
         private void folderBrowserDialog1_HelpRequest(object sender, EventArgs e)
@@ -120,7 +125,6 @@ namespace WindowsFormsApp1
         private void button2_Click(object sender, EventArgs e)
         {
             // 选择文件
-            Console.WriteLine("button click happened");
             OpenFileDialog openFileDialog = new OpenFileDialog();
             openFileDialog.Filter = "doc files (*.doc)|*.doc| docx files (*.docx)|*.docx";
             openFileDialog.Multiselect = true;
@@ -128,13 +132,7 @@ namespace WindowsFormsApp1
             if (openFileDialog.ShowDialog() == DialogResult.OK)
             {
                 fileNames = openFileDialog.FileNames;
-                Console.WriteLine("select files ");
-                for (int i = 0; i < fileNames.Length; i++)
-                {
-                    Console.WriteLine(fileNames[i]);
-                }
                 // 先清空列表,再渲染列表
-                listView1.Items.Clear();
                 listview_draw(fileNames);
             }
         }
@@ -146,6 +144,7 @@ namespace WindowsFormsApp1
 
         private void listview_draw(string[] fileNames)
         {
+            listView1.Items.Clear();
             for (int i = 0; i < fileNames.Length; i++)
             {
                 ListViewItem item = new ListViewItem();
@@ -156,6 +155,12 @@ namespace WindowsFormsApp1
             }
         }
 
+        private void execStatus(int index)
+        {
+            var item = listView1.Items[index];
+            item.SubItems[1].Text = "执行中";
+        }
+
 
         private void listView1_SelectedIndexChanged(object sender, EventArgs e)
         {
@@ -169,6 +174,7 @@ namespace WindowsFormsApp1
             {
                 AimFolder = folderBrowserDialog.SelectedPath;
                 textBox1.Text = AimFolder;
+                Analyzer.SetPath(AimFolder);
             }
         }
 

+ 6 - 0
WindowsFormsApp1/Log.cs

@@ -5,6 +5,12 @@ namespace Log
 {
     public static class Log
     {
+        public static void Init(string logPath)
+        {
+            Debug.Listeners.Clear();
+            Debug.Listeners.Add(new TextWriterTraceListener(logPath));
+            Debug.AutoFlush = true;
+        }
         public static void D(string message)
         {
             var sf = new StackFrame(1, true);

+ 37 - 14
WindowsFormsApp1/WordAnalyze.cs

@@ -12,11 +12,11 @@ namespace WordAnalyze
         }
 
         public static string splitChar = "---";
+        public string path = "";
 
-        // 将word根据指定的值切割成多个word
-        public string AnalyzeAndCut()
+        public void SetPath(string path)
         {
-            return "";
+            this.path = path;
         }
 
         public string AnalyzeFile(string fileName)
@@ -45,7 +45,7 @@ namespace WordAnalyze
                 for (int index = 1; index < garapraph.Count; index ++)
                 {
                     string text = garapraph[index].Range.Text.ToString();
-                    Logger.D("document parapraph with index({0}) get message ({1})", index, 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)
                     {
@@ -74,8 +74,7 @@ namespace WordAnalyze
                 }
                 newDoc.Close();
                 insert = false;
-
-                Logger.D("documents count is {0}", app.Documents.Count);
+                
             }
             catch (System.Exception e)
             {
@@ -111,16 +110,40 @@ namespace WordAnalyze
             return true;
         }
 
-        public string WriteFile(string filename)
-        {
-            return "";
-        }
-
+        // 用设定的目录当做子word的目录,默认为选择文件的目录
         public string Rename(string filename, int index)
         {
-            int lastIndex = filename.LastIndexOf('.');
-            string newFilename = string.Format("{0}{1}{2}", filename.Substring(0, lastIndex), index, filename.Substring(lastIndex));
-            return newFilename;
+            var filePathIndex = filename.LastIndexOf('\\');
+            string name = "";
+            string tempPath = "";
+            if (-1 != filePathIndex)
+            {
+                name = filename.Substring(filePathIndex);
+                if (name.Length > 0)
+                {
+                    name = name.Substring(1);
+                }
+                tempPath = filename.Substring(0, filePathIndex);
+            }
+
+            if ("" == path)
+            {
+                path = tempPath;
+            }
+
+            string newFilename = name;
+            newFilename = path + "\\" + name;
+            int lastIndex = newFilename.LastIndexOf('.');
+            string newName = "";
+            if (-1 != lastIndex)
+            {
+                newName = string.Format("{0}{1}{2}", newFilename.Substring(0, lastIndex), index, newFilename.Substring(lastIndex));
+            }
+            else
+            {
+                newName = string.Format("{0}{1}", newFilename, index);
+            }
+            return newName;
         }
     }
 }