当前编码的一个项目中有把查询结果(显示在DataGrid)导出为excel的需求,尝试了几种方法,作为技巧拿来和大家分享。
内容: 服务器端实现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 } 在需要导出的地方调用上面的方法就可以。不过这样的实现有两个问题:第一,datagrid中不能包含模板列;第二,只能够导出当前显示在datagrid的数据,无法在分页的情况下导出全部的查询结果。
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/**//// <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 }
|
|
来自: 悟静 > 《.net和asp.net》