EasyXLS™ library enables you to format Excel cells, rows, columns and cell ranges. The supported formatted options are:
- Cell text format: number format, currency format, date format, accounting format, percentage format, fraction format, text format and other custom formats - Cell alignment: horizontal and vertical alignment, text indentation, wrap text and shrink options, merge cells, text rotation and text direction - Cell font settings: font name, font size, foreground, bold ant italic options, underline style, strikethrough, superscript and subscript effects - Cell border: border line style and color - Cell fill settings: background color, pattern style, pattern color, gradient fill - Cell protection: locked cells and hidden formulas
The Excel file can be formatted inclusive using themes for MS Excel workbook.
EasyXLS provides a specific class, ExcelStyle, which stores all formatting settings. The ExcelStyle format can be set for a specific cell, for a row, for a column or for a range of cells. Setting a format for a cell overrides the format of a row or a column.
For better performances, EasyXLS recommends using as less as possible instances of ExcelStyle classes.
Setting the cell format can be accomplished from ExcelCell class using setStyle() method.
If you want to format cells, consider the code sample and the screenshot below:
.NET:# Create the formatting style for the header
xlsStyleHeader = ExcelStyle("Verdana", 8, True, True, Color.Yellow)
xlsStyleHeader.setBackground(Color.Black)
xlsStyleHeader.setBorderColors(Color.Gray, Color.Gray, Color.Gray, Color.Gray)
xlsStyleHeader.setBorderStyles(Border.BORDER_MEDIUM, Border.BORDER_MEDIUM,
Border.BORDER_MEDIUM, Border.BORDER_MEDIUM)
xlsStyleHeader.setHorizontalAlignment(Alignment.ALIGNMENT_CENTER)
xlsStyleHeader.setVerticalAlignment(Alignment.ALIGNMENT_BOTTOM)
xlsStyleHeader.setWrap(True)
# Add data in cells for report headerfor column in range(5):
xlsFirstTable.easy_getCell(0, column).setValue("Column " + str(column + 1))
xlsFirstTable.easy_getCell(0, column).setStyle(xlsStyleHeader)
Click here to see Continuous Code ListingJava:# Create the formatting style for the header
xlsStyleHeader = gateway.jvm.ExcelStyle("Verdana", 8,
True, True, gateway.jvm.Color.YELLOW)
xlsStyleHeader.setBackground(gateway.jvm.Color.BLACK)
xlsStyleHeader.setBorderColors(gateway.jvm.Color.GRAY, gateway.jvm.Color.GRAY,
gateway.jvm.Color.GRAY, gateway.jvm.Color.GRAY)
xlsStyleHeader.setBorderStyles(gateway.jvm.Border.BORDER_MEDIUM,
gateway.jvm.Border.BORDER_MEDIUM, gateway.jvm.Border.BORDER_MEDIUM,
gateway.jvm.Border.BORDER_MEDIUM)
xlsStyleHeader.setHorizontalAlignment(gateway.jvm.Alignment.ALIGNMENT_CENTER)
xlsStyleHeader.setVerticalAlignment(gateway.jvm.Alignment.ALIGNMENT_BOTTOM)
xlsStyleHeader.setWrap(True)
# Add data in cells for report headerfor column in range(5):
xlsFirstTable.easy_getCell(0, column).setValue("Column " + str(column + 1))
xlsFirstTable.easy_getCell(0, column).setStyle(xlsStyleHeader)
Click here to see Continuous Code Listing
The screen shot below represents the result of the above code that formats the cells. It shows an Excel file with two worksheets ('First Tab' and 'Second Tab'). The first worksheet has the following format for the cells. The column headers are formatted with font size (8), font name (Verdana), foreground (yellow), background (black), borders color (gray), border size (medium), horizontal alignment (center), vertical alignment (bottom), and wrap option. The table cells are formatted with default font size (10), default font name (Arial), borders color (gray), foreground color (dark gray).
The format attributes can be set directly to the cell. Use this scenario if there are only a few cells that require different formatting than the rest of the cells.
For better performances, EasyXLS recommends avoiding setting the format attributes for each ExcelCell class and use styles instead. For more details check Export large Excel files chapter.
Setting the row format can be accomplished from ExcelRow class using setStyle() method. Also, the row can be directly formatted using the specific methods from ExcelRow class.
<!-- Create the formatting style for row--><cfobject type="java"class="EasyXLS.ExcelStyle"name="xlsStyleRow"action="CREATE"><cfset xlsStyleRow.setFont("Verdana")><cfset xlsStyleRow.setFontSize(8)><cfset xlsStyleRow.setItalic(true)><cfset xlsStyleRow.setBold(true)><cfset xlsStyleRow.setForeground(Color.yellow)><cfset xlsStyleRow.setBackground(Color.black)><cfset xlsStyleRow.setBorderColors(Color.gray, Color.gray, Color.gray, Color.gray)><cfset xlsStyleRow.setBorderStyles(Border.BORDER_MEDIUM, Border.BORDER_MEDIUM,
Border.BORDER_MEDIUM, Border.BORDER_MEDIUM)><cfset xlsStyleRow.setHorizontalAlignment(Alignment.ALIGNMENT_CENTER)><cfset xlsStyleRow.setVerticalAlignment(Alignment.ALIGNMENT_BOTTOM)><cfset xlsStyleRow.setWrap(true)><!-- Apply the row format --><cfobject type="java"class="EasyXLS.ExcelRow"name="xlsRow"action="CREATE"><cfset xlsRow = xlsTable.easy_getRowAt(0)><cfset xlsRow.setStyle(xlsStyleRow)>
.NET:# Create the formatting style for the row
xlsStyleRow = ExcelStyle("Verdana", 8, True, True, Color.Yellow)
xlsStyleRow.setBackground(Color.Black)
xlsStyleRow.setBorderColors(Color.Gray, Color.Gray, Color.Gray, Color.Gray)
xlsStyleRow.setBorderStyles(Border.BORDER_MEDIUM, Border.BORDER_MEDIUM,
Border.BORDER_MEDIUM, Border.BORDER_MEDIUM)
xlsStyleRow.setHorizontalAlignment(Alignment.ALIGNMENT_CENTER)
xlsStyleRow.setVerticalAlignment(Alignment.ALIGNMENT_BOTTOM)
xlsStyleRow.setWrap(True)
# Apply the row format
xlsRow = xlsTable.easy_getRowAt(0)
xlsRow.setStyle(xlsStyleRow)
Java:# Create the formatting style for the row
xlsStyleRow = gateway.jvm.ExcelStyle("Verdana", 8,
True, True, gateway.jvm.Color.YELLOW)
xlsStyleRow.setBackground(gateway.jvm.Color.BLACK)
xlsStyleRow.setBorderColors(gateway.jvm.Color.GRAY, gateway.jvm.Color.GRAY,
gateway.jvm.Color.GRAY, gateway.jvm.Color.GRAY)
xlsStyleRow.setBorderStyles(gateway.jvm.Border.BORDER_MEDIUM,
gateway.jvm.Border.BORDER_MEDIUM, gateway.jvm.Border.BORDER_MEDIUM,
gateway.jvm.Border.BORDER_MEDIUM)
xlsStyleRow.setHorizontalAlignment(gateway.jvm.Alignment.ALIGNMENT_CENTER)
xlsStyleRow.setVerticalAlignment(gateway.jvm.Alignment.ALIGNMENT_BOTTOM)
xlsStyleRow.setWrap(True)
# Apply the row format
xlsRow = xlsTable.easy_getRowAt(0)
xlsRow.setStyle(xlsStyleRow)
If a cell is defined in the ExcelTable, the cell format will override the format of the row. The empty cells from the row will keep the row formatting.
Format columns
Setting the column format can be accomplished from ExcelColumn class using setStyle() method. Also, the column can be directly formatted using the specific methods from ExcelColumn class.
<!-- Create the formatting style for column--><cfobject type="java"class="EasyXLS.ExcelStyle"name="xlsStyleColumn"action="CREATE"><cfset xlsStyleColumn.setFont("Verdana")><cfset xlsStyleColumn.setFontSize(8)><cfset xlsStyleColumn.setItalic(true)><cfset xlsStyleColumn.setBold(true)><cfset xlsStyleColumn.setForeground(Color.yellow)><cfset xlsStyleColumn.setBackground(Color.black)><cfset xlsStyleColumn.setBorderColors(Color.gray, Color.gray, Color.gray, Color.gray)><cfset xlsStyleColumn.setBorderStyles(Border.BORDER_MEDIUM, Border.BORDER_MEDIUM,
Border.BORDER_MEDIUM, Border.BORDER_MEDIUM)><cfset xlsStyleColumn.setHorizontalAlignment(Alignment.ALIGNMENT_CENTER)><cfset xlsStyleColumn.setVerticalAlignment(Alignment.ALIGNMENT_BOTTOM)><cfset xlsStyleColumn.setWrap(true)><!-- Apply the column format --><cfobject type="java"class="EasyXLS.ExcelColumn"name="xlsColumn"action="CREATE"><cfset xlsColumn = xlsTable.easy_getColumnAt(0)><cfset xlsColumn.setStyle(xlsStyleColumn)>
.NET:# Create the formatting style for the column
xlsStyleColumn = ExcelStyle("Verdana", 8, True, True, Color.Yellow)
xlsStyleColumn.setBackground(Color.Black)
xlsStyleColumn.setBorderColors(Color.Gray, Color.Gray, Color.Gray, Color.Gray)
xlsStyleColumn.setBorderStyles(Border.BORDER_MEDIUM, Border.BORDER_MEDIUM,
Border.BORDER_MEDIUM, Border.BORDER_MEDIUM)
xlsStyleColumn.setHorizontalAlignment(Alignment.ALIGNMENT_CENTER)
xlsStyleColumn.setVerticalAlignment(Alignment.ALIGNMENT_BOTTOM)
xlsStyleColumn.setWrap(True)
# Apply the column format
xlsColumn = xlsTable.easy_getColumnAt(0)
xlsColumn.setStyle(xlsStyleColumn)
Java:# Create the formatting style for the column
xlsStyleColumn = gateway.jvm.ExcelStyle("Verdana", 8,
True, True, gateway.jvm.Color.YELLOW)
xlsStyleColumn.setBackground(gateway.jvm.Color.BLACK)
xlsStyleColumn.setBorderColors(gateway.jvm.Color.GRAY, gateway.jvm.Color.GRAY,
gateway.jvm.Color.GRAY, gateway.jvm.Color.GRAY)
xlsStyleColumn.setBorderStyles(gateway.jvm.Border.BORDER_MEDIUM,
gateway.jvm.Border.BORDER_MEDIUM, gateway.jvm.Border.BORDER_MEDIUM,
gateway.jvm.Border.BORDER_MEDIUM)
xlsStyleColumn.setHorizontalAlignment(gateway.jvm.Alignment.ALIGNMENT_CENTER)
xlsStyleColumn.setVerticalAlignment(gateway.jvm.Alignment.ALIGNMENT_BOTTOM)
xlsStyleColumn.setWrap(True)
# Apply the column format
xlsColumn = xlsTable.easy_getColumnAt(0)
xlsColumn.setStyle(xlsStyleColumn)
If a cell is defined in the ExcelTable, the cell format will override the format of the row. The empty cells from the row will keep the row formatting.
Format cell ranges
EasyXLS provides methods for applying a formatting style to a specific range of cells. Using ExcelTable.easy_setRangeStyle method, the same formatting is applied for all cells in the range.
' Create the formatting styleDim xlsStyleData As New ExcelStyle
xlsStyleData.setHorizontalAlignment(Alignment.ALIGNMENT_LEFT)
xlsStyleData.setForeground(Color.DarkGray)
xlsStyleData.setWrap(False)
xlsStyleData.setDataType(DataType.STRING)
xlsFirstTable.easy_setRangeStyle("A2:E101", xlsStyleData)
.NET:// Create the formatting style
$xlsStyleData = new COM("EasyXLS.ExcelStyle");
$xlsStyleData->setHorizontalAlignment($ALIGNMENT_ALIGNMENT_LEFT);
$xlsStyleData->setForeground((int)$COLOR_DARKGRAY);
$xlsStyleData->setWrap(false);
$xlsStyleData->setDataType($DATATYPE_STRING);
$xlsFirstTable->easy_setRangeStyle_2("A2:E101", $xlsStyleData);
Click here to see Continuous Code ListingJava:// Create the formatting style
$xlsStyleData = new java("EasyXLS.ExcelStyle");
$xlsStyleData->setHorizontalAlignment($ALIGNMENT_ALIGNMENT_LEFT);
$xlsStyleData->setForeground(java("java.awt.Color")->LIGHT_GRAY);
$xlsStyleData->setWrap(false);
$xlsStyleData->setDataType($DATATYPE_STRING);
$xlsFirstTable->easy_setRangeStyle("A2:E101", $xlsStyleData);
Click here to see Continuous Code Listing
.NET:# Create a formatting style
xlsStyleData = ExcelStyle()
xlsStyleData.setHorizontalAlignment(Alignment.ALIGNMENT_LEFT)
xlsStyleData.setForeground(Color.DarkGray)
xlsStyleData.setWrap(False)
xlsStyleData.setDataType(DataType.STRING)
xlsFirstTable.easy_setRangeStyle("A2:E101", xlsStyleData)
Click here to see Continuous Code ListingJava:# Create a formatting style
xlsStyleData = gateway.jvm.ExcelStyle()
xlsStyleData.setHorizontalAlignment(gateway.jvm.Alignment.ALIGNMENT_LEFT)
xlsStyleData.setForeground(gateway.jvm.Color.LIGHT_GRAY)
xlsStyleData.setWrap(False)
xlsStyleData.setDataType(gateway.jvm.DataType.STRING)
xlsFirstTable.easy_setRangeStyle("A2:E101", xlsStyleData)
Click here to see Continuous Code Listing
Autoformat for cell ranges
EasyXLS provides methods to apply a complex layout format for a range of cells. The autoformat includes formatting for header, inner data, footer and margins.
Using EasyXLS you can apply different font formatting in the same cell, so that the texts inside the cell have different look including bold, italic, colors, underline and other settings.
EasyXLS allows you to format the cells according to the theme set for the Excel file. The look of the spreadsheet changes according to the applied theme.