使用 EPPlus 将 Excel 工作表格式化为表格

A. *_*alt 6 c# excel epplus asp.net-core

我有一个 .Net 核心应用程序,而且我是在 c# 中使用 EPPlus 库的新手。我有一个数据表,里面装满了我目前正在使用 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);
Run Code Online (Sandbox Code Playgroud)

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

VDW*_*WWD 13

是的,这可以通过 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;
Run Code Online (Sandbox Code Playgroud)