首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用DataTable和ExcelLibrary创建Excel表?

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

Stack Overflow用户
提问于 2015-10-05 13:26:01
回答 2查看 2K关注 0票数 3

我有数据表,它不包含任何列,也有日期和“日期”数据类型。我试过以下选项

1)第3部分DLL- ExcelLibrary如果数据集中没有日期列,则工作正常,否则使用-65284之类的虚拟值来代替date。

代码语言:javascript
复制
ExcelLibrary.DataSetHelper.CreateWorkbook(@"C:\Users\ABC\Documents\Excel\Report123.xls", ds);

2)使用简单的导出格式,而不使用第三方DLL,如下所示

代码语言:javascript
复制
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时,会出现格式错误的错误。

另外,我希望在datatable中读取相同的文件以存储在数据库中。当我去阅读创建的excel (第二个选项)时,我会得到外部表没有预期格式的错误。如果我保存为相同的文件,那么它可以工作文件。

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

更新:

代码语言:javascript
复制
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();
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-10-05 13:32:17

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

代码语言:javascript
复制
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实现可能如下所示:

代码语言:javascript
复制
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();
}
票数 1
EN

Stack Overflow用户

发布于 2015-10-05 16:35:57

这对我起了作用:

代码语言:javascript
复制
        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();
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32949802

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档