将DataGrid导出为Excel文件

可以使用这个类来实现此功能:

using System.IO;
using System.Text;
using System.Web;
using System.Web.UI;

namespace Jeky.Web
{
    
/// <summary>
    
/// 输入文件的类型。
    
/// </summary>

    public enum ExportType
    
{
        
/// <summary>
        
/// 格式:*.htm、*.html等。
        
/// </summary>

        Html,
        
/// <summary>
        
/// 格式:*.doc。
        
/// </summary>

        Word,
        
/// <summary>
        
/// 格式:*.xls。
        
/// </summary>

        Excel,
    }
 ;


    
/// <summary>
    
/// 有关文件的一些操作方法。
    
/// </summary>

    public class FileHandle
    
{
        
/// <summary>
        
/// 初始化 <see cref="Jeky.Web.FileHandle">FileHandle</see> 类的新实例。
        
/// </summary>

        public FileHandle()
        
{
        }


        
/// <summary>
        
/// 将 Control 信息导出为某种特定类型的文件。
        
/// </summary>
        
/// <param name="ctl">Control,如 DataGrid 对象。</param>
        
/// <param name="strFileName">导出后的文件名称。</param>
        
/// <param name="et">导出类型。</param>
        
/// <example>将 Customers 表中的一组数据导出为 Word 文档
        
/// <code escaped="true">
        
/// void bt_click(object sender,EventArgs e){
        
///        SqlType ST=new SqlType();
        
///        Common CM=new Common();
        
///        // 创建一个 SqlConnection 对象
        
///        SqlConnection conn=ST.GetConn("server=localhost; database=Northwind; uid=sa; pwd=",false);
        
///        conn.Open();
        
///        string strSql="Select Top 10 CustomerID,CompanyName,ContactName,Address,City From Customers";
        
///        SqlCommand cmd=new SqlCommand(strSql,conn);
        
///        // 设置 DataGird 对象的数据源
        
///        myDataGrid.DataSource=cmd.ExecuteReader();
        
///        // 绑定信息
        
///        myDataGrid.DataBind();
        
///        conn.Close();
        
///        // 将 DataGrid 对象中的信息导出为指定格式的文件
        
///        CM.ToExportFile(myDataGrid,"Customers.doc",ExportType.Word);
        
/// }
        
/// </code>
        
/// </example>
        
/// <remarks>
        
/// 一、当对象(如DataGrid)有分页时,该方法会出现错误。
        
/// 二、当数据集中有不能认别的符号时,可能会出现乱码,没法处理。
        
/// </remarks>

        public static void ToExportFile(Control ctl, string strFileName, ExportType et)
        
{
            
string[] ETArray = new string[]
                
{
                    
"text/HTML""application/msword""application/ms-excel"
                }
;
            HttpContext.Current.Response.AppendHeader(
"Content-Disposition""attachment;filename=" + HttpUtility.UrlEncode(strFileName)); // 使用 UrlEncode 方法可以正确地输出中文名称。
            HttpContext.Current.Response.Charset = "UTF-8";
            HttpContext.Current.Response.ContentEncoding 
= Encoding.Default;
            HttpContext.Current.Response.ContentType 
= ETArray[(int) et];
            ctl.Page.EnableViewState 
= false;
            StringWriter tw 
= new StringWriter();
            HtmlTextWriter hw 
= new HtmlTextWriter(tw);
            ctl.RenderControl(hw);
            HttpContext.Current.Response.Write(tw.ToString());
            HttpContext.Current.Response.End();
        }

    }

}

另外,如果DataGrid中存在较长的数字值,例如“身份证”字段时,导出的Excel可能会以下格式出现:4.11303E+14,解决方法:

// 在 DataBound 事件中处理
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) 

    e.Item.Cells[
2].Attributes.Add("style""vnd.ms-excel.numberformat:@");
}
 
posted @ 2005-12-18 03:09  jeky  阅读(657)  评论(0编辑  收藏  举报
友情链接:逗死了笑话网 | 网络记事本