2010-03-23 56 views
1

我正在使用下面的代碼導出數據表到MS Word,Excel,CSV格式&它工作正常。但問題是,此代碼導出到MS Word 2003,Excel 2003版本。我需要將我的DataTable導出到Word 2007,Excel 2007,CSV,因爲我應該一次處理超過100,000條記錄,並且我們知道Excel 2003僅支持65,000條記錄。如何將數據表導出到MS Word 2007,Excel 2007,csv從asp.net?

請幫我,如果你知道如何導出數據表或數據集到Word 2007,Excel 2007中

public static void Convertword(DataTable dt, HttpResponse Response,string filename) 
{ 
     Response.Clear(); 
     Response.AddHeader("content-disposition", "attachment;filename=" + filename + ".doc"); 
     Response.Charset = ""; 
     Response.Cache.SetCacheability(HttpCacheability.NoCache); 
     Response.ContentType = "application/vnd.word"; 
     System.IO.StringWriter stringWrite = new System.IO.StringWriter(); 
     System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite); 
     System.Web.UI.WebControls.GridView dg = new System.Web.UI.WebControls.GridView(); 
     dg.DataSource = dt; 
     dg.DataBind(); 
     dg.RenderControl(htmlWrite); 
     Response.Write(stringWrite.ToString()); 
     Response.End(); 
     //HttpContext.Current.ApplicationInstance.CompleteRequest(); 

} 

    public static void Convertexcel(DataTable dt, HttpResponse Response, string filename) 
{ 

     Response.Clear(); 
     Response.AddHeader("content-disposition", "attachment;filename=" + filename + ".xls"); 
     Response.Charset = ""; 
     Response.Cache.SetCacheability(HttpCacheability.NoCache); 
     Response.ContentType = "application/vnd.ms-excel"; 
     System.IO.StringWriter stringWrite = new System.IO.StringWriter(); 
     System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite); 
     System.Web.UI.WebControls.DataGrid dg = new System.Web.UI.WebControls.DataGrid(); 
     dg.DataSource = dt; 
     dg.DataBind(); 
     dg.RenderControl(htmlWrite); 
     Response.Write(stringWrite.ToString()); 
     Response.End(); 
     //HttpContext.Current.ApplicationInstance.CompleteRequest(); 

} 

public static void ConvertCSV(DataTable dataTable, HttpResponse Response, string filename) 
{ 

     Response.Clear(); 
     Response.Buffer = true; 
     Response.AddHeader("content-disposition", "attachment;filename=" + filename + ".csv"); 
     Response.Charset = ""; 
     Response.Cache.SetCacheability(HttpCacheability.NoCache); 
     Response.ContentType = "Application/x-msexcel"; 
     StringBuilder sb = new StringBuilder(); 
     if (dataTable.Columns.Count != 0) 
     { 
      foreach (DataColumn column in dataTable.Columns) 
      { 
       sb.Append(column.ColumnName + ','); 
      } 
      sb.Append("\r\n"); 
      foreach (DataRow row in dataTable.Rows) 
      { 
       foreach (DataColumn column in dataTable.Columns) 
       { 
        if(row[column].ToString().Contains(',')==true) 
        { 
         row[column] = row[column].ToString().Replace(",", ""); 
        } 
        sb.Append(row[column].ToString() + ','); 
       } 
       sb.Append("\r\n"); 
      } 
     } 
     Response.Write(sb.ToString()); 
     Response.End(); 
     //HttpContext.Current.ApplicationInstance.CompleteRequest(); 

} 

回答

2

使用application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

相關問題