简体   繁体   中英

Format a Excel Sheet as a Table using EPPlus

I have a .Net core application, and i'm new to using the EPPlus Library with c#. I have a data-table filled with data which i'm currently inserting into an excel sheet using EPPlus. I was wondering if there is a way to format the sheet as a table after you added the data to the sheet and not before? (Like when you are using the Format a table function in excel)

sample of my Code which only fills the excel sheet:

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

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

excel.SaveAs(df);

please keep in mind the df variable above is of type file directory, and that the data in the data-table will be different for each user so the size will never be the same for two users.

Yes this can be done with EPPlus. You can use Dimension if you do not have fixed rows/columns.

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM