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.KFQ }); //IEnumerable 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 list = ExcelHelper.ExcelToList(textBox1.Text, GetApplyType); IList failslist = new List(); 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(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 list = ExcelHelper.ExcelToList(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 c = list.Count; foreach (PowerMattersBase p in list) { label1.Text = String.Format("正在插入新权力清单的详细数据({0}/{1})...", i, c); 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); } } label1.Text = "完成"; } 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 list = ExcelHelper.ExcelToList(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 bs = ProvidersFactory.GetBranchProvider((comboBox2.SelectedItem as CountyItem).CountyValue).FindOnceRunBranch(); foreach(Branch b in bs) { label3.Text = "正在导出“" + b.ShortName + ".xls”"; IEnumerable ps = ProvidersFactory.GetOnceRunPowerMattersProvider((comboBox2.SelectedItem as CountyItem).CountyValue).FindOnceRunPowerMattersByBranchGUID(b.GUID); foreach(PowerMattersBase p in ps) { p.QL_DEP = b.ShortName; p.OUGUID = b.GUID; } ExcelHelper.WriteExcel(ps.ToList(), 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; foreach (ICell c in row.Cells) { 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.GetFont(row.Sheet.Workbook).IsBold = false; if (i == 3) style.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Left; else style.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center; i++; if (p == null) style.FillBackgroundColor = 51; c.CellStyle = style; } } 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("窗口申报,快递送达"); foreach(ICell c in row.Cells) { 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; c.CellStyle = style; } } private void comboBox2_SelectedIndexChanged(object sender, EventArgs e) { } } }