How to use the XSSFCellStyle class to format cells

To format cells using the XSSFCellStyle class, you need to use the Apache POI library. The following are Maven dependencies using Apache POI: <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.2</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency> The following is an example of Excel: |Name | Age | Occupation| |-------- | -------- | --------| |John | 25 | Engineer| |Alice | 30 | Doctor| |Michael | 35 | Lawyer| The following is a Java example code that uses the XSSFCellStyle class to format cells: import java.io.FileOutputStream; import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.*; public class CellStyleExample { public static void main(String[] args) throws Exception { //Create workbooks and worksheets Workbook workbook = new XSSFWorkbook(); Sheet sheet = workbook.createSheet("Sheet1"); //Create Cell Style CellStyle style = workbook.createCellStyle(); style.setFillForegroundColor(IndexedColors.YELLOW.getIndex()); style.setFillPattern(FillPatternType.SOLID_FOREGROUND); //Create Font Style Font font = workbook.createFont(); font.setBold(true); style.setFont(font); //Create rows and cells, and set styles Row row1 = sheet.createRow(0); Cell cell1 = row1.createCell(0); Cell1. setCellValue ("Name"); cell1.setCellStyle(style); Cell cell2 = row1.createCell(1); Cell2. setCellValue ("age"); cell2.setCellStyle(style); Cell cell3 = row1.createCell(2); Cell3. setCellValue ("profession"); cell3.setCellStyle(style); //Write file try (FileOutputStream outputStream = new FileOutputStream("example.xlsx")) { workbook.write(outputStream); } System. out. println ("Excel file generated successfully!"); } } In this example code, we create a new workbook and worksheet, then create a cell style and font style, and apply them to the cells. Finally, we write the workbook into a file. The generated Excel file will have a yellow background and bold font style applied to the first line of the workbook.