★★★★★FireGrass★★★★★

※※※※※ΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞジ
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

导出Excel的几种方式

Posted on 2006-08-07 21:11  火草  阅读(272)  评论(0)    收藏  举报

1.DataGrid直接导出

 1private void Button1_Click(object sender, System.EventArgs e)
 2  {
 3   DataTable dt =  new DataTable();
 4   dt.Columns.Add(new DataColumn("id",typeof(string)));
 5   dt.Columns.Add(new DataColumn("name",typeof(string)));
 6   dt.Columns.Add(new DataColumn("sex",typeof(string)));
 7   DataRow dr  = null;
 8   for(int i=0;i<1000;i++)
 9    {
10        dr = dt.NewRow();
11        dr["id"=i.ToString();
12        dr["name"= "ssss";
13        dr["sex"= "";
14        dt.Rows.Add(dr);
15   }

16   this.DataGrid1.DataSource = dt;
17   this.DataGrid1.DataBind();
18   HttpContext.Current.Response.Charset ="UTF-8";
19   Response.Charset = "GB2312"
20   HttpContext.Current.Response.ContentEncoding =System.Text.Encoding.Default;
21   HttpContext.Current.Response.ContentType ="application/ms-excel"
22   HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=a.xls" );
23   DataGrid1.Page.EnableViewState =false;
24   System.IO.StringWriter  tw = new System.IO.StringWriter();
25   System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
26   DataGrid1.RenderControl(hw);
27   HttpContext.Current.Response.Write(tw.ToString());
28   HttpContext.Current.Response.End();
29  }

30
2.DataTable导出
一.使用GC.Collect();释放对象

 

 1public string DataToExcel(DataTable dt,string path,string filename)
 2  {
 3   Excel.Application  excel = null;
 4   Excel.Workbook book = null;
 5   Excel.Worksheet sheet = null;
 6   try
 7   {
 8    GC.Collect();
 9    excel = new Excel.ApplicationClass();
10   
11    book = excel.Workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
12    sheet = (Excel.Worksheet)book.ActiveSheet;
13    int rowIndex=1;
14    int colIndex=1;
15    //表头
16    foreach(DataColumn col in dt.Columns)
17    {
18     excel.Cells[1,colIndex] = col.ColumnName.ToString();
19     //处理科学记数和计算问题
20     if(col.DataType == System.Type.GetType("System.Int16"|| col.DataType == System.Type.GetType("System.int32"|| col.DataType == System.Type.GetType("System.int64")  || col.DataType == System.Type.GetType("System.Decimal"))
21     {
22              sheet.get_Range(excel.Cells[1,colIndex],excel.Cells[dt.Rows.Count+1,colIndex]).NumberFormatLocal = "G/通用格式";
23         }

24         else
25     {
26              sheet.get_Range(excel.Cells[1,colIndex],excel.Cells[dt.Rows.Count+1,colIndex]).NumberFormatLocal = "@";
27         }

28         colIndex++;
29    }

30   
31    foreach(DataRow row in dt.Rows)
32    {
33         rowIndex ++;
34         colIndex = 1;
35         foreach(DataColumn col in dt.Columns)
36     {
37              if(col.DataType == System.Type.GetType("System.String"))
38      {
39                   excel.Cells[rowIndex,colIndex] = row[col.ColumnName].ToString();
40              }

41              else
42      {
43                   excel.Cells[rowIndex,colIndex] = row[col.ColumnName].ToString();
44              }

45              colIndex ++;
46         }

47    }

48
52    excel.DisplayAlerts = false;
53    System.Random rnd = new Random();
54    string fileName = filename + rnd.Next(100000000).ToString() + ".xls";
55    this._path = path + @"Report\"+fileName;
56    book.SaveCopyAs(this._path); 
57    return this._path;
58   }

59   catch(Exception ex)
60   {
61    throw ex;
62   }

63   finally
64   {
65    System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet);
66    System.Runtime.InteropServices.Marshal.ReleaseComObject(book);
67    excel.Quit();
68    System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
69    sheet = null;
70    book = null;
71    excel = null;
72    GC.Collect();
73   }

74  }

75
76

 二.不使用GC.Collect();释放对象