如何使用DataTable和ExcelLibrary创建Excel工作表?

wan

我有不包含列的数据表,也没有带有“ DATE”数据类型的日期。我尝试了以下选项

1)第三部分DLL- ExcelLibrary如果数据集中没有日期列,它可以很好地工作,否则它使用一些虚拟值(例如-65284)代替日期。

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

2)使用简单的导出格式,而无需使用第三方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();
}
石匠

您的#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源代码。该库似乎不再维护。也许您应该转到积极维护的图书馆,例如EPPlusEPPlus实现可能如下所示:

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();
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用JXL创建从右到左对齐的Excel工作表

使用Parallel.For和EPPlus创建Excel工作表

使用VBA在Excel工作表中创建表

如何使用剪贴板将数据从Excel工作表复制到DataTable?

我如何使用VBA在Excel工作表中创建例如5个文件夹?

使用Excel工作表创建字典以替换值

如何在Scala / Spark中使用多个DataFrame中的多个工作表创建excel文件?

如何显示(或防止隐藏)从PowerShell创建的Excel工作表

在C#中使用ExcelLibrary使用多个工作表创建多个Excel

如何使用python创建GUI以接受用户输入并将其插入Excel工作表

熊猫:使用颜色标签创建Excel工作表

如何通过使用Excel VBA宏使Excel工作表受保护和不受保护?

如何基于Excel工作表创建某个数据框?

如何从HTML表格创建可下载的Excel工作表?

如何使用C#在Excel文件中创建第二张工作表

如何使用VBA在Excel中禁用保存和保护工作表提示

如何使用C#在Excel文件中的条件下创建新工作表

如何使用循环生成类似于Excel工作表的行和列键

如何使用jxl.jar和while循环在excel中创建几个不同的工作表?

如何使用vb.net从xml文件创建Excel工作表

Excel自动创建带有工作表副本和今天日期的新工作表

使用Java创建Excel工作表时遇到的问题

如何将使用Excel的名称框创建的名称从工作簿范围转换为工作表范围?

VBA代码:如何基于列数据从MS Excel复制行和创建工作表

如何检测用户是否在 excel 中使用 ctrl-click 创建工作表?

使用 Django 工作簿和工作表创建 Excel 文件

如何在 Python 中使用多张工作表从 Excel 创建数组

如何创建要在 Excel 工作表中使用的公式?

如何使用powershell脚本在excel中创建新工作表