public void ImproExcel() { SaveFileDialog sdfExport = new SaveFileDialog(); sdfExport.Filter = "Excel 97-2003文件|*.xls|Excel 2007文件|*.xlsx"; // sdfExport.ShowDialog(); if (sdfExport.ShowDialog() != DialogResult.OK) { return; } String filename = sdfExport.FileName; NPOIProgram NPOIexcel = new NPOIProgram(); DataTable tabl = new DataTable(); tabl = GetDgvToTable(dgvStyleCode); // DataTable dt = (StyleCodeInfodataGridView.DataSource as DataTable); NPOIexcel.ExcelWrite(filename, tabl);//excelhelper写出 if (MessageBox.Show("导出成功,文件保存在" + filename.ToString() + ",是否打开此文件?", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes) { if (File.Exists(filename)) //文件是否存在 { Process.Start(filename); //执行打开导出的文件 } else { MessageBox.Show("文件不存在!", "提示"); } } }
/// <summary> /// 导出整表 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnExportToExcel_Click(object sender, EventArgs e) { SaveFileDialog sdfExport = new SaveFileDialog(); sdfExport.Filter = "Excel 97-2003文件|*.xls|Excel 2007文件|*.xlsx"; // sdfExport.ShowDialog(); if (sdfExport.ShowDialog() != DialogResult.OK) { return; } String filename = sdfExport.FileName; NPOIProgram NPOIexcel = new NPOIProgram(); // DataTable tab1 = new DataTable(); #region 生成表 DataTable dt = new DataTable(); dt.Columns.Add("指令数"); //仓库指令数 dt.Columns.Add("指令数量"); //满箱指令数 dt.Columns.Add("箱数"); //未满指令数 dt.Columns.Add("箱数量"); //指令箱数 dt.Columns.Add("双数"); //仓库箱数 dt.Columns.Add("双数量"); //欠库箱数 dt.Columns.Add("单箱数"); //指令总双数 dt.Columns.Add("单箱数量"); //仓库总双数 dt.Columns.Add("单双数"); //总欠库双数 dt.Columns.Add("单双数量"); //单指令箱数 //给值 //行1 DataRow dtrow1 = dt.NewRow(); dtrow1["指令数"] = "仓库指令数"; dtrow1["指令数量"] = labsumcoutom.Text; dtrow1["箱数"] = "指令箱数"; dtrow1["箱数量"] = labsumcodebox.Text; dtrow1["双数"] = "指令总双数"; dtrow1["双数量"] = labcodecount.Text; dtrow1["单箱数"] = "单指令箱数"; dtrow1["单箱数量"] = labcustomcountbox.Text; dtrow1["单双数"] = "单指令双数"; dtrow1["单双数量"] = labcustomcount.Text; dt.Rows.Add(dtrow1); //行2 DataRow dtrow2 = dt.NewRow(); dtrow2["指令数"] = "满箱指令数"; dtrow2["指令数量"] = labsumokbox.Text; dtrow2["箱数"] = "仓库箱数"; dtrow2["箱数量"] = labsumbox.Text; dtrow2["双数"] = "仓库总双数"; dtrow2["双数量"] = labpdcount.Text; dtrow2["单箱数"] = "单指令库存箱"; dtrow2["单箱数量"] = labpdboxcount.Text; dtrow2["单双数"] = "单指令库存双"; dtrow2["单双数量"] = labsumcount.Text; dt.Rows.Add(dtrow2); //行3 DataRow dtrow3 = dt.NewRow(); dtrow3["指令数"] = "未满指令数"; dtrow3["指令数量"] = labsumnobox.Text; dtrow3["箱数"] = "欠库箱数"; dtrow3["箱数量"] = labsumcodenobox.Text; dtrow3["双数"] = "总欠库双数"; dtrow3["双数量"] = labnopdcount.Text; dtrow3["单箱数"] = "单指令欠库箱"; dtrow3["单箱数量"] = labpdnoboxcount.Text; dtrow3["单双数"] = "单指令欠库双"; dtrow3["单双数量"] = labnosumcount.Text; dt.Rows.Add(dtrow3); #endregion NPOIexcel.ExcelWrite(filename, dt);//excelhelper写出 if (MessageBox.Show("导出成功,文件保存在" + filename.ToString() + ",是否打开此文件?", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes) { if (File.Exists(filename)) //文件是否存在 { Process.Start(filename); //执行打开导出的文件 } else { MessageBox.Show("文件不存在!", "提示"); } } }