Document Solutions for Excel, Java Edition | Document Solutions
Features / Table / Table Style / Modify Table with Custom Style
In This Topic
    Modify Table with Custom Style
    In This Topic

    DsExcel Java allows users to modify tables with custom style.

    By default, a workbook possesses a collection of built-in table styles to enables users to apply formatting to tables. These default table styles represent that no formatting is applied to the tables. However, when a custom table style is created, it automatically gets added to the table style collection of a workbook and can be reused as and when required.

    While managing one or more table styles in your workbook, users can modify the built-in table style with custom table style. Each table style element represents the formatting for a particular element of the table. When a custom style for a table is defined, users first need to access the existing table style element in order to customize the table borders, set custom fill for the table, style row stripes or column stripes etc.

    In order to change the table style, you can use the setTableStyle method. In case you want to delete the applied table style, you can use the delete method.

    Refer to the following example code to modify table with custom style.

    Java
    Copy Code
    // Add one custom table style.
    ITableStyle style = workbook.getTableStyles().add("test");
    
    // Set WholeTable element style.
    style.getTableStyleElements().get(TableStyleElementType.WholeTable).getFont().setItalic(true);
    style.getTableStyleElements().get(TableStyleElementType.WholeTable).getFont().setThemeColor(ThemeColor.Accent6);
    style.getTableStyleElements().get(TableStyleElementType.WholeTable).getFont().setStrikethrough(true);
    style.getTableStyleElements().get(TableStyleElementType.WholeTable).getBorders().setLineStyle(BorderLineStyle.Dotted);
    style.getTableStyleElements().get(TableStyleElementType.WholeTable).getBorders().setThemeColor(ThemeColor.Accent2);
    style.getTableStyleElements().get(TableStyleElementType.WholeTable).getInterior().setColor(Color.FromArgb(24, 232, 192));
    
    // Set FirstColumnStripe element style.
    style.getTableStyleElements().get(TableStyleElementType.FirstColumnStripe).getFont().setBold(true);
    style.getTableStyleElements().get(TableStyleElementType.FirstColumnStripe).getFont().setColor(Color.FromArgb(255, 0, 0));
    style.getTableStyleElements().get(TableStyleElementType.FirstColumnStripe).getBorders().setLineStyle(BorderLineStyle.Thick);
    style.getTableStyleElements().get(TableStyleElementType.FirstColumnStripe).getBorders().setThemeColor(ThemeColor.Accent5);
    style.getTableStyleElements().get(TableStyleElementType.FirstColumnStripe).getInterior().setColor(Color.FromArgb(255, 255, 0));
    style.getTableStyleElements().get(TableStyleElementType.FirstColumnStripe).setStripeSize(2);
    
    // Set SecondColumnStripe element style.
    style.getTableStyleElements().get(TableStyleElementType.SecondColumnStripe).getFont().setColor(Color.FromArgb(255, 0, 255));
    style.getTableStyleElements().get(TableStyleElementType.SecondColumnStripe).getBorders().setLineStyle(BorderLineStyle.DashDot);
    style.getTableStyleElements().get(TableStyleElementType.SecondColumnStripe).getBorders().setColor(Color.FromArgb(42, 105, 162));
    style.getTableStyleElements().get(TableStyleElementType.SecondColumnStripe).getInterior().setColor(Color.FromArgb(204, 204, 255));
    
    ITable table = worksheet.getTables().get(0);
    
    // Set custom table style to table.
    table.setTableStyle(style);
    table.setShowTableStyleColumnStripes(true);