private void button4_Click(object sender, EventArgs e) { try { string Filename = ""; if (this.saveFileDialog1.ShowDialog() == DialogResult.OK) { Filename = this.saveFileDialog1.FileName.Trim(); GetAllnodes Allnodes = new GetAllnodes(); Allnodes.GetAllnode(this.treeView1.Nodes); ArrayList dm = Allnodes.ArrayNodeName; ArrayList nm = Allnodes.ArrayNodeText; ArrayList ph = Allnodes.ArrayNodeFullPath; ArrayList nn = Allnodes.ArrayNodeTag; if (Filename != "") { HSSFWorkbook hSSFWorkbook = new HSSFWorkbook(); HSSFSheet sheet1 = hSSFWorkbook.CreateSheet("Sheet1"); hSSFWorkbook.CreateSheet("Sheet2"); hSSFWorkbook.CreateSheet("Sheet3"); HSSFRow row1 = sheet1.CreateRow(0); row1.CreateCell(0).SetCellValue("行政代码"); row1.CreateCell(1).SetCellValue("单位名称"); for (int i = 0; i < dm.Count; i++) { HSSFRow row2 = sheet1.CreateRow(i + 1); row2.CreateCell(0).SetCellValue(nn[i].ToString()); row2.CreateCell(1).SetCellValue(nm[i].ToString()); } FileStream FileStreamfile = new FileStream(Filename, FileMode.Create); hSSFWorkbook.Write(FileStreamfile); FileStreamfile.Close(); MessageBox.Show("保存成功!", "管理系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show("文件名不能为空!", "管理系统提示", MessageBoxButtons.OK, MessageBoxIcon.Stop); } } } catch (System.Exception ex) { MessageBox.Show(ex.Message, "管理系统提示", MessageBoxButtons.OK, MessageBoxIcon.Stop); } }
private void button3_Click(object sender, EventArgs e) { string connectionString = Uitl.GetConnectionString();//写连接字符串 string selstring = "SELECT * FROM DataDic"; SqlConnection Connection1 = new SqlConnection(connectionString); try { Connection1.Open(); if (Tools.DataBasic.Create_Dic_table(connectionString))//如果"DataDic"表创建成功或已经存在 { SqlDataAdapter Adapter = DataBasic.Dic_Initadapter(selstring, connectionString); DataSet dataset = new DataSet(); Adapter.Fill(dataset, "DataDic");//填充Dataset GetAllnodes Allnodes = new GetAllnodes(); Allnodes.GetAllnode(this.treeView1.Nodes); ArrayList nm = Allnodes.ArrayNodeText; ArrayList ph = Allnodes.ArrayNodeFullPath; ArrayList nn = Allnodes.ArrayNodeTag; if (MessageBox.Show("数据库中原有内容将被全部覆盖!", "系统提示", MessageBoxButtons.OKCancel) == DialogResult.OK) { for (int i = 0; i < dataset.Tables["DataDic"].Rows.Count; i++) { DataRow Row = dataset.Tables["DataDic"].Rows[i]; Row.Delete(); } System.Data.DataTable TB = dataset.Tables["DataDic"]; DataRowCollection rowCollection = TB.Rows; for (int i = 0; i < nn.Count; i++) { string xzdm = nn[i].ToString(); DataRow row = dataset.Tables["DataDic"].NewRow(); row["ZXDM"] = xzdm; row["MC"] = nm[i].ToString(); row["SJDM"] = xzdm.Substring(0, 2); row["DSDM"] = xzdm.Substring(2, 2); row["XQDM"] = xzdm.Substring(4, 2); row["XZDM"] = xzdm.Substring(6, 3); row["XZCDM"] = xzdm.Substring(9, 3); //row["CMZDM"] = xzdm.Substring(12, 2); row["WZMC"] = ph[i].ToString(); dataset.Tables["DataDic"].Rows.Add(row); } Adapter.Update(dataset, "DataDic"); MessageBox.Show("数据字典上传完毕", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } } Connection1.Close(); } catch (System.Exception ex) { MessageBox.Show(ex.Message, "管理系统提示", MessageBoxButtons.OK, MessageBoxIcon.Stop); } finally { if (Connection1.State == ConnectionState.Open) { Connection1.Close(); } } }