private void OnExportData() { List <DataTable> dts = new List <DataTable>(); List <string> sheetNames = new List <string>(); if (this.dataGridView2.DataSource != null) { DataTable dt = this.dataGridView2.DataSource as DataTable; dt.TableName = "电芯数据"; dts.Add(dt); sheetNames.Add(dt.TableName); } SaveFileDialog dlg = new SaveFileDialog(); dlg.Filter = "excel files (*.xlsx)|*.xlsx"; if (dlg.ShowDialog() == DialogResult.OK) { try { string fileName = dlg.FileName; DelegateExportLog dlgtExportLog = new DelegateExportLog(AsyExportLog); dlgtExportLog.BeginInvoke(dts.ToArray(), fileName, sheetNames.ToArray(), CallbackExportlogOK, dlgtExportLog); waitDlg = new LogManage.WaitDlg(); waitDlg.ShowDialog(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } }
private void OnExportLog(DataTable dt) { SaveFileDialog dlg = new SaveFileDialog(); dlg.Filter = "excel files (*.xlsx)|*.xlsx"; if (dlg.ShowDialog() == DialogResult.OK) { try { string fileName = dlg.FileName; string sheetName = "Log"; DelegateExportLog dlgtExportLog = new DelegateExportLog(AsyExportLog); dlgtExportLog.BeginInvoke(dt, fileName, sheetName, CallbackExportlogOK, dlgtExportLog); waitDlg = new WaitDlg(); waitDlg.ShowDialog(); //CreateExcelFile(fileName, sheetName); ////string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + ////"Data Source=" + fileName + ";" + ////"Extended Properties='Excel 8.0; HDR=Yes; IMEX=2'"; //string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;" + //"Data Source=" + fileName + ";" + //"Extended Properties='Excel 12.0; HDR=Yes; IMEX=0'"; //using (OleDbConnection ole_conn = new OleDbConnection(strConn)) //{ // ole_conn.Open(); // using (OleDbCommand ole_cmd = ole_conn.CreateCommand()) // { // foreach (DataRow dr in dt.Rows) // { // string logContent = dr["内容"].ToString(); // logContent = logContent.Substring(0, Math.Min(255, logContent.Count())); // ole_cmd.CommandText = string.Format(@"insert into [{0}$](日志ID,内容,类别,日志来源,时间)values('{1}','{2}','{3}','{4}','{5}')", sheetName, dr["日志ID"].ToString(), logContent, dr["类别"].ToString(), dr["日志来源"].ToString(), dr["时间"].ToString()); // ole_cmd.ExecuteNonQuery(); // } // MessageBox.Show("数据导出成功......"); // } //} } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } }