






















内容:
服务器端实现DataGrid导出为excel
客户端实现DataGrid导出为excel
服务器端实现DataTable导出为excel(终极解决方案)
服务器端实现DataGrid导出为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
}

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

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 }
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 }
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就可以了。
1
/// <summary>
2
/// 把DataTable内容导出伟excel并返回客户端
3
/// </summary>
4
/// <param name="dgData">待导出的DataTable</param>
5
/// 创 建 人:陈文凯
6
/// 创建日期:2005年10月08日
7
/// 修 改 人:
8
/// 修改日期:
9
public static void DataTable2Excel(System.Data.DataTable dtData)
10
{
11
System.Web.UI.WebControls.DataGrid dgExport = null;
12
// 当前对话
13
System.Web.HttpContext curContext = System.Web.HttpContext.Current;
14
// IO用于导出并返回excel文件
15
System.IO.StringWriter strWriter = null;
16
System.Web.UI.HtmlTextWriter htmlWriter = null;
17
18
if (dtData != null)
19
{
20
// 设置编码和附件格式
21
curContext.Response.ContentType = "application/vnd.ms-excel";
22
curContext.Response.ContentEncoding =System.Text.Encoding.UTF8;
23
curContext.Response.Charset = "";
24
25
// 导出excel文件
26
strWriter = new System.IO.StringWriter();
27
htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);
28
29
// 为了解决dgData中可能进行了分页的情况,需要重新定义一个无分页的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
}

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

需要注意的是,导出excel之前要把datatable的列名更改为客户要求的文字,就ok了。因为是从DataTable导出的,所以这种方法解决了分页数据的问题,堪称终极解决方案。