【问题标题】:Format a Excel Sheet as a Table using EPPlus使用 EPPlus 将 Excel 工作表格式化为表格
【发布时间】:2019-03-04 09:57:05
【问题描述】:

我有一个 .Net 核心应用程序,而且我不熟悉将 EPPlus 库与 c# 一起使用。 我有一个填充了数据的数据表,我目前正在使用 EPPlus 将这些数据插入到 Excel 工作表中。 我想知道是否有办法在将数据添加到工作表之后而不是之前将工作表格式化为表格? (就像您在 excel 中使用格式化表格功能时一样)

我的代码示例,仅填充 excel 表:

var excelWorkSheet = excel.Workbook.Worksheets["WorkSheet1"];
using (SqlDataReader dr = cmd.ExecuteReader())
{
    dt.Load(dr);

}
excelWorkSheet.Cells["A1"].LoadFromDataTable(dt, true);

excel.SaveAs(df);

请记住,上面的 df 变量是文件目录类型,并且每个用户的数据表中的数据会有所不同,因此两个用户的大小永远不会相同。

【问题讨论】:

    标签: c# excel asp.net-core epplus


    【解决方案1】:

    是的,这可以通过 EPPlus 完成。如果您没有固定的行/列,您可以使用Dimension。

    using OfficeOpenXml.Table;
    
    
    ExcelWorksheet ws = excel.Workbook.Worksheets[1];
    
    //create a range for the table
    ExcelRange range = ws.Cells[1, 1, ws.Dimension.End.Row, ws.Dimension.End.Column];
    
    //add a table to the range
    ExcelTable tab = ws.Tables.Add(range, "Table1");
    
    //format the table
    tab.TableStyle = TableStyles.Medium2;
    

    【讨论】:

      猜你喜欢
      • 2023-03-08
      • 2018-03-21
      • 1970-01-01
      • 2014-07-02
      • 2015-04-26
      • 2020-08-21
      • 2021-12-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多