using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Diagnostics; using System.IO; using System.Text; using System.Windows.Forms; using System.Xml; using DevComponents.DotNetBar; namespace Cyberpipe { public partial class FrmMDDictory : Office2007Form { string filename = Utility.layerConfigFile; List<string> pipelineLayerNames = new List<string>();//线图层名称 List<string> workwellLayerNames = new List<string>();//工井图层名称 List<string> valueLayerNames = new List<string>();//阀门图层名称 List<string> instrumenLayerNames = new List<string>();//附属物图层名称 private static SqlConnection conn = null; private static SqlCommand sqlCmd = null; private static SqlDataReader sqlReader = null; public static string layernamePub; public FrmMDDictory() { InitializeComponent(); //初始化树 loadTreeView(layerTree); } private void layerTree_AfterSelect(object sender, TreeViewEventArgs e) { String layername = layerTree.SelectedNode.Text; layernamePub = layername; if (layerTree.SelectedNode.GetNodeCount(true) == 0) { searchLayerDicInfo(layername); } } private void searchLayerDicInfo(string layername) { string strSql = "select COLUMN_NAME 字段名, DATA_TYPE 类型编号, DATA_LENGTH 长度 from " + " user_tab_columns where TABLE_NAME='" + layername+"'"; DataTable table = OledbHelper.QueryTable(strSql); dataGridViewX1.DataSource = table; } private void loadTreeView(TreeView tv)//构建图层树 { tv.ShowLines = true; tv.CheckBoxes = false; TreeUtils.InitLayerTree(null, tv, null); } //导出结果 private void buttonX1_Click(object sender, EventArgs e) { if (dataGridViewX1.Rows.Count > 0) { SaveFileDialog dlg = new SaveFileDialog(); dlg.Filter = "Excel files (*.xls)|*.xls"; dlg.FilterIndex = 0; dlg.RestoreDirectory = true; //dlg.CreatePrompt = true; dlg.Title = "保存为Excel文件"; dlg.FileName = layernamePub + "图层-" + DateTime.Now.ToString("yyyyMMdd") + ".xls"; if (dlg.ShowDialog() == DialogResult.OK) { Stream myStream; myStream = dlg.OpenFile(); StreamWriter sw = new StreamWriter(myStream, Encoding.GetEncoding(-0)); string columnTitle = ""; try { sw.WriteLine("内容:操作日志;日期:" + DateTime.Now.ToString("yyyy-MM-dd")); //写入列标题 for (int i = 0; i < dataGridViewX1.ColumnCount; i++) { if (i > 0) { columnTitle += "\t"; } columnTitle += dataGridViewX1.Columns[i].HeaderText; } sw.WriteLine(columnTitle); //写入列内容 for (int j = 0; j < dataGridViewX1.Rows.Count; j++) { string columnValue = ""; for (int k = 0; k < dataGridViewX1.Columns.Count; k++) { if (k > 0) { columnValue += "\t"; } if (dataGridViewX1.Rows[j].Cells[k].Value == null) columnValue += ""; else columnValue += dataGridViewX1.Rows[j].Cells[k].Value.ToString().Trim(); } sw.WriteLine(columnValue); } sw.Close(); myStream.Close(); if (MessageBox.Show("导出Excel文件成功!是否打开?", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes) { Process.Start(dlg.FileName); } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } finally { sw.Close(); myStream.Close(); } } } } } }