Form1.cs 18 KB

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