XSSFWorkbook xssfWorkbook = new XSSFWorkbook();
XSSFSheet sheet = xssfWorkbook.createSheet("sheet1");
//设置表格中列宽
sheet.setColumnWidth(0, 6 * 256);
sheet.setColumnWidth(1, 30 * 256);
sheet.setColumnWidth(2, 35 * 256);
sheet.setColumnWidth(3,90 * 256);
XSSFRow row;
Cell cell0;
Cell cell1;
Cell cell2;
CellStyle cellStyle;
XSSFFont font;
for (int i=0;i<2;i++) {
font= xssfWorkbook.createFont();
cellStyle = xssfWorkbook.createCellStyle();
//设置字体 样式
font.setColor(Font.COLOR_NORMAL);
cellStyle.setAlignment(HorizontalAlignment.CENTER);
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
font.setFontName("宋体");
font.setFontHeightInPoints((short) 11);
if (i == 0) {
row = sheet.createRow(i);
cell0 = row.createCell(0);
cell1 = row.createCell(1);
cell2 = row.createCell(2);
font.setBold(true);
cellStyle.setFont(font);
cell0.setCellStyle(cellStyle);
cell0.setCellValue("序号");
cell1.setCellStyle(cellStyle);
cell1.setCellValue("名称");
cell2.setCellStyle(cellStyle);
cell2.setCellValue("姓名");
} else {
row = sheet.createRow(i);
cell0 = row.createCell(0);
cell1 = row.createCell(1);
cell2 = row.createCell(2);
Cell cell3 = row.createCell(3);
cellStyle.setFont(font);
cell0.setCellStyle(cellStyle);
cell0.setCellValue("1");
cell1.setCellStyle(cellStyle);
cell1.setCellValue("有限公司");
cell2.setCellStyle(cellStyle);
cell2.setCellValue("南京");
if(cell3.getColumnIndex()==3){
//如果需要设置其他样式与重新初始化
font= xssfWorkbook.createFont();
cellStyle = xssfWorkbook.createCellStyle();
font.setColor(Font.COLOR_RED);
font.setFontName("宋体");
font.setFontHeightInPoints((short) 11);
cellStyle.setFont(font);
cell3.setCellStyle(cellStyle);
cell3.setCellValue("注:上传将从第三行开始,请勿删除示例行(第二行)");
}
}
}