windows Form下将dataGrid1中的数据导入Excel 请给出详细的代码!!!万分感谢!!

解决方案 »

  1.   

    要注意的是using Excel这一句,可能报错。原因是你要添加引用,还有的就是要先装Excel
    using System;
    using System.Data;
    using Excel;
    using System.Reflection; 
    using System.Runtime.InteropServices;
    using System.IO;
    using System.Web;
    using System.Diagnostics;
    namespace HLDXS.NET.Comm
    {
    /// <summary>
    /// excel_down 的摘要说明。
    /// </summary>
    public class excel_down:System.IDisposable
    {
    DataSet Excel_DS;

    public excel_down(DataSet Excel_DS)
    {
    this.Excel_DS=Excel_DS;
    }
    public void Export_Excel(System.Web.UI.Page excel)
    {
    Excel.Application MyApp;
    if(this.Excel_DS.Tables.Count<1)
    {
    return;
    }
    int table_count=this.Excel_DS.Tables.Count;
    object oMissiong = System.Reflection.Missing.Value; MyApp=new Excel.ApplicationClass(); Workbooks workbooks = MyApp.Workbooks;
    _Workbook MyBook = workbooks.Add(XlWBATemplate.xlWBATWorksheet);

    Excel.Sheets mysheet=null;
    mysheet=MyApp.Worksheets;

    for(int i=0;i<table_count;i++)
    {
    Excel._Worksheet MySheet1=new Excel.WorksheetClass();
    mysheet.Add(oMissiong,oMissiong,1,oMissiong);
    }
    int Columns=1;
    Excel._Worksheet MySheets=null;
    for(int i=0;i<table_count;i++)
    {
    MySheets=(Excel._Worksheet)MyBook.Worksheets.get_Item(i+1);
    MySheets.Name=this.Excel_DS.Tables[i].TableName; MySheets.Cells[1,Columns]=this.Excel_DS.Tables[i].TableName;

    for(int k=0;k<this.Excel_DS.Tables[i].Columns.Count;k++)
    {
    MySheets.Cells[2,Columns+k]=this.Excel_DS.Tables[i].Columns[k].Caption;
    for(int j=0;j<this.Excel_DS.Tables[i].Rows.Count;j++)
    {
    MySheets.Cells[j+3,Columns+k]=this.Excel_DS.Tables[i].Rows[j][k];
    }             
    }
    MySheets.get_Range(MySheets.Cells[1,Columns],MySheets.Cells[1,Columns+Excel_DS.Tables[i].Columns.Count-1]).Merge(0);
    MySheets.get_Range(MySheets.Cells[1,Columns],MySheets.Cells[1,Columns+Excel_DS.Tables[i].Columns.Count-1]).Font.Size=15;
    MySheets.get_Range(MySheets.Cells[1,Columns],MySheets.Cells[1,Columns+Excel_DS.Tables[i].Columns.Count-1]).Font.Bold=true;
    MySheets.get_Range(MySheets.Cells[1,Columns],MySheets.Cells[1,Columns+Excel_DS.Tables[i].Columns.Count-1]).RowHeight=24;
    MySheets.get_Range(MySheets.Cells[1,Columns],MySheets.Cells[1,Columns+Excel_DS.Tables[i].Columns.Count]).Borders.Weight=(OWC.LineWeightEnum.owcLineWeightThin);
    MySheets.get_Range(MySheets.Cells[2,Columns],MySheets.Cells[2,Columns+Excel_DS.Tables[i].Columns.Count]).ColumnWidth=10;
    }
    //删除第一个sheet
    int tt=MyApp.Worksheets.Count;
    MySheets=new Excel.WorksheetClass();
    MySheets=(Excel._Worksheet)MyBook.Worksheets.get_Item(tt);
    MySheets.Delete();
    MySheets=null;
    //

    string filename=excel.MapPath("")+"/"+this.Excel_DS.DataSetName+".xls";
    MySheets=new Excel.WorksheetClass();
    MySheets=(Excel._Worksheet)MyBook.Worksheets.get_Item(1);
    MyApp.Visible=false;

    MySheets.SaveAs(filename,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong);
    MyApp.Workbooks.Close();
    MyApp.Quit();
    MyApp=null;
    GC.Collect();
    HttpResponse resp;
    resp =excel.Response;
    resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
    string DownFileName=this.Excel_DS.DataSetName+".xls";
    resp.AppendHeader("Content-Disposition", "attachment;filename=" +DownFileName); 
    FileInfo MyFileInfo;
    long StartPos = 0, FileSize;
    MyFileInfo = new FileInfo(filename);
    FileSize = MyFileInfo.Length; 
    resp.WriteFile(filename, StartPos, FileSize);
    resp.Flush();
    MyFileInfo.Delete();
    excel.Response.End();
    }
    public void Dispose()
    {
    this.Excel_DS=null;
    }
    }
    }
    完整的一个 dataset 导出一个excel 有几个表就导出几个sheet