http://blog.csdn.net/cpcpc/article/details/7029763
下面给出一个在网上下载的一个已经封装好的类
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI.WebControls;
- using System.Web.UI;
- using System.Data;
- using System.Text;
- using System.Globalization;
- using System.IO;
-
- namespace VMS.Test.Classes
- {
- public class ExcelHelper {
-
- #region Fields
-
- string _fileName;
- DataTable _dataSource;
- string[] _titles = null;
- string[] _fields = null;
- int _maxRecords = 1000;
-
- #endregion
-
- #region Properties
-
- /// <summary>
-
-
- public int MaxRecords {
- set { _maxRecords = value; }
- get { return _maxRecords; }
- }
-
- /// <summary>
-
-
- public string FileName {
- set { _fileName = value; }
- get { return _fileName; }
- }
-
- #endregion
-
- #region .ctor
-
- /// <summary>
-
-
-
-
-
- public ExcelHelper(string[] titles, string[] fields, DataTable dataSource): this(titles, dataSource) {
- if (fields == null || fields.Length == 0)
- throw new ArgumentNullException("fields");
-
- if (titles.Length != fields.Length)
- throw new ArgumentException("titles.Length != fields.Length", "fields");
-
- _fields = fields;
- }
-
- /// <summary>
-
-
-
-
- public ExcelHelper(string[] titles, DataTable dataSource): this(dataSource) {
- if (titles == null || titles.Length == 0)
- throw new ArgumentNullException("titles");
-
-
-
- _titles = titles;
- }
-
- /// <summary>
-
-
-
- public ExcelHelper(DataTable dataSource) {
- if (dataSource == null)
- throw new ArgumentNullException("dataSource");
-
-
-
- _dataSource = dataSource;
- }
-
- public ExcelHelper() {}
-
- #endregion
-
- #region public Methods
-
- /// <summary>
-
-
-
- public void Export(DataGrid dg) {
- if (dg == null)
- throw new ArgumentNullException("dg");
- if (dg.AllowPaging || dg.PageCount > 1)
- throw new ArgumentException("paged DataGrid can't be exported.", "dg");
-
-
- dg.HeaderStyle.Font.Bold = true;
- dg.HeaderStyle.BackColor = System.Drawing.Color.LightGray;
-
- RenderExcel(dg);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- /// <summary>
-
-
- public void Export() {
- if (_dataSource == null)
- throw new Exception("数据源尚未初始化");
-
- if (_fields == null && _titles != null && _titles.Length != _dataSource.Columns.Count)
- throw new Exception("_titles.Length != _dataSource.Columns.Count");
-
- if (_dataSource.Rows.Count > _maxRecords)
- throw new Exception("导出数据条数超过限制。请设置 MaxRecords 属性以定义导出的最多记录数。");
-
- DataGrid dg = new DataGrid();
- dg.DataSource = _dataSource;
-
- if (_titles == null) {
- dg.AutoGenerateColumns = true;
- }
- else {
- dg.AutoGenerateColumns = false;
- int cnt = _titles.Length;
-
- System.Web.UI.WebControls.BoundColumn col;
-
- if (_fields == null) {
- for (int i=0; i<cnt; i++) {
- col = new System.Web.UI.WebControls.BoundColumn();
- col.HeaderText = _titles[i];
- col.DataField = _dataSource.Columns[i].ColumnName;
- dg.Columns.Add(col);
- }
- }
- else {
- for (int i=0; i<cnt; i++) {
- col = new System.Web.UI.WebControls.BoundColumn();
- col.HeaderText = _titles[i];
- col.DataField = _fields[i];
- dg.Columns.Add(col);
- }
- }
- }
-
-
- dg.HeaderStyle.Font.Bold = true;
- dg.HeaderStyle.BackColor = System.Drawing.Color.LightGray;
- dg.ItemDataBound += new DataGridItemEventHandler(DataGridItemDataBound);
-
- dg.DataBind();
- RenderExcel(dg);
- }
-
- #endregion
-
- #region private Methods
-
- private void RenderExcel(Control c) {
-
- if (_fileName == null || _fileName == string.Empty || !(_fileName.ToLower().EndsWith(".xls")))
- _fileName = GetRandomFileName();
-
- HttpResponse response = HttpContext.Current.Response;
-
- response.Charset = "GB2312";
- response.ContentEncoding = Encoding.GetEncoding("GB2312");
- response.ContentType = "application/ms-excel/msword";
- response.AppendHeader("Content-Disposition", "attachment;filename=" +
- HttpUtility.UrlEncode(_fileName));
-
- CultureInfo cult = new CultureInfo("zh-CN", true);
- StringWriter sw = new StringWriter(cult);
- HtmlTextWriter writer = new HtmlTextWriter(sw);
-
- writer.WriteLine("<meta http-equiv=\"Content-Type\" content=\"text/html;charset=GB2312\">");
-
- DataGrid dg = c as DataGrid;
-
- if (dg != null) {
- dg.RenderControl(writer);
- }
- else {
- DataGrid xgrid = c as DataGrid;
-
- if (xgrid != null)
- xgrid.RenderControl(writer);
- else
- throw new ArgumentException("only supports DataGrid or ASPxGrid.", "c");
- }
- c.Dispose();
-
- response.Write(sw.ToString());
- response.End();
- }
-
-
- /// <summary>
-
-
-
- private string GetRandomFileName() {
- Random rnd = new Random((int) (DateTime.Now.Ticks));
- string s = rnd.Next(Int32.MaxValue).ToString();
- return DateTime.Now.ToShortDateString() + "_" + s + ".xls";
- }
-
- private void DataGridItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) {
- if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
- e.Item.Attributes.Add("style", "vnd.ms-excel.numberformat:@");
-
- }
- }
- #endregion
- }
- }
|
请发表评论