操作EXCEL(1)

原创 2006年05月29日 17:44:00

读取表内容到以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;
  }
 }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

ASP.NET服务端不装Office 操作Excel NPOI 1-2

作者:Tony Qu NPOI QQ交流群:  群1:78142590 (满) 群2:124527967 群3: 116053476(新) 更新 2009.3.24  把npoi 1....

VB.NET中操作Excel表实例1

  • 2009-02-27 11:00
  • 570KB
  • 下载

使用poi操作excel--1

1,常见用法 public class qq { public static void main(String args[]) throws Exception{ Workbook wb = ...

c# 操作excel(1)

0. 导入命名空间:  1 2 3 4 using Microsoft.Office.Core; using Microsoft...

通过NPOI 导出Excel 操作1

封装NPOI操作类,此类是通过NPOIHelper扩展来的 using System; using System.Collections.Generic; using System.Collec...

VC中操作Excel方法集锦(1)

1、MSDN   上的代码,很好的例子 #include   "excel8.h "     //   OLE   Variant   for   Optional.     &#...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)