DataGrid導出到Excel的時候,某些數字内容顯示不正确,"3.62201E+14"的格式
WebForm中,在DataGrid的ItemDataBound事件内
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
e.Item.Cells[0].Attributes.Add("style","vnd.ms-excel.numberformat:@");
}
在WinForm内Excel.Range range = (Excel.Range)worksheet.Cells[1,1];
//設定單元格數字内容顯示格式
range.NumberFormat = Excel.XlParameterDataType.xlParamTypeUnknown;
------
//設定單元格内容自動換行
range.WrapText = true ;
//設定單元格内容水準對齊方式
range.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
//設定單元格内容豎直堆砌方式
range.VerticalAlignment=Excel.XlVAlign.xlVAlignCenter;
range.WrapText = true;
目前編碼的一個項目中有把查詢結果(顯示在DataGrid)導出為excel的需求,嘗試了幾種方法,作為技巧拿來和大家分享。
内容:
伺服器端實作DataGrid導出為excel
用戶端實作DataGrid導出為excel
伺服器端實作DataTable導出為excel(終極解決方案)
這是網上出現的最多的做法:
1
/// <summary>
2
/// 把DataGrid内容導出偉excel并傳回用戶端
3
/// </summary>
4
/// <param name="dgData">待導出的DataGrid</param>
5
/// 創 建 人:calvin
6
/// 建立日期:2005年10月08日
7
/// 修 改 人:
8
/// 修改日期:
9
public static void DataGrid2Excel(System.Web.UI.WebControls.DataGrid dgData)
10
{
11
// 目前對話
12
System.Web.HttpContext curContext = System.Web.HttpContext.Current;
13
// IO用于導出并傳回excel檔案
14
System.IO.StringWriter strWriter = null;
15
System.Web.UI.HtmlTextWriter htmlWriter = null;
16
17
if (dgData != null)
18
{
19
// 設定編碼和附件格式
20
curContext.Response.ContentType = "application/vnd.ms-excel";
21
curContext.Response.ContentEncoding =System.Text.Encoding.UTF8;
22
curContext.Response.Charset = "";
23
24
// 導出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
}
在需要導出的地方調用上面的方法就可以。不過這樣的實作有兩個問題:第一,datagrid中不能包含模闆列;第二,隻能夠導出目前顯示在datagrid的資料,無法在分頁的情況下導出全部的查詢結果。
如果大家分析一下Control.RenderControl的方法,就會發現RenderControl隻是把控件的innerHTML導出來,既然如此,完全可以把導出操作放在用戶端來處理。
用戶端導出excel
1 /*
2 * 将DataGrid導出為Excel檔案
3 *
4 * @param strTitle 檔案标題
5 * @param dgData 待導出的DataGrid
6 * @param iStartCol 起始列序号
7 * @param iEndCol 結束列序号
8 *
9 * 建立人: calvin
10 * 建立日期: 2005-10-08
11 * 修改人:
12 * 修改日期:
13 **/
14 function DataGrid2Excel(strTitle, dgData, iStartCol, iEndCol)
15 {
16 // 定義Excel Applicaiton Object
17 var appExcel = null;
18 // 目前激活的工作簿
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 // 擷取目前激活的工作部
36 currentWork = appExcel.Workbooks.Add();
37 currentSheet = currentWork.ActiveSheet;
38
39 // 填充excel内容
40 // 設定标題
41 currentSheet.Cells(1,1).Value = strTitle;
42 currentSheet.Cells(1,1).Value = dgData.innerText;
43 window.alert(dgData.innerHTML);
44
45 // 填充内容
46 for (var iRow = 0; iRow < dgData.rows.length - 1; iRow++)
47 {
48 // 顯示指定列的内容
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 }
下面是調用的例子
1 /**
2 * 導出dgData中0-3列的資料到excel檔案中
3 **/
4 function ToExcel()
5 {
6 DataGrid2Excel("使用javascript導出excel的例子", document.getElementsById("dgData"), 0, 3);
7 }
這種方法的缺點是:
(1)了能夠在用戶端調用Excel.Application,需要把IE的安全級别設為“低”。
(2)與方法一相同,還是隻能導出目前顯示在datagrid裡面的資料,無法導出分頁的資料。
終極解決方案:将DataTable導出為excel
好,讓我們快點結束這篇無聊的post。一般來說,頁面上的datagrid是以查詢得到的一個DataTable為資料源的。那麼為了把全部資料導入excel中,我們隻要把DataTable資料源輸出為excel就可以了。
/// 把DataTable内容導出偉excel并傳回用戶端
/// <param name="dgData">待導出的DataTable</param>
/// 創 建 人:陳文凱
public static void DataTable2Excel(System.Data.DataTable dtData)
System.Web.UI.WebControls.DataGrid dgExport = null;
if (dtData != null)
// 為了解決dgData中可能進行了分頁的情況,需要重新定義一個無分頁的DataGrid
dgExport = new System.Web.UI.WebControls.DataGrid();
dgExport.DataSource = dtData.DefaultView;
dgExport.AllowPaging = false;
dgExport.DataBind();
34
35
// 傳回用戶端
36
dgExport.RenderControl(htmlWriter);
37
38
39
40
需要注意的是,導出excel之前要把datatable的列名更改為客戶要求的文字,就ok了。因為是從DataTable導出的,是以這種方法解決了分頁資料的問題,堪稱終極解決方案
本文轉自高海東部落格園部落格,原文連結:XXXXX,如需轉載請自行聯系原作者