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

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#region 从DataSet到出到Excel
127   /**//// <summary>
128   /// 导出指定的Excel文件
129   /// </summary>
130   /// <param name="ds">要导出的DataSet</param>
131   /// <param name="strExcelFileName">要导出的Excel文件名</param>

132   public void ExportToExcel(DataSet ds,string strExcelFileName)
133   
137  /**//// <summary>
138  /// 导出用户选择的Excel文件
139  /// </summary>
140  /// <param name="ds">DataSet</param>

141  public void ExportToExcel(DataSet ds)
142  {
143   if (saveFileDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK) 
144    doExport(ds,saveFileDlg.FileName);
145            
146  }

147  /**//// <summary>
148  /// 执行导出
149  /// </summary>
150  /// <param name="ds">要导出的DataSet</param>
151  /// <param name="strExcelFileName">要导出的文件名</param>

152  private void doExport(DataSet ds,string strExcelFileName)
153  {
154            
155   Excel.Application excel= new Excel.Application();
156            
157   //            Excel.Workbook obj=new Excel.WorkbookClass();
158   //            obj.SaveAs("c:\zn.xls",Excel.XlFileFormat.xlExcel9795,null,null,false,false,Excel.XlSaveAsAccessMode.xlNoChange,null,null,null,null);
159
160   int rowIndex=1;
161   int colIndex=0;
162
163   excel.Application.Workbooks.Add(true);
164            
165    
166   System.Data.DataTable table=ds.Tables[0] ;
167   foreach(DataColumn col in table.Columns)
168   {
169    colIndex++;    
170    excel.Cells[1,colIndex]=col.ColumnName;                
171   }

172
173   foreach(DataRow row in table.Rows)
174   {
175    rowIndex++;
176    colIndex=0;
177    foreach(DataColumn col in table.Columns)
178    {
179     colIndex++;
180     excel.Cells[rowIndex,colIndex]=row[col.ColumnName].ToString();
181    }

182   }

183   excel.Visible=false;    
184//   excel.Sheets[0] = "sss"; ///////////////////////////////?????????????????????//
185   excel.ActiveWorkbook.SaveAs(strExcelFileName+".XLS",Excel.XlFileFormat.xlExcel9795,null,null,false,false,Excel.XlSaveAsAccessMode.xlNoChange,null,null,null,null,null);
186            
187            
188   //wkbNew.SaveAs strBookName
189
190
191   //excel.Save(strExcelFileName);
192   excel.Quit();
193   excel=null;
194            
195   GC.Collect();//垃圾回收
196  }

197  #endregion

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
posted @ 2008-07-31 16:24  浪子の无悔  阅读(495)  评论(0编辑  收藏  举报