Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 819
A. Kriel Avatar asked Mar 04 '19 09:03

A. Kriel


People also ask

Does EPPlus work with XLS?

EPPlus does not work with the XLS format. Only XLSX. You'll need to find a new library.

How do I change the date format in Excel using EPPlus?

Format = "dd/MM/yyyy HH:mm"; if I set like this other columns which use numeric values are getting affected.


1 Answers

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;
like image 75
VDWWD Avatar answered Oct 24 '22 11:10

VDWWD