This topic demonstrates how you can preserve table formatting when loading Microsoft Excel files.
This topic contains the following sections:
Each table has an associated style which provides default format settings for cells in many different areas of the table. This style is represented by the WorksheetTableStyle class and is exposed on a table through the
WorksheetTable's Style property. Preserving
WorksheetTableStyles is supported, so when you are loading Excel files, your table formatting will be preserved.
Code snippet below shows how Microsoft Excel table formats are preserved. It loads a Microsoft Excel file with tables in it and saves it under a different name, so you are able to compare files and assure that formatting is preserved.
The following screenshot is a preview of the final result.
A Microsoft Excel file named Book4.xlsx with a worksheet saved at your C: hard drive.
Random numbers in range C3:F6 and random names in range C2:F2 and B2:B6. Selection B2:F6 is formatted as Table with style Table Style Medium 3. This is done by selecting range and choosing Home -> Format as Table -> Table Style Medium 3.
Random numbers in range C8:F12 and random names in range C8:F8 and B8:B12. Selection B8:F12 is formatted as table with custom style. You can create custom style by choosing Home -> Format as Table -> New Table Style…
Empty table in region B14:F18. It is created by selecting B14:F18 and choosing Insert->Table. Microsoft Excel chooses as default style Table Style Medium 2.
The following code sample loads a Microsoft Excel file and saves it back under a different name.
In Visual Basic:
' Load a Workbook with tables in it Dim workbook As Infragistics.Documents.Excel.Workbook = Infragistics.Documents.Excel.Workbook.Load("C:\Book4.xlsx") ' Save the workbook under different name in order to compare with original source that table formats are preserved workbook.Save("C:\Book4-TableFormatPreserved.xlsx")
// Load a Workbook with tables in it Infragistics.Documents.Excel.Workbook workbook = Infragistics.Documents.Excel.Workbook.Load("C:\\Book4.xlsx"); // Save the workbook under different name in order to compare with original source that table formats are preserved workbook.Save("C:\\Book4-TableFormatPreserved.xlsx");
The following topics provide additional information related to this topic.
The following material (available outside the Infragistics family of content) provides additional information related to this topic.