Form1.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. class CountyItem
  22. {
  23. public string Name { get; set; }
  24. public County CountyValue { get; set; }
  25. public override string ToString()
  26. {
  27. return Name;
  28. }
  29. }
  30. public Form1()
  31. {
  32. InitializeComponent();
  33. }
  34. private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
  35. {
  36. }
  37. private void button2_Click(object sender, EventArgs e)
  38. {
  39. if (openFileDialog1.ShowDialog() == DialogResult.OK)
  40. {
  41. textBox1.Text = openFileDialog1.FileName;
  42. comboBox1.Text = Path.GetFileNameWithoutExtension(openFileDialog1.FileName);
  43. dataGridView1.DataSource = ExcelHelper.ExcelToDataSet(textBox1.Text).Tables[0];
  44. dataGridView1.Show();
  45. }
  46. }
  47. private void Form1_Load(object sender, EventArgs e)
  48. {
  49. comboBox2.Items.Add(new CountyItem() {
  50. Name = "丽水",
  51. CountyValue = County.LS
  52. });
  53. comboBox2.Items.Add(new CountyItem()
  54. {
  55. Name = "莲都",
  56. CountyValue = County.LD
  57. });
  58. comboBox2.Items.Add(new CountyItem()
  59. {
  60. Name = "龙泉",
  61. CountyValue = County.LQ
  62. });
  63. comboBox2.Items.Add(new CountyItem()
  64. {
  65. Name = "青田",
  66. CountyValue = County.QT
  67. });
  68. comboBox2.Items.Add(new CountyItem()
  69. {
  70. Name = "云和",
  71. CountyValue = County.YH
  72. });
  73. comboBox2.Items.Add(new CountyItem()
  74. {
  75. Name = "缙云",
  76. CountyValue = County.JY
  77. });
  78. comboBox2.Items.Add(new CountyItem()
  79. {
  80. Name = "遂昌",
  81. CountyValue = County.SC
  82. });
  83. comboBox2.Items.Add(new CountyItem()
  84. {
  85. Name = "景宁",
  86. CountyValue = County.JN
  87. });
  88. comboBox2.Items.Add(new CountyItem()
  89. {
  90. Name = "庆元",
  91. CountyValue = County.QY
  92. });
  93. comboBox2.Items.Add(new CountyItem()
  94. {
  95. Name = "松阳",
  96. CountyValue = County.SY
  97. });
  98. comboBox2.Items.Add(new CountyItem()
  99. {
  100. Name = "开发区",
  101. CountyValue = County.KFQ
  102. });
  103. //IEnumerable<Branch> tmp = BranchProvider.Instance.FindAll();
  104. //PowerMatterTypeProvider.Instance.CreateTable();
  105. //PowerMettersBaseProvider.Instance.CreateTable();
  106. //OnceRunPowerMattersProvider.Instance.CreateTable();
  107. //foreach(Branch b in tmp)
  108. //{
  109. // comboBox1.Items.Add(b);
  110. //}
  111. }
  112. private void button1_Click(object sender, EventArgs e)
  113. {
  114. bool find = false;
  115. foreach (Branch b in comboBox1.Items)
  116. {
  117. if (comboBox1.Text == b.ShortName)
  118. {
  119. comboBox1.SelectedItem = b;
  120. find = true;
  121. break;
  122. }
  123. }
  124. if (!find)
  125. {
  126. if (MessageBox.Show("没有找到部门名为“" + comboBox1.Text + "”的部门,您是否向系统添加该部门?", "添加部门", MessageBoxButtons.OKCancel) == DialogResult.OK)
  127. {
  128. Branch b = new Branch()
  129. {
  130. GUID = comboBox1.Text,
  131. ShortName = comboBox1.Text
  132. };
  133. comboBox1.Items.Add(b);
  134. comboBox1.SelectedItem = b;
  135. }
  136. else
  137. return;
  138. }
  139. IList<PowerMattersBase> list = ExcelHelper.ExcelToList<PowerMattersBase>(textBox1.Text, GetApplyType);
  140. IList<PowerMattersBase> failslist = new List<PowerMattersBase>();
  141. ProvidersFactory.GetPowerMatterTypeProvider((comboBox2.SelectedItem as CountyItem).CountyValue).Insert(list, ref failslist);
  142. if (failslist.Count == 0)
  143. {
  144. MessageBox.Show("导入成功");
  145. }
  146. else
  147. {
  148. saveFileDialog1.FileName = String.Format("{0}(失败记录).xls", comboBox1.Text);
  149. if (saveFileDialog1.ShowDialog() == DialogResult.OK)
  150. {
  151. ExcelHelper.WriteExcel<PowerMattersBase>(failslist, saveFileDialog1.FileName, WriteHeard, WriteRow);
  152. }
  153. MessageBox.Show("完成");
  154. }
  155. }
  156. private void WriteRow(IRow row, PowerMattersBase obj)
  157. {
  158. row.CreateCell(0, CellType.String).SetCellValue(obj.QL_DEP);
  159. row.CreateCell(1, CellType.String).SetCellValue(obj.OUGUID);
  160. row.CreateCell(2, CellType.String).SetCellValue(obj.QL_NAME);
  161. row.CreateCell(3, CellType.Numeric).SetCellValue(obj.ApplyCode);
  162. row.CreateCell(4, CellType.String).SetCellValue((obj.ApplyCode > -1 && obj.ApplyCode < ApplyType.Names.Length)? ApplyType.Names[obj.ApplyCode] : "其他");
  163. }
  164. private void WriteHeard(IRow row)
  165. {
  166. row.CreateCell(0, CellType.String).SetCellValue("部门名称");
  167. row.CreateCell(1, CellType.String).SetCellValue("部门编号");
  168. row.CreateCell(2, CellType.String).SetCellValue("权力名称");
  169. row.CreateCell(3, CellType.String).SetCellValue("分类编码");
  170. row.CreateCell(4, CellType.String).SetCellValue("分类名称");
  171. }
  172. private PowerMattersBase GetApplyType(IRow row)
  173. {
  174. if (row.Cells.Count != 7)
  175. return null;
  176. PowerMattersBase a = new PowerMattersBase();
  177. a.QL_DEP = (comboBox1.SelectedItem as Branch).ShortName;
  178. a.OUGUID = (comboBox1.SelectedItem as Branch).GUID;
  179. a.QL_NAME = row.GetCell(1).StringCellValue.Trim();
  180. int i = 0;
  181. for (int j = 2; j <= row.Cells.Count - 1; j++)
  182. {
  183. ICell cell = row.GetCell(j);
  184. if (cell == null || cell.CellType == CellType.Blank || (cell.CellType == CellType.String && String.IsNullOrWhiteSpace(cell.StringCellValue))
  185. || (cell.CellType == CellType.Numeric && cell.NumericCellValue != 1))
  186. {
  187. i++;
  188. continue;
  189. }
  190. else
  191. break;
  192. }
  193. a.ApplyCode = i > ApplyType.Names.Length - 1 ? -1 : i;
  194. return a;
  195. }
  196. private void button3_Click(object sender, EventArgs e)
  197. {
  198. if (openFileDialog1.ShowDialog() == DialogResult.OK)
  199. {
  200. textBox2.Text = openFileDialog1.FileName;
  201. string countyName = Path.GetFileNameWithoutExtension(openFileDialog1.FileName);
  202. foreach(CountyItem n in comboBox2.Items)
  203. {
  204. if (countyName == n.Name)
  205. {
  206. comboBox2.SelectedItem = n;
  207. break;
  208. }
  209. }
  210. }
  211. }
  212. private void button4_Click(object sender, EventArgs e)
  213. {
  214. label1.Text = "正在载入数据...";
  215. IList<PowerMattersBase> list = ExcelHelper.ExcelToList<PowerMattersBase>(textBox2.Text, ReadPowerMatters);
  216. label1.Text = "正在更新部门信息...";
  217. foreach (PowerMattersBase p in list)
  218. {
  219. Branch b = ProvidersFactory.GetBranchProvider((comboBox2.SelectedItem as CountyItem).CountyValue).Find(p.OUGUID);
  220. if (b == null)
  221. {
  222. b = new Branch()
  223. {
  224. GUID = p.OUGUID,
  225. ShortName = p.QL_DEP
  226. };
  227. ProvidersFactory.GetBranchProvider((comboBox2.SelectedItem as CountyItem).CountyValue).Insert(b);
  228. }
  229. else
  230. {
  231. p.QL_DEP = b.ShortName;
  232. }
  233. }
  234. label1.Text = "正在清除老权力清单数据...";
  235. ProvidersFactory.GetPowerMettersBaseProvider((comboBox2.SelectedItem as CountyItem).CountyValue).DeleteAll();
  236. label1.Text = "正在插入新权力清单数据...";
  237. ProvidersFactory.GetPowerMettersBaseProvider((comboBox2.SelectedItem as CountyItem).CountyValue).Insert(list);
  238. label1.Text = "正在清理老权力清单的详细数据...";
  239. ProvidersFactory.GetPowerMettersDetailProvider((comboBox2.SelectedItem as CountyItem).CountyValue).DeleteByPowerMattersBase();
  240. string s = "正在插入新权力清单的详细数据({0}/{1})...";
  241. int i = 0;
  242. int c = list.Count;
  243. foreach (PowerMattersBase p in list)
  244. {
  245. label1.Text = String.Format("正在插入新权力清单的详细数据({0}/{1})...", i, c);
  246. if (!ProvidersFactory.GetPowerMettersDetailProvider((comboBox2.SelectedItem as CountyItem).CountyValue).IsExist(p.QL_INNER_CODE))
  247. {
  248. PowerMattersDetail pd = ProvidersFactory.GetQLSXProvider((comboBox2.SelectedItem as CountyItem).CountyValue).FindByID(p.QL_INNER_CODE);
  249. if (pd != null)
  250. ProvidersFactory.GetPowerMettersDetailProvider((comboBox2.SelectedItem as CountyItem).CountyValue).Insert(pd);
  251. }
  252. i += 1;
  253. }
  254. label1.Text = "完成";
  255. }
  256. private PowerMattersBase ReadPowerMatters(IRow row)
  257. {
  258. //if (row.Cells.Count < 6)
  259. // return null;
  260. PowerMattersBase a = new PowerMattersBase();
  261. a.OUGUID = row.GetCell(1).StringCellValue;
  262. a.QL_INNER_CODE = row.GetCell(3).StringCellValue;
  263. a.QL_DEP = row.GetCell(4).StringCellValue;
  264. a.QL_NAME = row.GetCell(5).StringCellValue;
  265. return a;
  266. }
  267. private void button5_Click(object sender, EventArgs e)
  268. {
  269. if (openFileDialog1.ShowDialog() == DialogResult.OK)
  270. {
  271. textBox3.Text = openFileDialog1.FileName;
  272. }
  273. }
  274. private void button6_Click(object sender, EventArgs e)
  275. {
  276. label2.Text = "正在载入数据...";
  277. IList<PowerMattersBase> list = ExcelHelper.ExcelToList<PowerMattersBase>(textBox3.Text, ReadPowerMatters);
  278. label2.Text = "正在更新部门信息...";
  279. foreach (PowerMattersBase p in list)
  280. {
  281. Branch b = ProvidersFactory.GetBranchProvider((comboBox2.SelectedItem as CountyItem).CountyValue).Find(p.OUGUID);
  282. if (b == null)
  283. {
  284. b = new Branch()
  285. {
  286. GUID = p.OUGUID,
  287. ShortName = p.QL_DEP
  288. };
  289. ProvidersFactory.GetBranchProvider((comboBox2.SelectedItem as CountyItem).CountyValue).Insert(b);
  290. }
  291. else
  292. {
  293. p.QL_DEP = b.ShortName;
  294. }
  295. }
  296. label2.Text = "正在清除老的最多跑一次数据...";
  297. ProvidersFactory.GetOnceRunPowerMattersProvider((comboBox2.SelectedItem as CountyItem).CountyValue).DeleteAll();
  298. label2.Text = "正在插入新的最多跑一次数据...";
  299. ProvidersFactory.GetOnceRunPowerMattersProvider((comboBox2.SelectedItem as CountyItem).CountyValue).Insert(list);
  300. label2.Text = "完成";
  301. }
  302. private void button7_Click(object sender, EventArgs e)
  303. {
  304. if(folderBrowserDialog1.ShowDialog() == DialogResult.OK)
  305. {
  306. IEnumerable<Branch> bs = ProvidersFactory.GetBranchProvider((comboBox2.SelectedItem as CountyItem).CountyValue).FindOnceRunBranch();
  307. foreach(Branch b in bs)
  308. {
  309. label3.Text = "正在导出“" + b.ShortName + ".xls”";
  310. IEnumerable<PowerMattersBase> ps = ProvidersFactory.GetOnceRunPowerMattersProvider((comboBox2.SelectedItem as CountyItem).CountyValue).FindOnceRunPowerMattersByBranchGUID(b.GUID);
  311. foreach(PowerMattersBase p in ps)
  312. {
  313. p.QL_DEP = b.ShortName;
  314. p.OUGUID = b.GUID;
  315. }
  316. ExcelHelper.WriteExcel<PowerMattersBase>(ps.ToList<PowerMattersBase>(), folderBrowserDialog1.SelectedPath + "\\" + b.ShortName + ".xls", ExportWriteHeard, ExportWriteRow);
  317. }
  318. label3.Text = "完成";
  319. }
  320. }
  321. private void ExportWriteRow(IRow row, PowerMattersBase obj)
  322. {
  323. row.CreateCell(0, CellType.String).SetCellValue(obj.OUGUID);
  324. row.CreateCell(1, CellType.String).SetCellValue(obj.QL_INNER_CODE);
  325. row.CreateCell(2, CellType.String).SetCellValue(obj.QL_DEP);
  326. row.CreateCell(3, CellType.String).SetCellValue(obj.QL_NAME);
  327. row.CreateCell(4, CellType.String).SetCellValue("");
  328. row.CreateCell(5, CellType.String).SetCellValue("");
  329. row.CreateCell(6, CellType.String).SetCellValue("");
  330. row.CreateCell(7, CellType.String).SetCellValue("");
  331. row.CreateCell(8, CellType.String).SetCellValue("");
  332. row.CreateCell(9, CellType.String).SetCellValue("");
  333. PowerMattersBase p = ProvidersFactory.GetPowerMatterTypeProvider((comboBox2.SelectedItem as CountyItem).CountyValue).FindById(obj.QL_INNER_CODE);
  334. if (p != null)
  335. row.CreateCell(5 + p.ApplyCode).SetCellValue("1");
  336. else
  337. row.CreateCell(4).SetCellValue("1");
  338. int i = 0;
  339. foreach (ICell c in row.Cells)
  340. {
  341. ICellStyle style = row.Sheet.Workbook.CreateCellStyle();
  342. style.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  343. style.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  344. style.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  345. style.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  346. style.GetFont(row.Sheet.Workbook).IsBold = false;
  347. if (i == 3)
  348. style.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Left;
  349. else
  350. style.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
  351. i++;
  352. if (p == null)
  353. style.FillBackgroundColor = 51;
  354. c.CellStyle = style;
  355. }
  356. }
  357. private void ExportWriteHeard(IRow row)
  358. {
  359. row.Sheet.SetColumnWidth(0, 15 * 256);
  360. row.Sheet.SetColumnWidth(1, 40 * 256);
  361. row.Sheet.SetColumnWidth(2, 15 * 256);
  362. row.Sheet.SetColumnWidth(3, 50 * 256);
  363. row.Sheet.SetColumnWidth(4, 15 * 256);
  364. row.Sheet.SetColumnWidth(5, 15 * 256);
  365. row.Sheet.SetColumnWidth(6, 15 * 256);
  366. row.Sheet.SetColumnWidth(7, 15 * 256);
  367. row.Sheet.SetColumnWidth(8, 15 * 256);
  368. row.Sheet.SetColumnWidth(9, 15 * 256);
  369. row.CreateCell(0, CellType.String).SetCellValue("部门编号");
  370. row.CreateCell(1, CellType.String).SetCellValue("权力编码");
  371. row.CreateCell(2, CellType.String).SetCellValue("部门名称");
  372. row.CreateCell(3, CellType.String).SetCellValue("权力名称");
  373. row.CreateCell(4, CellType.String).SetCellValue("其他");
  374. row.CreateCell(5, CellType.String).SetCellValue("即办");
  375. row.CreateCell(6, CellType.String).SetCellValue("全程网上申办,在线取件");
  376. row.CreateCell(7, CellType.String).SetCellValue("全程网上申办,快递送达");
  377. row.CreateCell(8, CellType.String).SetCellValue("网上申报,预审,窗口核验取件");
  378. row.CreateCell(9, CellType.String).SetCellValue("窗口申报,快递送达");
  379. foreach(ICell c in row.Cells)
  380. {
  381. ICellStyle style = row.Sheet.Workbook.CreateCellStyle();
  382. style.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  383. style.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  384. style.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  385. style.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  386. style.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
  387. style.VerticalAlignment = VerticalAlignment.Center;
  388. style.WrapText = true;
  389. style.GetFont(row.Sheet.Workbook).IsBold = true;
  390. c.CellStyle = style;
  391. }
  392. }
  393. private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
  394. {
  395. }
  396. private void comboBox2_SelectedIndexChanged_1(object sender, EventArgs e)
  397. {
  398. }
  399. }
  400. }