private void button2_Click(object sender, EventArgs e) //导出 { string sFileName = tabControl1.SelectedTab.Text + "数据导出"; if (tabControl1.SelectedTab.Text == "加工控制") { if (dataGridView1.Rows.Count > 0) { CommExcel.ExportExcel(sFileName, dataGridView1, true); } } else if (tabControl1.SelectedTab.Text == "排放控制") { if (dataGridView2.Rows.Count > 0) { CommExcel.ExportExcel(sFileName, dataGridView2, true); } } else if (tabControl1.SelectedTab.Text == "持续生产") { if (dataGridView3.Rows.Count > 0) { CommExcel.ExportExcel(sFileName, dataGridView3, true); } } }
private void button2_Click(object sender, EventArgs e) { if (dataGridView1.Rows.Count > 0) { CommExcel.ExportExcel(this.Text + "导出", dataGridView1, true); } }
private void button2_Click(object sender, EventArgs e) //取消 { if (bdsFind.Count == 0) { MessageBox.Show("无数据,无法导出"); return; } string sFileName = string.Empty; if (rbByHour.Checked) //小时 { sFileName = "按小时导出"; } else if (rbByDay.Checked) //每天 { sFileName = "按每天导出"; } else if (rbByMonth.Checked) //每月 { sFileName = "按每月导出"; } else if (rbByYear.Checked) //每年 { sFileName = "按每年导出"; } CommExcel.ExportExcel(sFileName, dataGridView1, true); }
private void btnEXCEL_Click(object sender, EventArgs e) { if (bdsMaster.Count == 0) { MessageBox.Show("查无数据"); return; } string sFileName = this.Text + "导出数据"; CommExcel.ExportExcel(sFileName, dataGridView1, true); }
private void button2_Click(object sender, EventArgs e) { string sFileName = "肉业公司日成本分析导出"; if (dataGridView1.Rows.Count > 0) { CommExcel.ExportExcel(sFileName, dataGridView1, true); } else { MessageBox.Show("无数据,无法导出"); return; } }
/// <summary> /// 导出数据 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button2_Click(object sender, EventArgs e) //导出 { string sFileName = tabControl1.SelectedTab.Text + Convert.ToDateTime(dateTimePicker1.Text).ToString("yyyy-MM-dd"); if (tabControl1.SelectedIndex == 0) { if (bdsTuZai.Count > 0) { CommExcel.ExportExcel(sFileName, dataGridView1, true); } else { MessageBox.Show("无数据,无法导出"); return; } } else if (tabControl1.SelectedIndex == 2) { if (bdsFengGe.Count > 0) { CommExcel.ExportExcel(sFileName, dataGridView3, true); } else { MessageBox.Show("无数据,无法导出"); return; } } else if (tabControl1.SelectedIndex == 1) { if (bdsPaiSuan.Count > 0) { CommExcel.ExportExcel(sFileName, dataGridView2, true); } else { MessageBox.Show("无数据,无法导出"); return; } } }
private void button2_Click(object sender, EventArgs e) { CommExcel.ExportExcel("", dataGridView1, true); return; SaveFileDialog saveFileDialog = new SaveFileDialog(); // ToExcel2(dataGridView1, saveFileDialog); saveFileDialog.Filter = "Execl files (*.xls)|*.xls"; saveFileDialog.FilterIndex = 0; saveFileDialog.RestoreDirectory = true; saveFileDialog.CreatePrompt = true; saveFileDialog.Title = "Export Excel File"; saveFileDialog.ShowDialog(); if (saveFileDialog.FileName == "") { return; } System.Data.DataTable Table = ds.Tables[0]; string ExcelFilePath = saveFileDialog.FileName; WaitFormService.CreateWaitForm(); WaitFormService.SetWaitFormCaption(" 正在导出,请稍候......"); try { if ((Table.TableName.Trim().Length == 0) || (Table.TableName.ToLower() == "table")) { Table.TableName = "Sheet1"; } //数据表的列数 int ColCount = Table.Columns.Count; //用于记数,实例化参数时的序号 int i = 0; //创建参数 OleDbParameter[] para = new OleDbParameter[ColCount]; //创建表结构的SQL语句 string TableStructStr = @"Create Table " + Table.TableName + "("; //连接字符串 string connString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + ExcelFilePath + ";Extended Properties=Excel 8.0;"; OleDbConnection objConn = new OleDbConnection(connString); //创建表结构 OleDbCommand objCmd = new OleDbCommand(); //数据类型集合 ArrayList DataTypeList = new ArrayList(); DataTypeList.Add("System.Decimal"); DataTypeList.Add("System.Double"); DataTypeList.Add("System.Int16"); DataTypeList.Add("System.Int32"); DataTypeList.Add("System.Int64"); DataTypeList.Add("System.Single"); //遍历数据表的所有列,用于创建表结构 foreach (DataColumn col in Table.Columns) { //如果列属于数字列,则设置该列的数据类型为double if (DataTypeList.IndexOf(col.DataType.ToString()) >= 0) { para[i] = new OleDbParameter("@" + col.ColumnName, OleDbType.Double); objCmd.Parameters.Add(para[i]); //如果是最后一列 if (i + 1 == ColCount) { TableStructStr += col.ColumnName + " double)"; } else { TableStructStr += col.ColumnName + " double,"; } } else { para[i] = new OleDbParameter("@" + col.ColumnName, OleDbType.VarChar); objCmd.Parameters.Add(para[i]); //如果是最后一列 if (i + 1 == ColCount) { TableStructStr += col.ColumnName + " varchar)"; } else { TableStructStr += col.ColumnName + " varchar,"; } } i++; } //创建Excel文件及文件结构 try { objCmd.Connection = objConn; objCmd.CommandText = TableStructStr; if (objConn.State == ConnectionState.Closed) { objConn.Open(); } objCmd.ExecuteNonQuery(); } catch (Exception exp) { throw exp; } //插入记录的SQL语句 string InsertSql_1 = "Insert into " + Table.TableName + " ("; string InsertSql_2 = " Values ("; string InsertSql = ""; //遍历所有列,用于插入记录,在此创建插入记录的SQL语句 for (int colID = 0; colID < ColCount; colID++) { if (colID + 1 == ColCount) //最后一列 { InsertSql_1 += Table.Columns[colID].ColumnName + ")"; InsertSql_2 += "@" + Table.Columns[colID].ColumnName + ")"; } else { InsertSql_1 += Table.Columns[colID].ColumnName + ","; InsertSql_2 += "@" + Table.Columns[colID].ColumnName + ","; } } InsertSql = InsertSql_1 + InsertSql_2; //遍历数据表的所有数据行 for (int rowID = 0; rowID < Table.Rows.Count; rowID++) { for (int colID = 0; colID < ColCount; colID++) { if (para[colID].DbType == DbType.Double && Table.Rows[rowID][colID].ToString().Trim() == "") { para[colID].Value = 0; } else { para[colID].Value = Table.Rows[rowID][colID].ToString().Trim(); } } try { objCmd.CommandText = InsertSql; objCmd.ExecuteNonQuery(); } catch (Exception exp) { string str = exp.Message; } } try { if (objConn.State == ConnectionState.Open) { objConn.Close(); } } catch (Exception exp) { WaitFormService.CloseWaitForm(); throw exp; } WaitFormService.CloseWaitForm(); MessageBox.Show("导出完成!", "软件提示"); } catch { WaitFormService.CloseWaitForm(); MessageBox.Show("导出失败!", "软件提示"); } //////////////////方法2 // SaveFileDialog saveFileDialog = new SaveFileDialog(); //// ToExcel2(dataGridView1, saveFileDialog); // saveFileDialog.Filter = "Execl files (*.xls)|*.xls"; // saveFileDialog.FilterIndex = 0; // saveFileDialog.RestoreDirectory = true; // saveFileDialog.CreatePrompt = true; // saveFileDialog.Title = "Export Excel File"; // saveFileDialog.ShowDialog(); // if (saveFileDialog.FileName == "") // return; // Stream myStream; // myStream = saveFileDialog.OpenFile(); // StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0)); // string str = ""; // WaitFormService.CreateWaitForm(); // WaitFormService.SetWaitFormCaption(" 正在导出,请稍候......"); // try // { // //寫dataGridView1表標題 // for (int i = 0; i < dataGridView1.ColumnCount; i++) // { // if (i > 0) // { // str += "\t"; // } // str += dataGridView1.Columns[i].HeaderText.Trim(); // } // sw.WriteLine(str); // //寫dataGridView1表內容 // for (int j = 0; j < dataGridView1.Rows.Count; j++) // { // string tempStr = ""; // for (int k = 0; k < dataGridView1.Columns.Count; k++) // { // if (k > 0) // { // tempStr += "\t"; // } // //tempStr += dataGridView1.Rows[j].Cells[k].Value.ToString().Trim() ; // tempStr += dataGridView1.Rows[j].Cells[k].Value; // //要增加對含數字的文字類,如身份証號 // //.........欠對身份証號處理,以免在EXCEL打開時被自動轉數字 // } // sw.WriteLine(tempStr); // } // sw.Close(); // myStream.Close(); // } // catch (Exception ex) // { // WaitFormService.CloseWaitForm(); // MessageBox.Show("导出失败!" + ex.ToString(), "软件提示"); // } // finally // { // sw.Close(); // myStream.Close(); // } // WaitFormService.CloseWaitForm(); // MessageBox.Show("导出完成!", "软件提示"); //////////////////方法1 //System.Data.DataTable dt = ds.Tables[0]; //if (dt.Rows.Count == 0) //{ // return; //} //string localFilePath = String.Empty; ////设置文件类型 ////saveFileDialog1.Filter = " xls files(*.xls)|*.txt|All files(*.*)|*.*"; ////设置文件名称: //saveFileDialog1.FileName = DateTime.Now.ToString("yyyyMMdd") + "-" + "食品应收帐款表.xls"; ////点了保存按钮进入 //if (saveFileDialog1.ShowDialog() == DialogResult.OK) //{ // WaitFormService.CreateWaitForm(); // WaitFormService.SetWaitFormCaption(" 正在导出,请稍候......"); // try // { // //获得文件路径 // localFilePath = saveFileDialog1.FileName.ToString(); // //string wordPath = Application.StartupPath + "\\btsk.xls"; //定义模板的路径 // Excel.Application app = new Excel.Application();//添加一个 Excle应用对象 // //打开工作簿,可见很多参数,第一个就是我们模板的路径 // //Excel._Workbook wbook = app.Workbooks.Open(null, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); // // Excel.Application.Workbooks.Add(true); // Microsoft.Office.Interop.Excel.Workbooks workbooks = app.Workbooks; // Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet); // Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1]; // Excel._Worksheet oSheet = (Excel._Worksheet)workbook.Worksheets[1];//创建一张sheet表 // int rowIndex=1; // int colIndex=0; // foreach(DataColumn col in dt.Columns) // { // colIndex++; // oSheet.Cells[1, colIndex] = col.ColumnName; // } // foreach(DataRow row in dt.Rows) // { // rowIndex++; // colIndex=0; // foreach(DataColumn col in dt.Columns) // { // colIndex++; // oSheet.Cells[rowIndex, colIndex] = row[col.ColumnName].ToString(); // } // } // app.Application.DisplayAlerts = false; // oSheet.SaveAs(localFilePath, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);//文件保存 // //打开后就要关闭。O(∩_∩)O~ // app.Workbooks.Close(); // //同样不要忘记结束进程 // System.Runtime.InteropServices.Marshal.ReleaseComObject(oSheet); // System.Runtime.InteropServices.Marshal.ReleaseComObject(app); // GC.Collect();//强制对所有代进行即时垃圾回收 // WaitFormService.CloseWaitForm(); // MessageBox.Show("导出完成!", "软件提示"); // } // catch (Exception ex) // { // WaitFormService.CloseWaitForm(); // MessageBox.Show("导出失败!" + ex.ToString(), "软件提示"); // return; // } //} }
private void button5_Click(object sender, EventArgs e) { // commUse.Excelout(this.ds); CommExcel.ExportExcel("", dataGridView1, true); }
private void button5_Click(object sender, EventArgs e) { CommExcel.ExportExcel("", dataGridView1, true); }