现在的位置: 首页 > 综合 > 正文

操作EXCEL(1)

2013年03月01日 ⁄ 综合 ⁄ 共 2817字 ⁄ 字号 评论关闭

读取表内容到以EX CEL形式发送到客户端

 using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using System.IO;
using System.Text;
namespace ZilongComExcel
{
 /// <summary>
 /// ComExcel 的摘要说明。
 /// </summary>
 public class ComExcel:System.Web.UI.Page
 {
  public ComExcel()
  {
   //
   // TODO: 在此处添加构造函数逻辑
   //
  }
  public void ExportToExcel(Control ctl, string fileName, string encoding)
  {
   HttpContext context = HttpContext.Current;
   HttpResponse response = context.Response;
   response.Clear();
   response.Buffer = true;
   response.Charset = encoding;
   response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName) + ".xls");
   response.ContentEncoding = Encoding.GetEncoding(encoding);
   response.ContentType = "application/ms-excel";
   if (ctl.Page != null)
   {
    ctl.Page.EnableViewState = false;
    StringWriter sw = new StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(sw);
    this.ClearControls(ctl);
    ctl.RenderControl(htw);
    response.Write(sw.ToString());
    response.End();
    ctl.Page.EnableViewState = true;
   }
   else
   {
    StringWriter sw = new StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(sw);
    ctl.RenderControl(htw);
    response.Write(sw.ToString());
    response.End();
   }

  }

  public void ExportToExcel(Control ctl, string fileName)
  {
   //this.ExportToExcel( ctl,fileName,"utf-8");
   this.ExportToExcel(ctl, fileName, "gb2312");
  }
  public void ExportToExcel(DataTable dt, string fileName)
  {
   DataGrid dg = new DataGrid();
   dg.DataSource = dt.DefaultView;
   dg.AllowPaging = false;
   dg.DataBind();
   this.ExportToExcel(dg, fileName);
  }
  public void ExportToExcel(DataSet ds, string fileName, string tableName)
  {
   DataTable dt = null;
   if (tableName == "")
    dt = ds.Tables[0];
   else
    dt = ds.Tables[tableName];
   this.ExportToExcel(dt, fileName);
  }

  public void ExportToExcel(DataSet ds, string fileName)
  {
   this.ExportToExcel(ds, fileName, "");
  }
  private void ClearControls(Control control)
  {
   for (int i=control.Controls.Count -1; i>=0; i--)
   {
    ClearControls(control.Controls[i]);
   }

   if (!(control is TableCell))
   {
    if (control.GetType().GetProperty("SelectedItem") != null)
    {
     LiteralControl literal = new LiteralControl();
     control.Parent.Controls.Add(literal);
     try
     {
      literal.Text = (string)control.GetType().GetProperty("SelectedItem").GetValue(control,null);
     }
     catch
     {
     }
     control.Parent.Controls.Remove(control);
    }
    else
     if (control.GetType().GetProperty("Text") != null)
    {
     LiteralControl literal = new LiteralControl();
     control.Parent.Controls.Add(literal);
     literal.Text = (string)control.GetType().GetProperty("Text").GetValue(control,null);
     control.Parent.Controls.Remove(control);
    }
   }
   return;
  }
 }
}

抱歉!评论已关闭.