Apply Styles to Excel Example

This program demonstrates applying styles to Excel cells.

Problem Statement

Write a Java program that applies background color to Excel cells.

Source Code

1 import java.io.*;
2 import org.apache.poi.ss.usermodel.*;
3 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
4 public class ExcelStylesExample {
5 public static void main(String[] args) {
6 System.out.println("Eduinq Excel Styles Example");
7 Workbook wb=new XSSFWorkbook();
8 Sheet sheet=wb.createSheet("EduinqData");
9 Row row=sheet.createRow(0);
10 Cell cell=row.createCell(0);
11 cell.setCellValue("Styled Cell");
12 CellStyle style=wb.createCellStyle();
13 style.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
14 style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
15 cell.setCellStyle(style);
16 try(FileOutputStream fos=new FileOutputStream("styled.xlsx")) {
17 wb.write(fos);
18 System.out.println("Eduinq Excel Styled Successfully");
19 } catch(IOException e) {
20 System.out.println("Eduinq Exception: "+e.getMessage());
21 }
22 }
23 }

Program Output

					Eduinq Excel Styles Example
Eduinq Excel Styled Successfully
				

Explanation

Styles use CellStyle with foreground color. - Applies solid fill pattern. - Exception handling ensures safe operations. - In enterprise systems, styles highlight important data. Conclusion: Shows applying styles to Excel cells.

Quick Links to Explore