http://dotnet.aspx.cc/ShowDetail.aspx?id=BF0A54F9-C7C7-4200-BD9A-802AC1F5DE50
http://dotnet.aspx.cc/ShowDetail.aspx?id=EC5E84EC-68F9-4CD7-9E11-6F5C92027F0B

解决方案 »

  1.   


     可以这样:
     先将你的dataset中的数据绑定到一个DataGrid上,
     然后利用如下代码即可将dataset中的数据导出excel ...
    HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=Excel.xls");
    Response.ContentType = "application/vnd.ms-excel";
    HttpContext.Current.Response.Charset ="";
    HttpContext.Current.Response.ContentEncoding =System.Text.Encoding.Default;
    dgTest.Page.EnableViewState =false;
    System.IO.StringWriter tw = new System.IO.StringWriter() ;
    System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
    dgTest.Visible=true;
    dgTest.RenderControl(hw);
    HttpContext.Current.Response.Write(tw.ToString());
    HttpContext.Current.Response.End();
    //dgTest 为DataGrid
      

  2.   

    这是datagrid to excel!
    我要的是dataset to excel!
      

  3.   

    上面的方法导出的不是纯excel,是html格式的excel
      

  4.   

    /// <summary>
    /// 把DataTable转化为xls
    /// </summary>
    public bool TableToExcel(DataTable dt, string fileName, bool showTitle)
    {
    bool boolResult = false;
    System.IO.FileStream fsobj = null;
    System.IO.StreamWriter _sw = null;

    try
    {
    //生成一个文件流
    fsobj = new FileStream(fileName,System.IO.FileMode.Create,FileAccess.ReadWrite);
    //生成一个写入器
    _sw = new StreamWriter(fsobj,System.Text.UnicodeEncoding.Unicode);

    //写列标题
    if(showTitle)
    {
    for(int i=0;i<dt.Columns.Count;i++)
    {
    _sw.Write(dt.Columns[i].ColumnName+"\t");
    }
    _sw.Write("\r");
    }
    //写数据
    for(int i=0;i<dt.Rows.Count;i++)
    {
    for(int j=0;j<dt.Columns.Count;j++)
    {
    _sw.Write(dt.Rows[i][j].ToString().Trim()+"\t");
    }
    _sw.Write("\r");
    } _sw.Close();
    fsobj.Close();
    boolResult = true;
    }
    catch(Exception er)
    {
    string a = er.Message;
    if(_sw!=null)
    {
    _sw.Close();
    }
    if(fsobj!=null)
    {
    fsobj.Close();
    }
    boolResult = false;
    } return boolResult;
    }
      

  5.   

    上面的老兄,转换的根本就不是EXCEL文件。
    我有源程序,联系我:
    Email:
    [email protected]
      

  6.   

    我试过了,他老是说我的Excel.Application excel= new Excel.Application();
    拒绝访问!!不知道为什么?