Form1.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using WordAnalyze;
  11. using Logger = Log.Log;
  12. namespace WindowsFormsApp1
  13. {
  14. public partial class Form1 : Form
  15. {
  16. private WordAnalyze.Analyze Analyzer = null;
  17. public Form1()
  18. {
  19. InitializeComponent();
  20. // 初始化处理文档对象
  21. Analyzer = new WordAnalyze.Analyze();
  22. Logger.Init("log.log");
  23. init_listView();
  24. init_textBox();
  25. }
  26. // 初始化显示的列表
  27. private void init_listView()
  28. {
  29. listView1.View = View.Details;
  30. Console.WriteLine(listView1.Columns.Count);
  31. // 第一列,文件名
  32. if (0 == listView1.Columns.Count)
  33. {
  34. listView1.Columns.Add("文件名");
  35. Console.WriteLine(listView1.Columns.Count);
  36. }
  37. Console.WriteLine(listView1.Columns.Count);
  38. listView1.Columns[0].Text = "文件名";
  39. listView1.Columns[0].Width = 200;
  40. listView1.Columns[0].TextAlign = HorizontalAlignment.Left;
  41. // 第二列,状态
  42. if (1 == listView1.Columns.Count)
  43. {
  44. listView1.Columns.Add("状态");
  45. }
  46. listView1.Columns[1].Text = "状态";
  47. listView1.Columns[1].Width = 50;
  48. listView1.Columns[1].TextAlign = HorizontalAlignment.Left;
  49. // 第三列,错误信息,成功则忽略
  50. if (2 == listView1.Columns.Count)
  51. {
  52. listView1.Columns.Add("信息");
  53. }
  54. listView1.Columns[2].Text = "信息";
  55. listView1.Columns[2].Width = 300;
  56. listView1.Columns[2].TextAlign = HorizontalAlignment.Left;
  57. }
  58. // 初始化存储文件夹的显示框
  59. private void init_textBox()
  60. {
  61. textBox1.Enabled = false;
  62. }
  63. private bool start_stop = false;
  64. // 开始处理word文档
  65. private void button1_Click(object sender, EventArgs e)
  66. {
  67. if (fileNames == null || fileNames.Length < 1)
  68. {
  69. Logger.D("event start, but no file to analyse");
  70. return;
  71. }
  72. button1.Enabled = false;
  73. listview_draw(fileNames);
  74. for (int i = 0; i < fileNames.Length; i ++)
  75. {
  76. execStatus(i);
  77. Logger.D("startanalyze with filename({0})", fileNames[i]);
  78. if (listView1.Items.Count < i + 1)
  79. {
  80. Logger.D("analyze with filename({0}) success but item with index({1}) is unexpected", fileNames[i], i);
  81. continue;
  82. }
  83. if (listView1.Items[i].SubItems.Count != 3)
  84. {
  85. 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);
  86. continue;
  87. }
  88. var result = Analyzer.AnalyzeFile(fileNames[i]);
  89. string status = "成功";
  90. if (result != "")
  91. {
  92. status = "失败";
  93. }
  94. listView1.Items[i].SubItems[1].Text = status;
  95. listView1.Items[i].SubItems[2].Text = result;
  96. Logger.D("analyze with filename({0}) success with result({1})", fileNames[i], result);
  97. }
  98. button1.Enabled = true;
  99. }
  100. private void folderBrowserDialog1_HelpRequest(object sender, EventArgs e)
  101. {
  102. }
  103. private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
  104. {
  105. }
  106. private void button2_Click(object sender, EventArgs e)
  107. {
  108. // 选择文件
  109. OpenFileDialog openFileDialog = new OpenFileDialog();
  110. openFileDialog.Filter = "doc files (*.doc)|*.doc| docx files (*.docx)|*.docx";
  111. openFileDialog.Multiselect = true;
  112. openFileDialog.Title = "选择doc或者docx文件";
  113. if (openFileDialog.ShowDialog() == DialogResult.OK)
  114. {
  115. fileNames = openFileDialog.FileNames;
  116. // 先清空列表,再渲染列表
  117. listview_draw(fileNames);
  118. }
  119. }
  120. private void Form1_Load(object sender, EventArgs e)
  121. {
  122. }
  123. private void listview_draw(string[] fileNames)
  124. {
  125. listView1.Items.Clear();
  126. for (int i = 0; i < fileNames.Length; i++)
  127. {
  128. ListViewItem item = new ListViewItem();
  129. item.Text = fileNames[i];
  130. item.SubItems.Add("等待中");
  131. item.SubItems.Add("");
  132. listView1.Items.Add(item);
  133. }
  134. }
  135. private void execStatus(int index)
  136. {
  137. var item = listView1.Items[index];
  138. item.SubItems[1].Text = "执行中";
  139. }
  140. private void listView1_SelectedIndexChanged(object sender, EventArgs e)
  141. {
  142. }
  143. private void button3_Click(object sender, EventArgs e)
  144. {
  145. FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
  146. if(folderBrowserDialog.ShowDialog() == DialogResult.OK)
  147. {
  148. AimFolder = folderBrowserDialog.SelectedPath;
  149. textBox1.Text = AimFolder;
  150. Analyzer.SetPath(AimFolder);
  151. }
  152. }
  153. private void textBox1_TextChanged(object sender, EventArgs e)
  154. {
  155. }
  156. }
  157. }