将DataGrid导出为Excel文件

该博客为转载内容,转载自https://www.cnblogs.com/jeky/archive/2005/12/18/299405.html ,涉及数据库相关知识。
可以使用这个类来实现此功能:

None.gifusing System.IO;
None.gif
using System.Text;
None.gif
using System.Web;
None.gif
using System.Web.UI;
None.gif
None.gif
namespace Jeky.Web
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 输入文件的类型。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public enum ExportType
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 格式:*.htm、*.html等。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        Html,
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 格式:*.doc。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        Word,
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 格式:*.xls。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        Excel,
ExpandedSubBlockEnd.gif    }
 ;
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 有关文件的一些操作方法。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class FileHandle
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 初始化 <see cref="Jeky.Web.FileHandle">FileHandle</see> 类的新实例。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public FileHandle()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

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

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

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

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

None.gif// 在 DataBound 事件中处理
None.gif
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
InBlock.gif    e.Item.Cells[
2].Attributes.Add("style""vnd.ms-excel.numberformat:@");
ExpandedBlockEnd.gif}
 

转载于:https://www.cnblogs.com/jeky/archive/2005/12/18/299405.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值