ClosedXML New Workbook Styles All Sheets
Asked Answered
I

3

6

I just got started with ClosedXML. When I create a new workbook with the code below, it automatically applies "Blue, Table Style Light 9" to each worksheet. I don't want any style on the worksheets. How do I specify no style?

XLWorkbook wb = new XLWorkbook();
wb.Worksheets.Add(dt, "sheet1");

I'm just basically filling the sheet with a SQL datatable.

Iniquitous answered 7/7, 2017 at 18:39 Comment(0)
J
13

By default, ClosedXML will create a new Excel table when you use the IXLWorksheets.Add(DataTable dt) method. Excel tables always have styles applied.

To populate your worksheet with the DataTable without any styles, use this code:

using (var wb = new XLWorkbook())
{
    var ws = wb.Worksheets.Add("sheet1");
    // The false parameter indicates that a table should not be created:
    ws.FirstCell().InsertTable(dt, false);
}
Judicator answered 10/7, 2017 at 9:42 Comment(1)
Glad to have answer from one of the authors!Itself
R
3

To add a dt without a Theme use the following code:

var ws = wb.Worksheets.Add("SheetName");
ws.FirstCell().InsertTable.InsertTable(dt).Theme = XLTableTheme.None;;
Rori answered 15/10, 2019 at 18:29 Comment(0)
B
2

XLWorkbook and XLWorksheet both have a Style property. You can modify the style like so:

var workbook = new XLWorkbook();
var ws = workbook.Worksheets.Add("Style Worksheet");

ws.Style.Font.Bold = true;
ws.Style.Font.FontColor = XLColor.Red;
ws.Style.Fill.BackgroundColor = XLColor.Cyan;

See here for the documentation: https://github.com/ClosedXML/ClosedXML/wiki/Using-Default-Styles

Blancheblanchette answered 7/7, 2017 at 18:45 Comment(3)
Yeah, I saw all of that. So you're saying the only way to get no format is to format the background to white, and the font color to black??? I'm not saying that's wrong...it just seems odd that it would start with an arbitrary style. Seems like it would have no style unless you explicitly chose one.Iniquitous
I think the tables look nice with an auto-chosen style. If you don't like it, yes, you have to explicitly remove it.Blancheblanchette
I don't mind the look...but the recipient of the report seems to be fairly particular. Amazing how we must please others. Haha! Thanks for answering!Iniquitous

© 2022 - 2024 — McMap. All rights reserved.