Form1.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. using NPOI.HSSF.UserModel;
  2. using NPOI.HSSF.Util;
  3. using NPOI.SS.UserModel;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Data;
  8. using System.Drawing;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows.Forms;
  14. using Winsoft.GOV.Framework.Model;
  15. using Winsoft.GOV.Framework.Provider;
  16. using static Winsoft.GOV.ImportDataForm.ExcelHelper;
  17. namespace Winsoft.GOV.ImportDataForm
  18. {
  19. public partial class Form1 : Form
  20. {
  21. public Form1()
  22. {
  23. InitializeComponent();
  24. }
  25. private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
  26. {
  27. }
  28. private void button2_Click(object sender, EventArgs e)
  29. {
  30. if (openFileDialog1.ShowDialog() == DialogResult.OK)
  31. {
  32. textBox1.Text = openFileDialog1.FileName;
  33. comboBox1.Text = Path.GetFileNameWithoutExtension(openFileDialog1.FileName);
  34. dataGridView1.DataSource = ExcelHelper.ExcelToDataSet(textBox1.Text).Tables[0];
  35. dataGridView1.Show();
  36. }
  37. }
  38. private void Form1_Load(object sender, EventArgs e)
  39. {
  40. IEnumerable<Branch> tmp = BranchProvider.Instance.FindAll();
  41. PowerMatterTypeProvider.Instance.CreateTable();
  42. PowerMettersBaseProvider.Instance.CreateTable();
  43. OnceRunPowerMattersProvider.Instance.CreateTable();
  44. foreach(Branch b in tmp)
  45. {
  46. comboBox1.Items.Add(b);
  47. }
  48. }
  49. private void button1_Click(object sender, EventArgs e)
  50. {
  51. bool find = false;
  52. foreach (Branch b in comboBox1.Items)
  53. {
  54. if (comboBox1.Text == b.ShortName)
  55. {
  56. comboBox1.SelectedItem = b;
  57. find = true;
  58. break;
  59. }
  60. }
  61. if (!find)
  62. {
  63. if (MessageBox.Show("没有找到部门名为“" + comboBox1.Text + "”的部门,您是否向系统添加该部门?", "添加部门", MessageBoxButtons.OKCancel) == DialogResult.OK)
  64. {
  65. Branch b = new Branch()
  66. {
  67. GUID = comboBox1.Text,
  68. ShortName = comboBox1.Text
  69. };
  70. comboBox1.Items.Add(b);
  71. comboBox1.SelectedItem = b;
  72. }
  73. else
  74. return;
  75. }
  76. IList<PowerMattersBase> list = ExcelHelper.ExcelToList<PowerMattersBase>(textBox1.Text, GetApplyType);
  77. IList<PowerMattersBase> failslist = new List<PowerMattersBase>();
  78. PowerMatterTypeProvider.Instance.Insert(list, ref failslist);
  79. if (failslist.Count == 0)
  80. {
  81. MessageBox.Show("导入成功");
  82. }
  83. else
  84. {
  85. saveFileDialog1.FileName = String.Format("{0}(失败记录).xls", comboBox1.Text);
  86. if (saveFileDialog1.ShowDialog() == DialogResult.OK)
  87. {
  88. ExcelHelper.WriteExcel<PowerMattersBase>(failslist, saveFileDialog1.FileName, WriteHeard, WriteRow);
  89. }
  90. MessageBox.Show("完成");
  91. }
  92. }
  93. private void WriteRow(IRow row, PowerMattersBase obj)
  94. {
  95. row.CreateCell(0, CellType.String).SetCellValue(obj.QL_DEP);
  96. row.CreateCell(1, CellType.String).SetCellValue(obj.OUGUID);
  97. row.CreateCell(2, CellType.String).SetCellValue(obj.QL_NAME);
  98. row.CreateCell(3, CellType.Numeric).SetCellValue(obj.ApplyCode);
  99. row.CreateCell(4, CellType.String).SetCellValue((obj.ApplyCode > -1 && obj.ApplyCode < ApplyType.Names.Length)? ApplyType.Names[obj.ApplyCode] : "其他");
  100. }
  101. private void WriteHeard(IRow row)
  102. {
  103. row.CreateCell(0, CellType.String).SetCellValue("部门名称");
  104. row.CreateCell(1, CellType.String).SetCellValue("部门编号");
  105. row.CreateCell(2, CellType.String).SetCellValue("权力名称");
  106. row.CreateCell(3, CellType.String).SetCellValue("分类编码");
  107. row.CreateCell(4, CellType.String).SetCellValue("分类名称");
  108. }
  109. private PowerMattersBase GetApplyType(IRow row)
  110. {
  111. if (row.Cells.Count != 7)
  112. return null;
  113. PowerMattersBase a = new PowerMattersBase();
  114. a.QL_DEP = (comboBox1.SelectedItem as Branch).ShortName;
  115. a.OUGUID = (comboBox1.SelectedItem as Branch).GUID;
  116. a.QL_NAME = row.GetCell(1).StringCellValue.Trim();
  117. int i = 0;
  118. for (int j = 2; j <= row.Cells.Count - 1; j++)
  119. {
  120. ICell cell = row.GetCell(j);
  121. if (cell == null || cell.CellType == CellType.Blank || (cell.CellType == CellType.String && String.IsNullOrWhiteSpace(cell.StringCellValue))
  122. || (cell.CellType == CellType.Numeric && cell.NumericCellValue != 1))
  123. {
  124. i++;
  125. continue;
  126. }
  127. else
  128. break;
  129. }
  130. a.ApplyCode = i > ApplyType.Names.Length - 1 ? -1 : i;
  131. return a;
  132. }
  133. private void button3_Click(object sender, EventArgs e)
  134. {
  135. if (openFileDialog1.ShowDialog() == DialogResult.OK)
  136. {
  137. textBox2.Text = openFileDialog1.FileName;
  138. }
  139. }
  140. private void button4_Click(object sender, EventArgs e)
  141. {
  142. label1.Text = "正在载入数据...";
  143. IList<PowerMattersBase> list = ExcelHelper.ExcelToList<PowerMattersBase>(textBox2.Text, ReadPowerMatters);
  144. label1.Text = "正在更新部门信息...";
  145. foreach (PowerMattersBase p in list)
  146. {
  147. Branch b = BranchProvider.Instance.Find(p.OUGUID);
  148. if (b == null)
  149. {
  150. b = new Branch()
  151. {
  152. GUID = p.OUGUID,
  153. ShortName = p.QL_DEP
  154. };
  155. //BranchProvider.Instance.Insert(b);
  156. }
  157. else
  158. {
  159. p.QL_DEP = b.ShortName;
  160. }
  161. }
  162. label1.Text = "正在清除老权力清单数据...";
  163. //PowerMettersBaseProvider.Instance.DeleteAll();
  164. label1.Text = "正在插入新权力清单数据...";
  165. //PowerMettersBaseProvider.Instance.Insert(list);
  166. label1.Text = "正在清理老权力清单的详细数据...";
  167. PowerMettersDetailProvider.Instance.DeleteByPowerMattersBase();
  168. label1.Text = "正在插入新权力清单的详细数据...";
  169. foreach (PowerMattersBase p in list)
  170. {
  171. if (!PowerMettersDetailProvider.Instance.IsExist(p.QL_INNER_CODE))
  172. {
  173. PowerMattersDetail pd = QLSXProvider.Instance.FindByID(p.QL_INNER_CODE);
  174. if (pd != null)
  175. PowerMettersDetailProvider.Instance.Insert(pd);
  176. }
  177. }
  178. label1.Text = "完成";
  179. }
  180. private PowerMattersBase ReadPowerMatters(IRow row)
  181. {
  182. if (row.Cells.Count < 6)
  183. return null;
  184. PowerMattersBase a = new PowerMattersBase();
  185. a.OUGUID = row.GetCell(1).StringCellValue;
  186. a.QL_INNER_CODE = row.GetCell(3).StringCellValue;
  187. a.QL_DEP = row.GetCell(4).StringCellValue;
  188. a.QL_NAME = row.GetCell(5).StringCellValue;
  189. return a;
  190. }
  191. private void button5_Click(object sender, EventArgs e)
  192. {
  193. if (openFileDialog1.ShowDialog() == DialogResult.OK)
  194. {
  195. textBox3.Text = openFileDialog1.FileName;
  196. }
  197. }
  198. private void button6_Click(object sender, EventArgs e)
  199. {
  200. label2.Text = "正在载入数据...";
  201. IList<PowerMattersBase> list = ExcelHelper.ExcelToList<PowerMattersBase>(textBox3.Text, ReadPowerMatters);
  202. label2.Text = "正在更新部门信息...";
  203. foreach (PowerMattersBase p in list)
  204. {
  205. Branch b = BranchProvider.Instance.Find(p.OUGUID);
  206. if (b == null)
  207. {
  208. b = new Branch()
  209. {
  210. GUID = p.OUGUID,
  211. ShortName = p.QL_DEP
  212. };
  213. BranchProvider.Instance.Insert(b);
  214. }
  215. else
  216. {
  217. p.QL_DEP = b.ShortName;
  218. }
  219. }
  220. label2.Text = "正在清除老的最多跑一次数据...";
  221. OnceRunPowerMattersProvider.Instance.DeleteAll();
  222. label2.Text = "正在插入新的最多跑一次数据...";
  223. OnceRunPowerMattersProvider.Instance.Insert(list);
  224. label2.Text = "完成";
  225. }
  226. private void button7_Click(object sender, EventArgs e)
  227. {
  228. if(folderBrowserDialog1.ShowDialog() == DialogResult.OK)
  229. {
  230. IEnumerable<Branch> bs = BranchProvider.Instance.FindOnceRunBranch();
  231. foreach(Branch b in bs)
  232. {
  233. label3.Text = "正在导出“" + b.ShortName + ".xls”";
  234. IEnumerable<PowerMattersBase> ps = OnceRunPowerMattersProvider.Instance.FindOnceRunPowerMattersByBranchGUID(b.GUID);
  235. foreach(PowerMattersBase p in ps)
  236. {
  237. p.QL_DEP = b.ShortName;
  238. p.OUGUID = b.GUID;
  239. }
  240. ExcelHelper.WriteExcel<PowerMattersBase>(ps.ToList<PowerMattersBase>(), folderBrowserDialog1.SelectedPath + "\\" + b.ShortName + ".xls", ExportWriteHeard, ExportWriteRow);
  241. }
  242. label3.Text = "完成";
  243. }
  244. }
  245. private void ExportWriteRow(IRow row, PowerMattersBase obj)
  246. {
  247. row.CreateCell(0, CellType.String).SetCellValue(obj.OUGUID);
  248. row.CreateCell(1, CellType.String).SetCellValue(obj.QL_INNER_CODE);
  249. row.CreateCell(2, CellType.String).SetCellValue(obj.QL_DEP);
  250. row.CreateCell(3, CellType.String).SetCellValue(obj.QL_NAME);
  251. row.CreateCell(4, CellType.String).SetCellValue("");
  252. row.CreateCell(5, CellType.String).SetCellValue("");
  253. row.CreateCell(6, CellType.String).SetCellValue("");
  254. row.CreateCell(7, CellType.String).SetCellValue("");
  255. row.CreateCell(8, CellType.String).SetCellValue("");
  256. row.CreateCell(9, CellType.String).SetCellValue("");
  257. PowerMattersBase p = PowerMatterTypeProvider.Instance.FindById(obj.QL_INNER_CODE);
  258. if (p != null)
  259. row.CreateCell(5 + p.ApplyCode).SetCellValue("1");
  260. else
  261. row.CreateCell(4).SetCellValue("1");
  262. int i = 0;
  263. foreach (ICell c in row.Cells)
  264. {
  265. ICellStyle style = row.Sheet.Workbook.CreateCellStyle();
  266. style.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  267. style.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  268. style.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  269. style.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  270. style.GetFont(row.Sheet.Workbook).IsBold = false;
  271. if (i == 3)
  272. style.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Left;
  273. else
  274. style.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
  275. i++;
  276. if (p == null)
  277. style.FillBackgroundColor = 51;
  278. c.CellStyle = style;
  279. }
  280. }
  281. private void ExportWriteHeard(IRow row)
  282. {
  283. row.Sheet.SetColumnWidth(0, 15 * 256);
  284. row.Sheet.SetColumnWidth(1, 40 * 256);
  285. row.Sheet.SetColumnWidth(2, 15 * 256);
  286. row.Sheet.SetColumnWidth(3, 50 * 256);
  287. row.Sheet.SetColumnWidth(4, 15 * 256);
  288. row.Sheet.SetColumnWidth(5, 15 * 256);
  289. row.Sheet.SetColumnWidth(6, 15 * 256);
  290. row.Sheet.SetColumnWidth(7, 15 * 256);
  291. row.Sheet.SetColumnWidth(8, 15 * 256);
  292. row.Sheet.SetColumnWidth(9, 15 * 256);
  293. row.CreateCell(0, CellType.String).SetCellValue("部门编号");
  294. row.CreateCell(1, CellType.String).SetCellValue("权力编码");
  295. row.CreateCell(2, CellType.String).SetCellValue("部门名称");
  296. row.CreateCell(3, CellType.String).SetCellValue("权力名称");
  297. row.CreateCell(4, CellType.String).SetCellValue("其他");
  298. row.CreateCell(5, CellType.String).SetCellValue("即办");
  299. row.CreateCell(6, CellType.String).SetCellValue("全程网上申办,在线取件");
  300. row.CreateCell(7, CellType.String).SetCellValue("全程网上申办,快递送达");
  301. row.CreateCell(8, CellType.String).SetCellValue("网上申报,预审,窗口核验取件");
  302. row.CreateCell(9, CellType.String).SetCellValue("窗口申报,快递送达");
  303. foreach(ICell c in row.Cells)
  304. {
  305. ICellStyle style = row.Sheet.Workbook.CreateCellStyle();
  306. style.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  307. style.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  308. style.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  309. style.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  310. style.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
  311. style.VerticalAlignment = VerticalAlignment.Center;
  312. style.WrapText = true;
  313. style.GetFont(row.Sheet.Workbook).IsBold = true;
  314. c.CellStyle = style;
  315. }
  316. }
  317. }
  318. }