【问题标题】:export Datagrid to excel asp将Datagrid导出到excel asp
【发布时间】:2010-09-21 06:03:01
【问题描述】:

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

干杯..

【问题讨论】:

    标签: c# asp.net datagrid export-to-excel


    【解决方案1】:

    SpreadsheetGear for .NET 会做到的。

    您可以查看带有 C# 和 VB 源代码here 的实时 ASP.NET 示例。其中一些示例演示了将 DataSet 或 DataTable 转换为 Excel - 您可以轻松地从 DataGrid 中获取 DataSet 或 DataTable。如果您想自己尝试,可以下载免费试用版here。

    免责声明:我拥有 SpreadsheetGear LLC

    【讨论】:

      【解决方案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。

      【讨论】:

      • 此方法不保留可能是泰语或日语等外语的文本
      【解决方案3】:

      最简单的方法是简单地将 csv 或 html(特别是 <table><tr><td>...</td></tr>...</table>)写入输出,然后通过 content-type 标头简单地假装它是 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();
              }
      

      【讨论】:

        猜你喜欢
        • 2013-11-08
        • 1970-01-01
        • 1970-01-01
        • 2011-05-07
        • 1970-01-01
        • 1970-01-01
        • 2023-03-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多