1private void ExportExcel(System.Data.DataTable ds, string strExcelFileName)
2 {
3 object objOpt = Missing.Value;
4 Excel.Application excel = new Excel.Application();
5 excel.Visible = true;
6 _Workbook wkb = excel.Workbooks.Add(objOpt);
7 _Worksheet wks = (_Worksheet)wkb.ActiveSheet;
8
9 wks.Visible = XlSheetVisibility.xlSheetVisible;
10
11 int rowIndex = 1;
12 int colIndex = 0;
13
14 System.Data.DataTable table = ds;
15 foreach (DataColumn col in table.Columns)
16 {
17 colIndex++;
18 excel.Cells[1, colIndex] = col.ColumnName;
19 }
20
21 foreach (DataRow row in table.Rows)
22 {
23 rowIndex++;
24 colIndex = 0;
25 foreach (DataColumn col in table.Columns)
26 {
27 colIndex++;
28 excel.Cells[rowIndex, colIndex] = row[col.ColumnName].ToString();
29 }
30 }
31 try
32 {
33 wkb.SaveAs(string.Format("{0}\\{1}",System.Windows.Forms.Application.StartupPath,strExcelFileName), objOpt, null, null, false, false, XlSaveAsAccessMode.xlNoChange, ConflictOption.OverwriteChanges, null, null, null,null);
34
35 }
36 catch
37 {
38 }
39 finally
40 {
41 wkb.Close(false, objOpt, objOpt);
42 excel.Quit();
43 }
44
45 //excel.Sheets[0] = "sss";
46 }
47
48 private void tsbtnExportAll_Click(object sender, EventArgs e)
49 {
50 if (dgvList.Rows.Count <= 0)
51 return;
52 ExportExcel(dsForExport.akGetAllData(), "出库明细表.xls");
53 if (File.Exists(string.Format("{0}\\{1}", System.Windows.Forms.Application.StartupPath, "出库明细表.xls")))
54 System.Diagnostics.Process.Start(string.Format("{0}\\{1}", System.Windows.Forms.Application.StartupPath, "出库明细表.xls"));
55
56 }
以上方法需添加引用:
using Excel;
using System.Reflection;
using System.IO;
2 {
3 object objOpt = Missing.Value;
4 Excel.Application excel = new Excel.Application();
5 excel.Visible = true;
6 _Workbook wkb = excel.Workbooks.Add(objOpt);
7 _Worksheet wks = (_Worksheet)wkb.ActiveSheet;
8
9 wks.Visible = XlSheetVisibility.xlSheetVisible;
10
11 int rowIndex = 1;
12 int colIndex = 0;
13
14 System.Data.DataTable table = ds;
15 foreach (DataColumn col in table.Columns)
16 {
17 colIndex++;
18 excel.Cells[1, colIndex] = col.ColumnName;
19 }
20
21 foreach (DataRow row in table.Rows)
22 {
23 rowIndex++;
24 colIndex = 0;
25 foreach (DataColumn col in table.Columns)
26 {
27 colIndex++;
28 excel.Cells[rowIndex, colIndex] = row[col.ColumnName].ToString();
29 }
30 }
31 try
32 {
33 wkb.SaveAs(string.Format("{0}\\{1}",System.Windows.Forms.Application.StartupPath,strExcelFileName), objOpt, null, null, false, false, XlSaveAsAccessMode.xlNoChange, ConflictOption.OverwriteChanges, null, null, null,null);
34
35 }
36 catch
37 {
38 }
39 finally
40 {
41 wkb.Close(false, objOpt, objOpt);
42 excel.Quit();
43 }
44
45 //excel.Sheets[0] = "sss";
46 }
47
48 private void tsbtnExportAll_Click(object sender, EventArgs e)
49 {
50 if (dgvList.Rows.Count <= 0)
51 return;
52 ExportExcel(dsForExport.akGetAllData(), "出库明细表.xls");
53 if (File.Exists(string.Format("{0}\\{1}", System.Windows.Forms.Application.StartupPath, "出库明细表.xls")))
54 System.Diagnostics.Process.Start(string.Format("{0}\\{1}", System.Windows.Forms.Application.StartupPath, "出库明细表.xls"));
55
56 }
以上方法需添加引用:
using Excel;
using System.Reflection;
using System.IO;