1//将datagrid中的数据文件保存到excel表中
2 public void ExportToExcel(System.Data.DataSet dstReport ,string strName)
3 {
4 //导出报表的时候判断以下看yld目录是否存在
5// //string strDir=System.Windows.Forms.Application.StartupPath+"\\"+strName;
6 string strPath;
7// if (!Directory.Exists(strDir))
8// {
9// Directory.CreateDirectory(strDir);
10// }
11
12 //创建一个Excel文件
13 Excel.Application myExcel=new Excel.Application();
14 Excel.Workbook oBook;
15 // Excel.Workbooks oBooks;
16 Excel.Sheets oSheets ;
17 Excel.Worksheet oSheet;
18 Excel.Range oCells;
19 oBook=myExcel.Application.Workbooks.Add(true);
20
21 oSheets=oBook.Worksheets;
22 oSheet=(Excel.Worksheet)oSheets[1];
23 oCells=oSheet.Cells ;
24
25
26 //让excel不可见
27 myExcel.Visible=false;
28
29 myExcel.DisplayAlerts = false;
30
31 //第一行为报表名称
32 oCells.Cells[1,4]=strName;
33 //逐行写入报表
34 System.Data.DataTable dtReport=dstReport.Tables[0];
35 //逐行写入报表
36 //打印列名
37
38 //循环打印列名
39 int i=0,j=0;
40 for(i=1;i<dstReport.Tables[0].Columns.Count;i++)
41 {
42 oCells.Cells[2,i]=dstReport.Tables[0].Columns[i-1].ColumnName;
43 }
44
45 //打印内容,最后一行列合计不转换
46 for( i=0;i<=dstReport.Tables[0].Rows.Count-1;i++)
47 {
48 for(j=1;j<=dstReport.Tables[0].Columns.Count-1;j++)
49 {
50 oCells.Cells[i+3,j]=dstReport.Tables[0].Rows[i][j-1].ToString().Trim();
51 }
52 }
53
54 strPath=Path.GetTempPath()+strName+".xls";
55
56 if (File.Exists(strPath)) //如果存在该文件则删除
57 {
58 File.Delete(strPath);
59
60 }
61
62
63 oSheet.SaveAs(strPath,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value);
64
65
66
67 myExcel.Quit();
68 Marshal.ReleaseComObject(oCells);
69 oCells=null;
70 Marshal.ReleaseComObject(oSheet);
71 oSheet=null;
72 Marshal.ReleaseComObject(oSheets);
73 oSheets=null;
74 Marshal.ReleaseComObject(oBook);
75 oBook=null;
76
77 Marshal.ReleaseComObject(myExcel);
78 myExcel=null;
79
80 System.GC.Collect();
81 System.Diagnostics.Process.Start(strPath);
82
83 }
2 public void ExportToExcel(System.Data.DataSet dstReport ,string strName)
3 {
4 //导出报表的时候判断以下看yld目录是否存在
5// //string strDir=System.Windows.Forms.Application.StartupPath+"\\"+strName;
6 string strPath;
7// if (!Directory.Exists(strDir))
8// {
9// Directory.CreateDirectory(strDir);
10// }
11
12 //创建一个Excel文件
13 Excel.Application myExcel=new Excel.Application();
14 Excel.Workbook oBook;
15 // Excel.Workbooks oBooks;
16 Excel.Sheets oSheets ;
17 Excel.Worksheet oSheet;
18 Excel.Range oCells;
19 oBook=myExcel.Application.Workbooks.Add(true);
20
21 oSheets=oBook.Worksheets;
22 oSheet=(Excel.Worksheet)oSheets[1];
23 oCells=oSheet.Cells ;
24
25
26 //让excel不可见
27 myExcel.Visible=false;
28
29 myExcel.DisplayAlerts = false;
30
31 //第一行为报表名称
32 oCells.Cells[1,4]=strName;
33 //逐行写入报表
34 System.Data.DataTable dtReport=dstReport.Tables[0];
35 //逐行写入报表
36 //打印列名
37
38 //循环打印列名
39 int i=0,j=0;
40 for(i=1;i<dstReport.Tables[0].Columns.Count;i++)
41 {
42 oCells.Cells[2,i]=dstReport.Tables[0].Columns[i-1].ColumnName;
43 }
44
45 //打印内容,最后一行列合计不转换
46 for( i=0;i<=dstReport.Tables[0].Rows.Count-1;i++)
47 {
48 for(j=1;j<=dstReport.Tables[0].Columns.Count-1;j++)
49 {
50 oCells.Cells[i+3,j]=dstReport.Tables[0].Rows[i][j-1].ToString().Trim();
51 }
52 }
53
54 strPath=Path.GetTempPath()+strName+".xls";
55
56 if (File.Exists(strPath)) //如果存在该文件则删除
57 {
58 File.Delete(strPath);
59
60 }
61
62
63 oSheet.SaveAs(strPath,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value);
64
65
66
67 myExcel.Quit();
68 Marshal.ReleaseComObject(oCells);
69 oCells=null;
70 Marshal.ReleaseComObject(oSheet);
71 oSheet=null;
72 Marshal.ReleaseComObject(oSheets);
73 oSheets=null;
74 Marshal.ReleaseComObject(oBook);
75 oBook=null;
76
77 Marshal.ReleaseComObject(myExcel);
78 myExcel=null;
79
80 System.GC.Collect();
81 System.Diagnostics.Process.Start(strPath);
82
83 }