填充和颜色
Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("new sheet");
// Create a row and put some cells in it. Rows are 0 based.
Row row = sheet.createRow(1);
style = wb.createCellStyle();
//设置填充颜色
style.setFillForegroundColor(IndexedColors.ORANGE.getIndex());
//设置填充 颗粒
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
cell = row.createCell(2);
cell.setCellValue("X");
cell.setCellStyle(style);
// Write the output to a file
try (OutputStream fileOut = new FileOutputStream("workbook.xls")) {
wb.write(fileOut);
}
wb.close();
合并单元格
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("new sheet");
Row row = sheet.createRow(1);
Cell cell = row.createCell(1);
cell.setCellValue("This is a test of merging");
sheet.addMergedRegion(new CellRangeAddress(
1, //first row (0-based)
1, //last row (0-based)
1, //first column (0-based)
2 //last column (0-based)
));
// Write the output to a file
try (OutputStream fileOut = new FileOutputStream("workbook.xls")) {
wb.write(fileOut);
}
wb.close();
要想让文字保持如下效果
文字垂直居中&水平居中
Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("new sheet");
//创建单元行
Row row = sheet.createRow(0);
CellStyle style = wb.createCellStyle();
Cell cell = row.createCell(0);
//水平居中
style.setAlignment(HorizontalAlignment.CENTER);
//垂直居中
style.setVerticalAlignment(VerticalAlignment.CENTER);
cell.setCellValue("nobb show mecode");
cell.setCellStyle(style);
字体
// Create a new font and alter it.
CellStyle style = wb.createCellStyle();
Font font = wb.createFont();
//设置字体大小
font.setFontHeightInPoints((short)20);
//字体名称
font.setFontName("Microsoft Yahe");
//是否斜体
font.setItalic(true);
//设置删除线
font.setStrikeout(false);
style.setFont(font);