赞
踩
//导出按钮 private void ImportBnt_Click(object sender, EventArgs e) { if (this.dgvFaultCodeView.Rows[0].Cells["Value"].Value == null) { MessageBox.Show(Properties.Resources.AlarmForm_NODataSave); return; } SaveFileDialog SaveOscData = new SaveFileDialog(); SaveOscData.Filter = "Excel(*.xls)|*.xls"; if (SaveOscData.ShowDialog() == DialogResult.OK) { bool Chinese = false; if (Utility.APP_LANG == Utility.CHINESE_LANG) {//中文 Chinese = true; } //获得oledb数据库连接 String tempPath = SaveOscData.FileName; string tempCon = "Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source =" + tempPath + ";Extended Properties=Excel 8.0"; System.Data.OleDb.OleDbConnection tempoledecon = new System.Data.OleDb.OleDbConnection(tempCon); try { if (File.Exists(tempPath)) { File.Delete(tempPath); } tempoledecon.Open(); //打开连接 System.Data.OleDb.OleDbCommand tempOlecmd = new System.Data.OleDb.OleDbCommand(); tempOlecmd.Connection = tempoledecon; //绑定OleDb命令与连接 if (Chinese) { //创建表格 sheet, 名称是[错误代码],表头有 错误代码和说明两个字段,字段类型是VARCHAR tempOlecmd.CommandText = "CREATE TABLE [错误代码] ([错误代码] VARCHAR,[说明] VARCHAR)"; } else { tempOlecmd.CommandText = "CREATE TABLE [ErrorCode] ([ErrorCode] VARCHAR,[Description] VARCHAR)"; } tempOlecmd.ExecuteNonQuery(); string ErrCode = this.dgvFaultCodeView.Rows[0].Cells["Value"].Value.ToString(); string ErrMsg = this.dgvFaultCodeView.Rows[0].Cells["Instruction"].Value.ToString(); if (Chinese) { //插入数据到【错误代码】sheet中,绑定表头字段对应的值 tempOlecmd.CommandText = "INSERT INTO[错误代码$](错误代码,说明) VALUES('" + ErrCode + "','" + ErrMsg + "')"; } else { tempOlecmd.CommandText = "INSERT INTO[ErrorCode$](ErrorCode,Description) VALUES('" + ErrCode + "','" + ErrMsg + "')"; } tempOlecmd.ExecuteNonQuery(); if (Chinese) { //创建表格,sheet tempOlecmd.CommandText = "CREATE TABLE [故障信息] ([名称] VARCHAR,[值] VARCHAR,[单位] VARCHAR)"; } else { //创建表格 tempOlecmd.CommandText = "CREATE TABLE [FaultInfo] ([Name] VARCHAR,[ValueData] VARCHAR,[Unit] VARCHAR)"; } tempOlecmd.ExecuteNonQuery(); if (Chinese) { for (int i = 0; i < this.dgvMessageView.Rows.Count; i++) { string name = this.dgvMessageView.Rows[i].Cells["MsgName"].Value.ToString(); string value = this.dgvMessageView.Rows[i].Cells["MsgValue"].Value.ToString(); string unit = this.dgvMessageView.Rows[i].Cells["MsgUnit"].Value.ToString(); //插入数据到【故障信息】sheet中,绑定表头字段对应的数据 tempOlecmd.CommandText = "INSERT INTO[故障信息$](名称,值,单位) VALUES('" + name + "','" + value + "','" + unit + "')"; tempOlecmd.ExecuteNonQuery(); } } else { for (int i = 0; i < this.dgvMessageView.Rows.Count; i++) { string name = this.dgvMessageView.Rows[i].Cells["MsgName"].Value.ToString(); string value = this.dgvMessageView.Rows[i].Cells["MsgValue"].Value.ToString(); string unit = this.dgvMessageView.Rows[i].Cells["MsgUnit"].Value.ToString(); tempOlecmd.CommandText = "INSERT INTO[FaultInfo$](Name,ValueData,Unit) VALUES('" + name + "','" + value + "','" + unit + "')"; tempOlecmd.ExecuteNonQuery(); } } tempoledecon.Close(); } catch (Exception ex) { tempoledecon.Close(); MessageBox.Show(Properties.Resources.AlarmForm_FailSave); } } }
创建了两个Sheet,每个sheet中有自己的数据。
注:该Demo中 的工程使用的是.net Frame 4.0
1)创建sheet的名称不能太长,如果是英文两个单词之间不能使用空格隔开。(比如使用 Error Message(有空格) 或 ErrorMessages(名称太长)都会出错)
2)创建的sheet和表头名称,和后面要插入数据的sheet和表头字段需要一一对应上。(注创建字段的名称不能叫Value, PS:我前面使用了一直报错,无奈之下将Value更改为ValueData才成功)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。