Dear reader,
In Excel there is an option to format some data as a table using 'Format as Table'. This can be more or less reversed by using 'Convert to Range' which will convert the table back to regular data. However, the table styling is kept in place. I'd like to achieve exactly this effect using the infragistics Excel engine. I'd like to apply table formatting, but only to trigger the related style changes. I wish to avoid triggering any Excel functionality related to 'table formatting', such as sorting, filtering, table cell references, unique column names, etc.
Is it possible to achieve this using the excel engine? Either by triggering something similar to 'Convert to Range' or by other means?
Many thanks,Remy.
Hi Remy,
Thank you for following up. If you have any questions please do not hesitate to ask. Thanks and have a nice day.
First of all: whoaaa this support is excellent!
Thank you for your detailed reply. I'm sure it will be sufficient.
Hi Remy, I think I can help here. With our table support, you can add a table, specifying the style you'd like to use, and then you can remove the table and opt to leave the styling in place, just as "Convert to Range" works. The code for this might look something like this:
var workbook = new Workbook(WorkbookFormat.Excel2007);
var worksheet = workbook.Worksheets.Add("Sheet1");
var table = worksheet.Tables.Add("A1:B20",
tableHasHeaders: true,
tableStyle: workbook.StandardTableStyles["TableStyleMedium6"]);
worksheet.Tables.Remove(table, convertToRange: true);
This will not perform any sorting, filtering, or formula manipulation using table cell references, but it will enforce unique column names, which you have stated you do you want. So, we can modify this slightly and instead say that the table does not have headers when we add it. This will cause header cells to get inserted on our behalf with the unique names, and then we can simply hide those header cells, which will delete them and shift the table back up:
tableHasHeaders: false,
table.IsHeaderRowVisible = false;
But this presents two additional problems:
The first problem is unavoidable. If there are other entities below the table that intersect with, but are not completed contained in the new table's columns, an exception will be thrown. In this case, we will have to add the table with headers and work around the fact that the column names will be made unique. The second problem can be fixed by copying the header format to the cells at the top of the range after the table has been removed:
var style = workbook.StandardTableStyles["TableStyleMedium6"];
tableStyle: style);
var temp = workbook.CreateNewWorksheetCellFormat();
temp.SetFormatting(style.AreaFormats[WorksheetTableStyleArea.HeaderRow]);
foreach (var cell in worksheet.GetRegion("A1:B1"))
{
cell.CellFormat.SetFormatting(temp);
}
I hope this helps. Please let me know if you have any other questions.