[英]Java create Excel with styles
I have problems when I join the styles and fonts. 加入样式和字体时出现问题。 Finally when I created the excel, the excel create success, but my excel doesn't have style and font, he doesn't have nothing, only datas , I need background-color: red and color-solid white in row == 0
最后,当我创建excel时,excel会成功,但是我的excel没有样式和字体,他什么也没有,只有data,我需要background-color:红色和行中== 0的纯白色
My code: 我的代码:
try {
String filename = pathTempdownloadFile;
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet(eyelash);
CellStyle backgroundStyle = workbook.createCellStyle();
HSSFFont font = workbook.createFont();
backgroundStyle.setFillBackgroundColor(IndexedColors.RED.getIndex());
font.setColor(IndexedColors.WHITE.getIndex());
HSSFRow rowhead = sheet.createRow((short) 0);
rowhead.createCell(0).setCellValue("YEAR");
rowhead.createCell(1).setCellValue("MONTH");
rowhead.createCell(2).setCellValue("NAME)");
rowhead.createCell(3).setCellValue("SURNAME");
rowhead.setRowStyle(backgroundStyle);
backgroundStyle.setFont(font);
FileOutputStream fileOut = new FileOutputStream(filename);
workbook.write(fileOut);
fileOut.close();
workbook.close();
} catch (Exception ex) {
System.out.println(ex);
}
Repeat : I can create the excel with data, but My excel doesn't have style neither font. 重复:我可以用数据创建excel,但是excel既没有样式,也没有字体。
This should work: 这应该工作:
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet();
HSSFFont font = workbook.createFont();
font.setColor(HSSFColor.WHITE.index);
HSSFCellStyle style = workbook.createCellStyle();
style.setFont(font);
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
style.setFillForegroundColor(HSSFColor.RED.index);
List<String> header = Arrays.asList(new String [] {"YEAR", "MONTH", "NAME", "SURNAME"});
HSSFRow rowhead = sheet.createRow((short) 0);
int cellnum = 0;
for (String s : header) {
HSSFCell cell = rowhead.createCell(cellnum++);
cell.setCellValue(s);
cell.setCellStyle(style);
}
FileOutputStream fileOut = new FileOutputStream(filename);
workbook.write(fileOut);
fileOut.close();
workbook.close();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.