當(dāng)前編碼的一個(gè)項(xiàng)目中有把查詢結(jié)果(顯示在DataGrid)導(dǎo)出為excel的需求,嘗試了幾種方法,作為技巧拿來和大家分享。
內(nèi)容:
服務(wù)器端實(shí)現(xiàn)DataGrid導(dǎo)出為excel
客戶端實(shí)現(xiàn)DataGrid導(dǎo)出為excel
服務(wù)器端實(shí)現(xiàn)DataTable導(dǎo)出為excel(終極解決方案)
服務(wù)器端實(shí)現(xiàn)DataGrid導(dǎo)出為excel
這是網(wǎng)上出現(xiàn)的最多的做法:
1/**//// <summary>
2 /// 把DataGrid內(nèi)容導(dǎo)出偉excel并返回客戶端
3 /// </summary>
4 /// <param name="dgData">待導(dǎo)出的DataGrid</param>
5 /// 創(chuàng) 建 人:calvin
6 /// 創(chuàng)建日期:2005年10月08日
7 /// 修 改 人:
8 /// 修改日期: 9 public static void DataGrid2Excel(System.Web.UI.WebControls.DataGrid dgData)
10 {
11 // 當(dāng)前對(duì)話
12 System.Web.HttpContext curContext = System.Web.HttpContext.Current;
13 // IO用于導(dǎo)出并返回excel文件
14 System.IO.StringWriter strWriter = null;
15 System.Web.UI.HtmlTextWriter htmlWriter = null;
16
17 if (dgData != null)
18 {
19 // 設(shè)置編碼和附件格式
20 curContext.Response.ContentType = "application/vnd.ms-excel";
21 curContext.Response.ContentEncoding =System.Text.Encoding.UTF8;
22 curContext.Response.Charset = "";
23
24 // 導(dǎo)出excel文件
25 strWriter = new System.IO.StringWriter();
26 htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);
27
28 // 返回客戶端
29 dgData.RenderControl(htmlWriter);
30 curContext.Response.Write(strWriter.ToString());
31 curContext.Response.End();
32 }
33 } 在需要導(dǎo)出的地方調(diào)用上面的方法就可以。不過這樣的實(shí)現(xiàn)有兩個(gè)問題:第一,datagrid中不能包含模板列;第二,只能夠?qū)С霎?dāng)前顯示在datagrid的數(shù)據(jù),無法在分頁的情況下導(dǎo)出全部的查詢結(jié)果。
如果大家分析一下Control.RenderControl的方法,就會(huì)發(fā)現(xiàn)RenderControl只是把控件的innerHTML導(dǎo)出來,既然如此,完全可以把導(dǎo)出操作放在客戶端來處理。
客戶端導(dǎo)出excel
1 /*
2 * 將DataGrid導(dǎo)出為Excel文件
3 *
4 * @param strTitle 文件標(biāo)題
5 * @param dgData 待導(dǎo)出的DataGrid
6 * @param iStartCol 起始列序號(hào)
7 * @param iEndCol 結(jié)束列序號(hào)
8 *
9 * 創(chuàng)建人: calvin
10 * 創(chuàng)建日期: 2005-10-08
11 * 修改人:
12 * 修改日期:
13 **/
14 function DataGrid2Excel(strTitle, dgData, iStartCol, iEndCol)
15 {
16 // 定義Excel Applicaiton Object
17 var appExcel = null;
18 // 當(dāng)前激活的工作簿
19 var currentWork = null;
20 var currentSheet = null;
21
22 try
23 {
24 // 初始化application
25 appExcel = new ActiveXObject("Excel.Application");
26 appExcel.Visible = true;
27 }
28 catch(e)
29 {
30 window.alert("Please Install Excel First");
31
32 return;
33 }
34
35 // 獲取當(dāng)前激活的工作部
36 currentWork = appExcel.Workbooks.Add();
37 currentSheet = currentWork.ActiveSheet;
38
39 // 填充excel內(nèi)容
40 // 設(shè)置標(biāo)題
41 currentSheet.Cells(1,1).Value = strTitle;
42 currentSheet.Cells(1,1).Value = dgData.innerText;
43 window.alert(dgData.innerHTML);
44
45 // 填充內(nèi)容
46 for (var iRow = 0; iRow < dgData.rows.length - 1; iRow++)
47 {
48 // 顯示指定列的內(nèi)容
49 for (var iCol = iStartCol; iCol <= iEndCol; iCol++)
50 {
51 currentSheet.Cells(iRow + 2, iCol + 1).Value =
52 dgData.rows[iRow].cells[iCol].innerText;
53 }
54 }
55 }
下面是調(diào)用的例子
1 /**
2 * 導(dǎo)出dgData中0-3列的數(shù)據(jù)到excel文件中
3 **/
4 function ToExcel()
5 {
6 DataGrid2Excel("使用javascript導(dǎo)出excel的例子", document.getElementsById("dgData"), 0, 3);
7 }
這種方法的缺點(diǎn)是:
(1)了能夠在客戶端調(diào)用Excel.Application,需要把IE的安全級(jí)別設(shè)為“低”。
(2)與方法一相同,還是只能導(dǎo)出當(dāng)前顯示在datagrid里面的數(shù)據(jù),無法導(dǎo)出分頁的數(shù)據(jù)。
終極解決方案:將DataTable導(dǎo)出為excel
好,讓我們快點(diǎn)結(jié)束這篇無聊的post。一般來說,頁面上的datagrid是以查詢得到的一個(gè)DataTable為數(shù)據(jù)源的。那么為了把全部數(shù)據(jù)導(dǎo)入excel中,我們只要把DataTable數(shù)據(jù)源輸出為excel就可以了。
1/**//// <summary>
2 /// 把DataTable內(nèi)容導(dǎo)出偉excel并返回客戶端
3 /// </summary>
4 /// <param name="dgData">待導(dǎo)出的DataTable</param>
5 /// 創(chuàng) 建 人:陳文凱
6 /// 創(chuàng)建日期:2005年10月08日
7 /// 修 改 人:
8 /// 修改日期: 9 public static void DataTable2Excel(System.Data.DataTable dtData)
10 {
11 System.Web.UI.WebControls.DataGrid dgExport = null;
12 // 當(dāng)前對(duì)話
13 System.Web.HttpContext curContext = System.Web.HttpContext.Current;
14 // IO用于導(dǎo)出并返回excel文件
15 System.IO.StringWriter strWriter = null;
16 System.Web.UI.HtmlTextWriter htmlWriter = null;
17
18 if (dtData != null)
19 {
20 // 設(shè)置編碼和附件格式
21 curContext.Response.ContentType = "application/vnd.ms-excel";
22 curContext.Response.ContentEncoding =System.Text.Encoding.UTF8;
23 curContext.Response.Charset = "";
24
25 // 導(dǎo)出excel文件
26 strWriter = new System.IO.StringWriter();
27 htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);
28
29 // 為了解決dgData中可能進(jìn)行了分頁的情況,需要重新定義一個(gè)無分頁的DataGrid
30 dgExport = new System.Web.UI.WebControls.DataGrid();
31 dgExport.DataSource = dtData.DefaultView;
32 dgExport.AllowPaging = false;
33 dgExport.DataBind();
34
35 // 返回客戶端
36 dgExport.RenderControl(htmlWriter);
37 curContext.Response.Write(strWriter.ToString());
38 curContext.Response.End();
39 }
40 }
需要注意的是,導(dǎo)出excel之前要把datatable的列名更改為客戶要求的文字,就ok了。因?yàn)槭菑腄ataTable導(dǎo)出的,所以這種方法解決了分頁數(shù)據(jù)的問題,堪稱終極解決方案。