【问题标题】:How to export data to .xlsx format, without using Open XML format?如何在不使用 Open XML 格式的情况下将数据导出为 .xlsx 格式?
【发布时间】:2015-10-12 11:17:38
【问题描述】:

帮助我将数据导出到 Excel。我在数据表中有一个数据列表。

我想将其导出为 Excel .xlsx 格式,而不使用 Open XML。

编程语言:C# .Net 版本:2.0

【问题讨论】:

    标签: excel .net-2.0 openxml


    【解决方案1】:

    找到这个....

    public static void DataGridToExcel(DataTable tbl)
    {
    HttpContext context = HttpContext.Current;
    context.Response.Clear();
    foreach (DataColumn c in tbl.Columns)
    {
        context.Response.Write(c.ColumnName + ";");
    }
    context.Response.Write(Environment.NewLine);
    foreach (DataRow r in tbl.Rows)
    {
        for (int i = 0; i < tbl.Columns.Count; i++)
        {
            context.Response.Write(r[i].ToString().Replace(";", string.Empty) + ";");
        }
        context.Response.Write(Environment.NewLine);
    }
    context.Response.ContentType = "text/csv";
    context.Response.AppendHeader("Content-Disposition",
        "attachment; filename=export.csv");
    context.Response.End();
    }
    

    这将从 ASP.NET 输出带有 Excel 2007 可以打开的 CSV 文件的响应。如果您愿意,可以将扩展名更改为模仿 excel,只需替换以下几行即可:

    context.Response.ContentType = "application/vnd.ms-excel";
    context.Response.AppendHeader("Content-Disposition",
        "attachment; filename=export.xlsx");
    

    如果您不需要做任何复杂的事情,CSV 是最简单的方法。如果您确实要求它是真正的原生格式的 Excel 2007 文件,则需要使用 Office 库来构建它或将其从 CSV 转换,然后提供/保存。

    【讨论】:

      猜你喜欢
      • 2014-09-29
      • 2017-10-22
      • 1970-01-01
      • 1970-01-01
      • 2011-06-25
      • 2020-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多