public class ExcelTest {
public static void main(String[] args) throws FileNotFoundException, IOException {
SXSSFWorkbook wb = new SXSSFWorkbook();
Sheet sheet = wb.createSheet("TEST");
Row row = sheet.createRow(2);
Cell cell = row.createCell(2);
cell.setCellValue("test");
CellStyle style = wb.createCellStyle();
style.setAlignment(CellStyle.ALIGN_LEFT);
style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
style.setFillForegroundColor(IndexedColors.AQUA.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
Font greyFont = wb.createFont();
greyFont.setFontName("Times New Roman");
greyFont.setBoldweight(Font.BOLDWEIGHT_NORMAL);
style.setFont(greyFont);
cell.setCellStyle(style);
cell = row.createCell(3);
cell.setCellValue("test2");
style = wb.createCellStyle();
style.setAlignment(CellStyle.ALIGN_LEFT);
style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
style.setFillBackgroundColor(IndexedColors.AQUA.getIndex());
style.setFillPattern(CellStyle.BIG_SPOTS);
cell.setCellStyle(style);
File file = new File("E:/Test.xlsx");
if (file.exists()) {
file.delete();
}
wb.write(new FileOutputStream(file));
}
}
效果图: