Java Apache POI, how to set the background color and border at the same time
•
Java
At first I wanted to say that I was new in the developer world
I tried to generate an excel table with a bordered multiplication table and set the background color, but it is only applicable to column 1 and row 1
This is a correct example
I wrote something similar, but in the result file, the color cells have no borders: (
Please explain how I set the background color and border at the same time
import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.IndexedColors; import java.awt.image.IndexColorModel; import java.io.FileOutputStream; import java.io.IOException; import java.util.Scanner; public class Excel { public static void main(String[] args) throws IOException { Scanner in = new Scanner(system.in); System.out.println("enter number of rows: "); int x = in.nextInt(); System.out.println("enter number of columns: "); int y = in.nextInt(); System.out.println("enter name of file: "); String fileName = in.next() + ".xls"; System.out.println("Multiplication table will be created in file: " + fileName); createExcelMultiplicationTable(fileName,x,y); System.out.println("Process successful executed"); } private static void createExcelMultiplicationTable(String fileName,int x,int y) throws IOException { Workbook workbook = new HSSFWorkbook(); Sheet sheet = workbook.createSheet("multiplicationTable"); CellStyle backgroundStyle = workbook.createCellStyle(); backgroundStyle.setFillBackgroundColor(IndexedColors.GREY_50_PERCENT.getIndex()); backgroundStyle.setFillPattern(CellStyle.soLID_FOREGROUND); CellStyle borderStyle = workbook.createCellStyle(); borderStyle.setBorderBottom(CellStyle.BORDER_THIN); borderStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex()); borderStyle.setBorderLeft(CellStyle.BORDER_THIN); borderStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex()); borderStyle.setBorderRight(CellStyle.BORDER_THIN); borderStyle.setRightBorderColor(IndexedColors.BLACK.getIndex()); borderStyle.setBorderTop(CellStyle.BORDER_THIN); borderStyle.setTopBorderColor(IndexedColors.BLACK.getIndex()); for (int i = 1; i <= x; i++) { Row row = sheet.createRow(i - 1); for (int j = 1; j <= y; j++) { Cell cell = row.createCell(j - 1); cell.setCellValue(i * j); cell.setCellStyle(borderStyle); if (cell.getRowIndex() == 0 || cell.getColumnIndex() == 0) { cell.setCellStyle(backgroundStyle); } } } FileOutputStream out = new FileOutputStream(fileName); workbook.write(out); out.close(); } }
Solution
Change backgroundstyle setFillBackgroundColor(IndexedColors.GREY_50_PERCENT.getIndex()); to
backgroundStyle.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
You can set the following borders:
backgroundStyle.setBorderBottom(CellStyle.BORDER_THIN); backgroundStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex()); backgroundStyle.setBorderLeft(CellStyle.BORDER_THIN); backgroundStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex()); backgroundStyle.setBorderRight(CellStyle.BORDER_THIN); backgroundStyle.setRightBorderColor(IndexedColors.BLACK.getIndex()); backgroundStyle.setBorderTop(CellStyle.BORDER_THIN); backgroundStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
This will provide you with yellow and borders as needed
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码