我使用的是asp.net  c#要把datagrid中的数据导入到客户端的excel,最好能让用户打开继续进行格式调整同时,大部分office版本是2003,可能用到的是owc11,大哥大姐们帮忙!

解决方案 »

  1.   

    我用了这个方法解决了:
    public void ToExcel(System.Web.UI.Control ctl)  
    {
    // HttpContext.Current.Response.Charset ="GB2312";
    HttpContext.Current.Response.Charset ="utf-8";
    HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=memory.xls");

    HttpContext.Current.Response.ContentEncoding =System.Text.Encoding.GetEncoding("utf-8"); 
    HttpContext.Current.Response.ContentType ="application/ms-excel";//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword
    ctl.Page.EnableViewState =false;
    System.IO.StringWriter  tw = new System.IO.StringWriter() ;
    System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter (tw);
    ctl.RenderControl(hw);
    HttpContext.Current.Response.Write(tw.ToString());
    HttpContext.Current.Response.End();
    }
      

  2.   

    public void Export(string filename,string sql)
    {
    SpreadsheetClass xlsheet = new  SpreadsheetClass();
    GTSH.Database d=new atabase(Globals.DatabaseConnectionString);
    SqlCommand com=d.GetCommand(sql);

    SqlDataReader reader=com.ExecuteReader();
    int cols=reader.FieldCount;
    int row=1;
    d.OpenConnection();
    while(reader.Read())
    {
    for(int i=0;i<cols;i++)
    {
    xlsheet.ActiveSheet.Cells[row,i+1]=reader.GetValue(i).ToString();
    }
    row++;
    }
    reader.Close();
    d.CloseConnection();
    xlsheet.Export(HttpContext.Current.Server.MapPath("../../")+"ExcelFolder\\"+filename+".xls",OWC11.SheetExportActionEnum.ssExportActionNone,OWC11.SheetExportFormat.ssExportAsAppropriate);
    }
      

  3.   

    http://dotnet.aspx.cc/ShowDetail.aspx?id=BF0A54F9-C7C7-4200-BD9A-802AC1F5DE50