Jincw! A za A za Fighting!

既然选择了远方,便只顾风雨兼程!
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

如何将dataSet中的数据导入到Excel文件(*.xls)整理

Posted on 2006-04-20 01:44  西瓜K菠萝  阅读(1546)  评论(0编辑  收藏  举报

 

  1using System;
  2using System.Collections;
  3using System.ComponentModel;
  4using System.Data;
  5using System.Drawing;
  6using System.Web;
  7using System.Web.SessionState;
  8using System.Web.UI;
  9using System.Web.UI.WebControls;
 10using System.Web.UI.HtmlControls;
 11//*************************************
 12public class DataSetToExcel
 13 {
 14  public DataSetToExcel(){}
 15  public void Convert(DataSet oDS,HttpResponse Response)
 16  {
 17   Response.Clear();
 18   Response.Charset = "";
 19   Response.ContentType = "application/vnd.ms-excel";
 20   System.IO.StringWriter oSW = new System.IO.StringWriter();
 21   HtmlTextWriter oHW = new HtmlTextWriter(oSW);
 22   DataGrid oDG = new DataGrid();
 23   oDG.DataSource = oDS.Tables[0];
 24   oDG.DataBind();
 25   oDG.RenderControl(oHW);
 26   Response.Write(oSW.ToString());
 27   Response.Flush();
 28   Response.Close();
 29  }

 30 }

 31//*********************************************************
 32调用这个类就OK了
 33
 34 
 35
 362
 37
 38using System;
 39using System.Data;
 40using System.Data.OleDb;
 41namespace GRIS.ExcelReprot
 42{
 43 /// <summary>
 44 /// ImportExportToExcel 的摘要说明。
 45 /// </summary>

 46 public class ImportExportToExcel
 47 {
 48  private string strConn ;
 49        
 50  private System.Windows.Forms.OpenFileDialog openFileDlg=new System.Windows.Forms.OpenFileDialog();
 51  private System.Windows.Forms.SaveFileDialog saveFileDlg=new System.Windows.Forms.SaveFileDialog();      
 52 
 53  public ImportExportToExcel()
 54  {
 55   //
 56   // TODO: 在此处添加构造函数逻辑
 57   //
 58   this.openFileDlg.DefaultExt = "xls";
 59   this.openFileDlg.Filter = "Excel文件 (*.xls)|*.xls";
 60
 61   this.saveFileDlg.DefaultExt="xls";
 62   this.saveFileDlg.Filter= "Excel文件 (*.xls)|*.xls";
 63
 64  }

 65
 66从Excel文件导入到DataSet
125
126从DataSet到出到Excel
198
199从XML导入到Dataset
232
233从DataSet导出到XML
271 }

272}

273
274public void ExportResult(DataSet ds)
275        {
276            HttpContext.Current.Response.Clear();
277            HttpContext.Current.Response.Charset = "";
278            HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
279            StringWriter stringWrite = new StringWriter();
280            HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
281
282            DataGrid dg = new DataGrid();
283            dg.DataSource = ds.Tables[0];
284            dg.DataBind();
285            dg.RenderControl(htmlWrite);
286            HttpContext.Current.Response.AddHeader("content-disposition""attachment; filename=result.xls");
287
288            HttpContext.Current.Response.Write(stringWrite.ToString());
289            HttpContext.Current.Response.End();
290        }

291
292