| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439 |
- using NPOI.HSSF.UserModel;
- using NPOI.HSSF.Util;
- using NPOI.SS.UserModel;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using Winsoft.GOV.Framework.Model;
- using Winsoft.GOV.Framework.Provider;
- using static Winsoft.GOV.ImportDataForm.ExcelHelper;
- namespace Winsoft.GOV.ImportDataForm
- {
- public partial class Form1 : Form
- {
- class CountyItem
- {
- public string Name { get; set; }
- public County CountyValue { get; set; }
- public override string ToString()
- {
- return Name;
- }
- }
- public Form1()
- {
- InitializeComponent();
- }
- private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
- {
- }
- private void button2_Click(object sender, EventArgs e)
- {
- if (openFileDialog1.ShowDialog() == DialogResult.OK)
- {
- textBox1.Text = openFileDialog1.FileName;
- comboBox1.Text = Path.GetFileNameWithoutExtension(openFileDialog1.FileName);
- dataGridView1.DataSource = ExcelHelper.ExcelToDataSet(textBox1.Text).Tables[0];
- dataGridView1.Show();
- }
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- comboBox2.Items.Add(new CountyItem() {
- Name = "丽水",
- CountyValue = County.LS
- });
- comboBox2.Items.Add(new CountyItem()
- {
- Name = "莲都",
- CountyValue = County.LD
- });
- comboBox2.Items.Add(new CountyItem()
- {
- Name = "龙泉",
- CountyValue = County.LQ
- });
- comboBox2.Items.Add(new CountyItem()
- {
- Name = "青田",
- CountyValue = County.QT
- });
- comboBox2.Items.Add(new CountyItem()
- {
- Name = "云和",
- CountyValue = County.YH
- });
- comboBox2.Items.Add(new CountyItem()
- {
- Name = "缙云",
- CountyValue = County.JY
- });
- comboBox2.Items.Add(new CountyItem()
- {
- Name = "遂昌",
- CountyValue = County.SC
- });
- comboBox2.Items.Add(new CountyItem()
- {
- Name = "景宁",
- CountyValue = County.JN
- });
- comboBox2.Items.Add(new CountyItem()
- {
- Name = "庆元",
- CountyValue = County.QY
- });
- comboBox2.Items.Add(new CountyItem()
- {
- Name = "松阳",
- CountyValue = County.SY
- });
- comboBox2.Items.Add(new CountyItem()
- {
- Name = "开发区",
- CountyValue = County.KFQ
- });
- //IEnumerable<Branch> tmp = BranchProvider.Instance.FindAll();
- //PowerMatterTypeProvider.Instance.CreateTable();
- //PowerMettersBaseProvider.Instance.CreateTable();
- //OnceRunPowerMattersProvider.Instance.CreateTable();
- //foreach(Branch b in tmp)
- //{
- // comboBox1.Items.Add(b);
- //}
- }
- private void button1_Click(object sender, EventArgs e)
- {
- bool find = false;
- foreach (Branch b in comboBox1.Items)
- {
- if (comboBox1.Text == b.ShortName)
- {
- comboBox1.SelectedItem = b;
- find = true;
- break;
- }
- }
- if (!find)
- {
- if (MessageBox.Show("没有找到部门名为“" + comboBox1.Text + "”的部门,您是否向系统添加该部门?", "添加部门", MessageBoxButtons.OKCancel) == DialogResult.OK)
- {
- Branch b = new Branch()
- {
- GUID = comboBox1.Text,
- ShortName = comboBox1.Text
- };
- comboBox1.Items.Add(b);
- comboBox1.SelectedItem = b;
- }
- else
- return;
- }
- IList<PowerMattersBase> list = ExcelHelper.ExcelToList<PowerMattersBase>(textBox1.Text, GetApplyType);
- IList<PowerMattersBase> failslist = new List<PowerMattersBase>();
- ProvidersFactory.GetPowerMatterTypeProvider((comboBox2.SelectedItem as CountyItem).CountyValue).Insert(list, ref failslist);
- if (failslist.Count == 0)
- {
- MessageBox.Show("导入成功");
- }
- else
- {
- saveFileDialog1.FileName = String.Format("{0}(失败记录).xls", comboBox1.Text);
- if (saveFileDialog1.ShowDialog() == DialogResult.OK)
- {
- ExcelHelper.WriteExcel<PowerMattersBase>(failslist, saveFileDialog1.FileName, WriteHeard, WriteRow);
- }
- MessageBox.Show("完成");
- }
- }
- private void WriteRow(IRow row, PowerMattersBase obj)
- {
- row.CreateCell(0, CellType.String).SetCellValue(obj.QL_DEP);
- row.CreateCell(1, CellType.String).SetCellValue(obj.OUGUID);
- row.CreateCell(2, CellType.String).SetCellValue(obj.QL_NAME);
- row.CreateCell(3, CellType.Numeric).SetCellValue(obj.ApplyCode);
- row.CreateCell(4, CellType.String).SetCellValue((obj.ApplyCode > -1 && obj.ApplyCode < ApplyType.Names.Length)? ApplyType.Names[obj.ApplyCode] : "其他");
- }
- private void WriteHeard(IRow row)
- {
- row.CreateCell(0, CellType.String).SetCellValue("部门名称");
- row.CreateCell(1, CellType.String).SetCellValue("部门编号");
- row.CreateCell(2, CellType.String).SetCellValue("权力名称");
- row.CreateCell(3, CellType.String).SetCellValue("分类编码");
- row.CreateCell(4, CellType.String).SetCellValue("分类名称");
- }
- private PowerMattersBase GetApplyType(IRow row)
- {
- if (row.Cells.Count != 7)
- return null;
- PowerMattersBase a = new PowerMattersBase();
- a.QL_DEP = (comboBox1.SelectedItem as Branch).ShortName;
- a.OUGUID = (comboBox1.SelectedItem as Branch).GUID;
- a.QL_NAME = row.GetCell(1).StringCellValue.Trim();
- int i = 0;
- for (int j = 2; j <= row.Cells.Count - 1; j++)
- {
- ICell cell = row.GetCell(j);
- if (cell == null || cell.CellType == CellType.Blank || (cell.CellType == CellType.String && String.IsNullOrWhiteSpace(cell.StringCellValue))
- || (cell.CellType == CellType.Numeric && cell.NumericCellValue != 1))
- {
- i++;
- continue;
- }
- else
- break;
- }
- a.ApplyCode = i > ApplyType.Names.Length - 1 ? -1 : i;
- return a;
- }
- private void button3_Click(object sender, EventArgs e)
- {
- if (openFileDialog1.ShowDialog() == DialogResult.OK)
- {
- textBox2.Text = openFileDialog1.FileName;
- string countyName = Path.GetFileNameWithoutExtension(openFileDialog1.FileName);
- foreach(CountyItem n in comboBox2.Items)
- {
- if (countyName == n.Name)
- {
- comboBox2.SelectedItem = n;
- break;
- }
- }
- }
- }
- private void button4_Click(object sender, EventArgs e)
- {
- label1.Text = "正在载入数据...";
- IList<PowerMattersBase> list = ExcelHelper.ExcelToList<PowerMattersBase>(textBox2.Text, ReadPowerMatters);
- label1.Text = "正在更新部门信息...";
- foreach (PowerMattersBase p in list)
- {
- Branch b = ProvidersFactory.GetBranchProvider((comboBox2.SelectedItem as CountyItem).CountyValue).Find(p.OUGUID);
- if (b == null)
- {
- b = new Branch()
- {
- GUID = p.OUGUID,
- ShortName = p.QL_DEP
- };
- ProvidersFactory.GetBranchProvider((comboBox2.SelectedItem as CountyItem).CountyValue).Insert(b);
- }
- else
- {
- p.QL_DEP = b.ShortName;
- }
- }
- label1.Text = "正在清除老权力清单数据...";
- ProvidersFactory.GetPowerMettersBaseProvider((comboBox2.SelectedItem as CountyItem).CountyValue).DeleteAll();
- label1.Text = "正在插入新权力清单数据...";
- ProvidersFactory.GetPowerMettersBaseProvider((comboBox2.SelectedItem as CountyItem).CountyValue).Insert(list);
- label1.Text = "正在清理老权力清单的详细数据...";
- ProvidersFactory.GetPowerMettersDetailProvider((comboBox2.SelectedItem as CountyItem).CountyValue).DeleteByPowerMattersBase();
- string s = "正在插入新权力清单的详细数据({0}/{1})...";
- int i = 0;
- int cw = 0;
- int c = list.Count;
- foreach (PowerMattersBase p in list)
- {
- label1.Text = String.Format("正在插入新权力清单的详细数据({0}/{1})...", i, c);
- try {
- if (!ProvidersFactory.GetPowerMettersDetailProvider((comboBox2.SelectedItem as CountyItem).CountyValue).IsExist(p.QL_INNER_CODE))
- {
- PowerMattersDetail pd = ProvidersFactory.GetQLSXProvider((comboBox2.SelectedItem as CountyItem).CountyValue).FindByID(p.QL_INNER_CODE);
- if (pd != null)
- ProvidersFactory.GetPowerMettersDetailProvider((comboBox2.SelectedItem as CountyItem).CountyValue).Insert(pd);
- }
- }
- catch
- {
- cw += 1;
- }
- i += 1;
- }
- label1.Text = String.Format("完成{0}条,出错{1}条", i, cw);
- }
- private PowerMattersBase ReadPowerMatters(IRow row)
- {
- //if (row.Cells.Count < 6)
- // return null;
- PowerMattersBase a = new PowerMattersBase();
- a.OUGUID = row.GetCell(1).StringCellValue;
- a.QL_INNER_CODE = row.GetCell(3).StringCellValue;
- a.QL_DEP = row.GetCell(4).StringCellValue;
- a.QL_NAME = row.GetCell(5).StringCellValue;
- return a;
- }
- private void button5_Click(object sender, EventArgs e)
- {
- if (openFileDialog1.ShowDialog() == DialogResult.OK)
- {
- textBox3.Text = openFileDialog1.FileName;
- }
- }
- private void button6_Click(object sender, EventArgs e)
- {
- label2.Text = "正在载入数据...";
- IList<PowerMattersBase> list = ExcelHelper.ExcelToList<PowerMattersBase>(textBox3.Text, ReadPowerMatters);
- label2.Text = "正在更新部门信息...";
- foreach (PowerMattersBase p in list)
- {
- Branch b = ProvidersFactory.GetBranchProvider((comboBox2.SelectedItem as CountyItem).CountyValue).Find(p.OUGUID);
- if (b == null)
- {
- b = new Branch()
- {
- GUID = p.OUGUID,
- ShortName = p.QL_DEP
- };
- ProvidersFactory.GetBranchProvider((comboBox2.SelectedItem as CountyItem).CountyValue).Insert(b);
- }
- else
- {
- p.QL_DEP = b.ShortName;
- }
- }
- label2.Text = "正在清除老的最多跑一次数据...";
- ProvidersFactory.GetOnceRunPowerMattersProvider((comboBox2.SelectedItem as CountyItem).CountyValue).DeleteAll();
- label2.Text = "正在插入新的最多跑一次数据...";
- ProvidersFactory.GetOnceRunPowerMattersProvider((comboBox2.SelectedItem as CountyItem).CountyValue).Insert(list);
- label2.Text = "完成";
- }
- private void button7_Click(object sender, EventArgs e)
- {
- if(folderBrowserDialog1.ShowDialog() == DialogResult.OK)
- {
- IEnumerable<Branch> bs = ProvidersFactory.GetBranchProvider((comboBox2.SelectedItem as CountyItem).CountyValue).FindAll();
- foreach(Branch b in bs)
- {
- label3.Text = "正在导出“" + b.ShortName + ".xls”";
- IEnumerable<PowerMattersBase> ps = ProvidersFactory.GetPowerMettersBaseProvider((comboBox2.SelectedItem as CountyItem).CountyValue).FindByBranchGUID(b.GUID);
- foreach(PowerMattersBase p in ps)
- {
- p.QL_DEP = b.ShortName;
- p.OUGUID = b.GUID;
- }
- ExcelHelper.WriteExcel<PowerMattersBase>(ps.ToList<PowerMattersBase>(), folderBrowserDialog1.SelectedPath + "\\" + b.ShortName + ".xls", ExportWriteHeard, ExportWriteRow);
- }
- label3.Text = "完成";
- }
- }
- private void ExportWriteRow(IRow row, PowerMattersBase obj)
- {
- row.CreateCell(0, CellType.String).SetCellValue(obj.OUGUID);
- row.CreateCell(1, CellType.String).SetCellValue(obj.QL_INNER_CODE);
- row.CreateCell(2, CellType.String).SetCellValue(obj.QL_DEP);
- row.CreateCell(3, CellType.String).SetCellValue(obj.QL_NAME);
- row.CreateCell(4, CellType.String).SetCellValue("");
- row.CreateCell(5, CellType.String).SetCellValue("");
- row.CreateCell(6, CellType.String).SetCellValue("");
- row.CreateCell(7, CellType.String).SetCellValue("");
- row.CreateCell(8, CellType.String).SetCellValue("");
- row.CreateCell(9, CellType.String).SetCellValue("");
- PowerMattersBase p = ProvidersFactory.GetPowerMatterTypeProvider((comboBox2.SelectedItem as CountyItem).CountyValue).FindById(obj.QL_INNER_CODE);
- if (p != null)
- row.CreateCell(5 + p.ApplyCode).SetCellValue("1");
- else
- row.CreateCell(4).SetCellValue("1");
- int i = 0;
- ICellStyle leftStyle = row.Sheet.Workbook.CreateCellStyle();
- ICellStyle rightStyle = row.Sheet.Workbook.CreateCellStyle();
- leftStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
- leftStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
- leftStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
- leftStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
- leftStyle.GetFont(row.Sheet.Workbook).IsBold = false;
- leftStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Left;
- rightStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
- rightStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
- rightStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
- rightStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
- rightStyle.GetFont(row.Sheet.Workbook).IsBold = false;
- rightStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
- foreach (ICell c in row.Cells)
- {
-
-
- if (i == 3)
- c.CellStyle = leftStyle;
- else
- c.CellStyle = rightStyle;
- i++;
- //if (p == null)
- // c.CellStyle.FillBackgroundColor = 51;
- }
- }
- private void ExportWriteHeard(IRow row)
- {
- row.Sheet.SetColumnWidth(0, 15 * 256);
- row.Sheet.SetColumnWidth(1, 40 * 256);
- row.Sheet.SetColumnWidth(2, 15 * 256);
- row.Sheet.SetColumnWidth(3, 50 * 256);
- row.Sheet.SetColumnWidth(4, 15 * 256);
- row.Sheet.SetColumnWidth(5, 15 * 256);
- row.Sheet.SetColumnWidth(6, 15 * 256);
- row.Sheet.SetColumnWidth(7, 15 * 256);
- row.Sheet.SetColumnWidth(8, 15 * 256);
- row.Sheet.SetColumnWidth(9, 15 * 256);
- row.CreateCell(0, CellType.String).SetCellValue("部门编号");
- row.CreateCell(1, CellType.String).SetCellValue("权力编码");
- row.CreateCell(2, CellType.String).SetCellValue("部门名称");
- row.CreateCell(3, CellType.String).SetCellValue("权力名称");
- row.CreateCell(4, CellType.String).SetCellValue("其他");
- row.CreateCell(5, CellType.String).SetCellValue("即办");
- row.CreateCell(6, CellType.String).SetCellValue("全程网上申办,在线取件");
- row.CreateCell(7, CellType.String).SetCellValue("全程网上申办,快递送达");
- row.CreateCell(8, CellType.String).SetCellValue("网上申报,预审,窗口核验取件");
- row.CreateCell(9, CellType.String).SetCellValue("窗口申报,快递送达");
- ICellStyle style = row.Sheet.Workbook.CreateCellStyle();
- style.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
- style.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
- style.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
- style.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
- style.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
- style.VerticalAlignment = VerticalAlignment.Center;
- style.WrapText = true;
- style.GetFont(row.Sheet.Workbook).IsBold = true;
- foreach (ICell c in row.Cells)
- {
- c.CellStyle = style;
- }
- }
- private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
- {
- }
- private void comboBox2_SelectedIndexChanged_1(object sender, EventArgs e)
- {
- }
- }
- }
|