Remove all formatting of the table created from InsertTable method in ClosedXML
Asked Answered
E

2

5

I'm using ClosedXML to generate a report. The data supplied to Excel is from a database which is stored to a DataTable object. The table is displayed fine in the Excel sheet - however, it has a default theme applied on the created table. Below is my sample code:

Dim workbook As XLWorkbook = New XLWorkbook()    
Dim _tempSummary= workbook.AddWorksheet("Summary").Cell(1, 1).InsertTable(tblSummary)

This is the result:

enter image description here

I wanted to remove the bold font styling of the created table. However, when I did this

_tempSummary.AsTable().Ranges("B1:G1,I1:O1,Q1:R1").Style.Font.SetBold(False)

It doesn't seem to work. Please tell me what I'm doing wrong. Thanks in advance.

Eliaeliades answered 17/12, 2015 at 15:23 Comment(3)
Try without the AsTable(). And if you don't want any styling at all, use InsertData() instead of InsertTable().Surplus
Please be a good Stackoverflow.com citizen and mark the question as answered.Wallace
I already marked the answer as the answer. Thanks ~Eliaeliades
E
4

I figured it out today. It was a very obvious mistake. I should have not used AsTable() since the cells I wanted to format are already in a table. This is the result of bad intellisense of VS2010, for some reason Table.Theme() doesn't show up on the context menu. I found the fixed of me own problem. It should have been:

_tempSummary.Theme() = XLTableTheme.None
Eliaeliades answered 18/12, 2015 at 14:33 Comment(2)
Mark this as the answer.Wallace
Hello, sorry I wasn't able to mark this as an answer before. I lacked the necessary points thing (I'm new to StackOverflow) to mark this as an answer. Thanks. :)Eliaeliades
L
4

I was using a DataTable and wanted to remove the blue default style of the table, Only the flowing worked for me :

using (var workbook = new XLWorkbook())
{
    var ws = workbook.Worksheets.Add(productListDataTable, "Product List Sheet");
    ws.Tables.FirstOrDefault().Theme = XLTableTheme.None; //works with manully added tables, datatables
    workbook.SaveAs(diskPath);
}

Other styling options are for ranges / cells are confusing, so if you are applying a style to a "table" you added (not a cell / a range) you will need to select that table and apply "XLTableTheme" on it.

Thanks

Licastro answered 17/10, 2020 at 17:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.