白开心

  IT博客 :: 首页 ::  :: 联系 :: 聚合  :: 管理 ::
  9 随笔 :: 76 文章 :: 28 评论 :: 0 Trackbacks
#region 导出 DataTable 到 Excel 文档中 ------
        
/// <summary>
        
/// DataTable 导出为 Excel
        
/// </summary>
        
/// <param name="dtSource">DataTable 数据源</param>
        
/// <param name="fileName">导出的 Excel 文件名称</param>
        protected void ExportTableToExcel(DataTable dtSource, string fileName)
        {
            ExportTableToExcel(dtSource, fileName, 
null);
        }

        
/// <summary>
        
/// DataTable 导出为 Excel
        
/// </summary>
        
/// <param name="dtSource">DataTable 数据源</param>
        
/// <param name="fileName">导出的 Excel 文件名称</param>
        
/// <param name="itemCreatedAction">DataGrid的ItemCreated事件</param>
        
/// <param name="action">封装的一个方法</param>
        protected void ExportTableToExcel(DataTable dtSource, string fileName, DataGridItemEventHandler itemCreatedAction, Action<DataGrid> action)
        {
            DataGrid grid 
= new DataGrid();
            grid.ItemCreated 
+= itemCreatedAction;
            grid.DataSource 
= dtSource.DefaultView;   //输出DataTable的内容
            grid.DataBind();
            ExportTableToExcel(grid, fileName, action);
        }

        
/// <summary>
        
/// DataTable 导出为 Excel
        
/// </summary>
        
/// <param name="dtSource">DataTable 数据源</param>
        
/// <param name="fileName">导出的 Excel 文件名称</param>
        
/// <param name="itemCreatedAction">DataGrid的ItemCreated事件</param>
        protected void ExportTableToExcel(DataTable dtSource, string fileName, DataGridItemEventHandler itemCreatedAction)
        {
            ExportTableToExcel(dtSource, fileName, itemCreatedAction, 
null);
        }

        
/// <summary>
        
/// DataTable 导出为 Excel
        
/// </summary>
        
/// <param name="dtSource">DataTable 数据源</param>
        
/// <param name="fileName">导出的 Excel 文件名称</param>
        protected void ExportTableToExcel(DataGrid grid, string fileName)
        {
            ExportTableToExcel(grid, fileName, 
null);
        }

        
/// <summary>
        
/// DataTable 导出为 Excel
        
/// </summary>
        
/// <param name="dtSource">DataTable 数据源</param>
        
/// <param name="fileName">导出的 Excel 文件名称</param>
        
/// <param name="DataGrid">封装的一个方法</param>
        protected void ExportTableToExcel(DataGrid grid, string fileName, Action<DataGrid> action)
        {
            ExportTableToExcel(grid, fileName, 
string.Empty, action);
        }

        
/// <summary>
        
/// DataTable 导出为 Excel
        
/// </summary>
        
/// <param name="dtSource">DataTable 数据源</param>
        
/// <param name="fileName">导出的 Excel 文件名称</param>
        
/// <param name="coding">编码</param>
        
/// <param name="DataGrid">封装的一个方法</param>
        protected void ExportTableToExcel(DataGrid grid, string fileName, string coding, Action<DataGrid> action)
        {
            
if (coding == string.Empty)
            {
                coding 
= "UTF-8"//GB2312,UTF-8
            }
            System.Text.Encoding code;
            
try
            {
                code 
= System.Text.Encoding.GetEncoding(coding);

                
// 设置导出的 DataGrid 样式
                grid.GridLines = GridLines.Both;
                grid.HeaderStyle.Font.Bold 
= true;
                
if (action != null)
                {
                    action(grid);
                }

                System.IO.StringWriter stringWriter 
= new System.IO.StringWriter();
                HtmlTextWriter htmlWriter 
= new HtmlTextWriter(stringWriter);
                

                
//if (Path.GetExtension(fileName).ToLower() != ".xls")
                
//{
                
//    fileName = Path.GetFileNameWithoutExtension(fileName) + ".xls";
                
//}
                
//fileName = HttpUtility.UrlEncode(fileName);//, code);
                
//Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName + ";");
                
//HttpContext.Current.Response.ContentType = "application/ms-excel";
                HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"// 要么使用这个方式,要么使用以上注释的两行。
                Response.Charset = "";  //GB2312
                Response.ContentEncoding = code;// System.Text.Encoding.Default;// System.Text.Encoding.GetEncoding("GB2312");
                this.EnableViewState = false;
                grid.RenderControl(htmlWriter);
                Response.Write(stringWriter.ToString());
            }
            
catch (Exception ex)
            {
                
string msg = ex.Message;
                
return;
            }
            HttpContext.Current.Response.End();
        }


        
#endregion

posted on 2010-06-24 15:24 白开心 阅读(1412) 评论(0)  编辑 收藏 引用 所属分类: .Net(学习ing...)
只有注册用户登录后才能发表评论。