【问题标题】:How to create Excel sheet using DataTable and the ExcelLibrary?如何使用 DataTable 和 ExcelLibrary 创建 Excel 工作表?
【发布时间】:2016-01-02 04:19:58
【问题描述】:

我有一个数据表,其中不包含任何列,日期也为“DATE”数据类型。 我尝试了以下选项

1) 第三部分 DLL- ExcelLibrary 如果数据集中没有日期列,它可以正常工作,否则它会使用一些虚拟值,例如 -65284 而不是日期。

ExcelLibrary.DataSetHelper.CreateWorkbook(@"C:\Users\ABC\Documents\Excel\Report123.xls", ds);

2) 使用简单的导出格式,不使用 3rd 方 DLL,如下所示

public void ExportToExcel(System.Data.DataTable dt)
{
    if (dt.Rows.Count > 0)
    {
        string filename = "Report123.xls";
        System.IO.StringWriter tw = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
        DataGrid dgGrid = new DataGrid();
        dgGrid.DataSource = dt;
        dgGrid.DataBind();

        //Get the HTML for the control.
        dgGrid.RenderControl(hw);
        //Write the HTML back to the browser.
        //Response.ContentType = application/vnd.ms-excel;
        Response.ContentType = "application/vnd.ms-excel";
        Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + "");
        this.EnableViewState = false;
        Response.Write(tw.ToString());
        Response.End();
    }
}

上面的代码完美提取了Excel,但是当我们打开同一个excel时,出现格式错误的错误。

我还想读取数据表中的相同文件以存储在数据库中。当我去阅读创建的 excel(通过第二个选项)时,我得到错误,外部表不是预期的格式。如果我保存为同一个文件,那么它可以工作文件。

但我不想每次都“另存为”文件。请帮帮我

更新:

public void ExportToExcel1(System.Data.DataTable dt)
{
    //clear the response of any junk. This may not be necessary
    Response.Clear();

    //add a header so it has a nice file name
    Response.AddHeader("content-disposition", "attachment;filename=Reportengg.xlsx");

    //Set the MIME type correctly
    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

    //create a new package, this is the equivalent of an XLSX file.
    var package = new ExcelPackage();

    //Add a new sheet to the workbook
    var sheet = package.Workbook.Worksheets.Add("Sheet1");

    //EPPlus contains helper function to load data from a DataTable, though you could manually fill in rows/column values yourself if you want
    sheet.Cells["A1"].LoadFromDataTable(dt, true);
  //  byte[] array = package.GetAsByteArray();
    //write the file bytes to the response
    Response.BinaryWrite(package.GetAsByteArray());


    //end the response so we don't send anymore down and corrupt the file
    Response.End();
}

【问题讨论】:

  • 第 2 种技术是要避免的。格式确实不对。它不是 Excel 文件,而是带有 Excel 扩展名和 MIME 类型的 HTML 文件。您应该坚持使用 #1 之类的技术来创建实际的 Excel 文件。

标签: asp.net excel excellibrary


【解决方案1】:

您的#1 技巧是将文件保存在服务器上。服务器端代码无法直接保存到客户端文件系统。您必须将文件字节写入响应,然后客户端的 PC 将选择如何处理它。他们是选择“另存为”还是直接保存到某个下载文件夹取决于他们的浏览器设置。我不熟悉ExcelLibrary,但我想他们有某种 API 来获取文件字节?去做。然后将这些字节写入响应。

byte[] bytes = GetBytesFromTheirApiSomehow();
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AppendHeader("Content-Disposition", "attachment; filename=filename.xlsx");
Response.BinaryWrite(bytes);
Response.End();

我查看了 ExcelLibrary 源代码。该库似乎不再维护。也许您应该转移到一个积极维护的库,例如EPPlus。 EPPlus 实现可能如下所示:

public void ExportToExcel(System.Data.DataTable dt)
{
    //clear the response of any junk. This may not be necessary
    Response.Clear();

    //add a header so it has a nice file name
    Response.AddHeader("content-disposition", "attachment;filename=myexcelfile.xlsx");

    //Set the MIME type correctly
    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

    //create a new package, this is the equivalent of an XLSX file.
    var package = new ExcelPackage();

    //Add a new sheet to the workbook
    var sheet = package.Workbook.Worksheets.Add("My Data"); 

    //EPPlus contains helper function to load data from a DataTable, though you could manually fill in rows/column values yourself if you want
    sheet.Cells["A1"].LoadFromDataTable(dt, true); 

    //write the file bytes to the response
    Response.BinaryWrite(package.GetAsByteArray());

    //end the response so we don't send anymore down and corrupt the file
    Response.End();
}

【讨论】:

  • @mason- 仍然有同样的错误,1) 日期格式错误(存储虚拟数据)2) 尝试读取表时出现“外部表不是预期格式”的错误。跨度>
  • @dwan 你切换到 EPPlus 了吗?您是否像我的示例中那样确定并使用了.xlsx 扩展名?
  • 查看生成的文件。它是多少字节?如果你在做package.GetAsByteArray()的时候引入一个局部变量那是多少字节?
  • 字节是-字节[10897]
  • @dwan 和客户端的文件大小一样吗?
【解决方案2】:

这对我有用:

        Response.ClearContent();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Report123.xls"));
        Response.ContentType = "application/vnd.ms-excel";
        System.IO.StringWriter sw = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw);
        GridView1.AllowPaging = false;
        GridView1.RenderControl(hw);
        Response.Write(sw.ToString());
        Response.End();

【讨论】:

  • @losopha- 同样的错误“外部表不是预期的格式。”
猜你喜欢
  • 2012-07-11
  • 1970-01-01
  • 2020-01-13
  • 2013-12-15
  • 2019-01-08
  • 1970-01-01
  • 2018-07-23
  • 2012-03-13
  • 1970-01-01
相关资源
最近更新 更多