📜  apache poi excel color - Java (1)

📅  最后修改于: 2023-12-03 15:29:26.004000             🧑  作者: Mango

Apache POI Excel Color - Java

Apache POI is a Java library that provides APIs for manipulating Microsoft Office documents, including Excel spreadsheets. One of the features of Apache POI is the ability to set colors for cells and fonts in Excel spreadsheets using Java code. In this article, we'll explore how to use Apache POI to set colors in Excel spreadsheets.

Setting Cell Background Color

To set the background color of a cell in an Excel spreadsheet using Apache POI, you can use the setFillBackgroundColor method of the XSSFCellStyle class. Here's an example:

XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("Sheet1");

XSSFCellStyle style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.GREEN.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);

XSSFRow row = sheet.createRow(0);
XSSFCell cell = row.createCell(0);
cell.setCellValue("Hello, world!");
cell.setCellStyle(style);

FileOutputStream outputStream = new FileOutputStream("example.xlsx");
workbook.write(outputStream);
workbook.close();
outputStream.close();

In this example, we create a workbook, a sheet, and a cell style. We set the foreground color of the style to green using the FillForegroundColor property, and the fill pattern to solid using the FillPatternType property. We then create a row and a cell in the sheet, set the cell value to "Hello, world!", and set the cell style to the style we just created. Finally, we save the workbook to a file.

Setting Font Color

To set the color of the font in a cell in an Excel spreadsheet using Apache POI, you can use the setFontColor method of the XSSFFont class. Here's an example:

XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("Sheet1");

XSSFCellStyle style = workbook.createCellStyle();
XSSFFont font = workbook.createFont();
font.setColor(IndexedColors.RED.getIndex());
style.setFont(font);

XSSFRow row = sheet.createRow(0);
XSSFCell cell = row.createCell(0);
cell.setCellValue("Hello, world!");
cell.setCellStyle(style);

FileOutputStream outputStream = new FileOutputStream("example.xlsx");
workbook.write(outputStream);
workbook.close();
outputStream.close();

In this example, we create a workbook, a sheet, a font, and a cell style. We set the color of the font to red using the setColor method of the font, and then set the font of the style to the font we just created. We then create a row and a cell in the sheet, set the cell value to "Hello, world!", and set the cell style to the style we just created. Finally, we save the workbook to a file.

Conclusion

Apache POI is a powerful Java library that provides APIs for working with Microsoft Office documents. In this article, we've looked at how to use Apache POI to set colors in Excel spreadsheets using Java code. Whether you're working on a small project or a large enterprise application, Apache POI can help you manipulate Excel spreadsheets in a flexible and efficient way.