2008-11-07 112 views
1

将Datagrid导出为ex​​cel的最佳方式是什么?我没有任何经验将datagrid导出为ex​​cel,所以我想知道你们如何将excel导出datagrid。 我读了有很多的方法,但是我在想,只是做一个简单的表格输出到数据网格function.i正在使用asp.net C#将Datagrid导出为ex​​cel asp

欢呼声..

回答

6

最简单的方法是简单地将csv或html(特别是<table><tr><td>...</td></tr>...</table>)写入输出,并简单地通过内容类型标头假装它为excel格式。 Excel将很高兴地加载; CSV是简单...

这里有一个类似的例子(它实际上需要一个IEnumerable,但它会从任何来源相似(如DataTable,遍历行)。

 public static void WriteCsv(string[] headers, IEnumerable<string[]> data, string filename) 
     { 
      if (data == null) throw new ArgumentNullException("data"); 
      if (string.IsNullOrEmpty(filename)) filename = "export.csv"; 

      HttpResponse resp = System.Web.HttpContext.Current.Response; 
      resp.Clear(); 
      // remove this line if you don't want to prompt the user to save the file 
      resp.AddHeader("Content-Disposition", "attachment;filename=" + filename); 
      // if not saving, try: "application/ms-excel" 
      resp.ContentType = "text/csv"; 
      string csv = GetCsv(headers, data); 
      byte[] buffer = resp.ContentEncoding.GetBytes(csv); 
      resp.AddHeader("Content-Length", buffer.Length.ToString()); 
      resp.BinaryWrite(buffer); 
      resp.End(); 
     } 
     static void WriteRow(string[] row, StringBuilder destination) 
     { 
      if (row == null) return; 
      int fields = row.Length; 
      for (int i = 0; i < fields; i++) 
      { 
       string field = row[i]; 
       if (i > 0) 
       { 
        destination.Append(','); 
       } 
       if (string.IsNullOrEmpty(field)) continue; // empty field 

       bool quote = false; 
       if (field.Contains("\"")) 
       { 
        // if contains quotes, then needs quoting and escaping 
        quote = true; 
        field = field.Replace("\"", "\"\""); 
       } 
       else 
       { 
        // commas, line-breaks, and leading-trailing space also require quoting 
        if (field.Contains(",") || field.Contains("\n") || field.Contains("\r") 
         || field.StartsWith(" ") || field.EndsWith(" ")) 
        { 
         quote = true; 
        } 
       } 
       if (quote) 
       { 
        destination.Append('\"'); 
        destination.Append(field); 
        destination.Append('\"'); 
       } 
       else 
       { 
        destination.Append(field); 
       } 

      } 
      destination.AppendLine(); 
     } 
     static string GetCsv(string[] headers, IEnumerable<string[]> data) 
     { 
      StringBuilder sb = new StringBuilder(); 
      if (data == null) throw new ArgumentNullException("data"); 
      WriteRow(headers, sb); 
      foreach (string[] row in data) 
      { 
       WriteRow(row, sb); 

      } 
      return sb.ToString(); 
     } 
2

你可以做这样:

private void ExportButton_Click(object sender, System.EventArgs e) 
{ 
    Response.Clear(); 
    Response.Buffer = true; 
    Response.ContentType = "application/vnd.ms-excel"; 
    Response.Charset = ""; 
    this.EnableViewState = false; 
    System.IO.StringWriter oStringWriter = new System.IO.StringWriter(); 
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter); 
    this.ClearControls(dataGrid); 
    dataGrid.RenderControl(oHtmlTextWriter); 
    Response.Write(oStringWriter.ToString()); 
    Response.End(); 
} 

完整示例here

+0

此方法不保留可能是外语的文本,如泰文或日文 – htm11h 2012-03-13 17:41:45

0

会这样做。

你可以看到C#和VB源代码here的实时ASP.NET样本。其中几个示例演示了如何将DataSet或DataTable转换为Excel - 并且您可以轻松地从DataGrid获取DataSet或DataTable。如果您想自己尝试,可以下载免费试用版here

声明:我自己的SpreadsheetGear LLC