poi创建excel,代码没有优化哈,初稿

   @RequestMapping("/createExcel")
public void createExcel(HttpServletRequest request, HttpServletResponse response){
log.info("the method is start");
HSSFWorkbook workbook = new HSSFWorkbook();// 创建一个Excel文件
HSSFSheet sheet = workbook.createSheet();// 创建一个Excel的Sheet
sheet.createFreezePane(0, 2);// 冻结
// 1.设置列宽
sheet.setColumnWidth(0, 6500);
sheet.setColumnWidth(1, 3500);
sheet.setColumnWidth(2, 3500);
sheet.setColumnWidth(3, 6500);
sheet.setColumnWidth(4, 6500);
sheet.setColumnWidth(5, 6500);
sheet.setColumnWidth(6, 6500);
sheet.setColumnWidth(7, 6500);

//2.标题样式
HSSFFont headfont = workbook.createFont();
headfont.setFontName("黑体");
headfont.setFontHeightInPoints((short) 24);// 字体大小
headfont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 加粗

HSSFCellStyle headstyle = workbook.createCellStyle();
headstyle.setFont(headfont);
headstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中
headstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
headstyle.setLeftBorderColor(HSSFColor.BLACK.index);// 左边框的颜色
headstyle.setBorderLeft((short) 1);// 边框的大小
headstyle.setRightBorderColor(HSSFColor.BLACK.index);// 右边框的颜色
headstyle.setBorderRight((short) 1);// 边框的大小
headstyle.setBottomBorderColor(HSSFColor.BLACK.index);
headstyle.setBorderBottom((short) 1);// 边框的大小
headstyle.setTopBorderColor(HSSFColor.BLACK.index);
headstyle.setBorderTop((short) 1);
headstyle.setLocked(true);
headstyle.setWrapText(true);// 自动换行

// 2.列头样式
HSSFFont columnHeadFont = workbook.createFont();
columnHeadFont.setFontName("宋体");
columnHeadFont.setFontHeightInPoints((short) 10);
columnHeadFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

HSSFCellStyle columnHeadStyle = workbook.createCellStyle();
columnHeadStyle.setFont(columnHeadFont);
columnHeadStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中
columnHeadStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
columnHeadStyle.setLocked(true);
columnHeadStyle.setWrapText(true);
columnHeadStyle.setLeftBorderColor(HSSFColor.BLACK.index);// 左边框的颜色
columnHeadStyle.setBorderLeft((short) 1);// 边框的大小
columnHeadStyle.setRightBorderColor(HSSFColor.BLACK.index);// 右边框的颜色
columnHeadStyle.setBorderRight((short) 1);// 边框的大小
columnHeadStyle.setTopBorderColor(HSSFColor.BLACK.index);
columnHeadStyle.setBorderTop((short) 1);
columnHeadStyle.setBottomBorderColor(HSSFColor.BLACK.index);
columnHeadStyle.setBorderBottom((short) 1);// 边框的大小
// 设置单元格的背景颜色(单元格的样式会覆盖列或行的样式)
columnHeadStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
columnHeadStyle.setFillForegroundColor(HSSFColor.YELLOW.index);

try {
// 创建第一行
HSSFRow row0 = sheet.createRow(0);
// 设置行高
row0.setHeight((short) 900);
// 创建第一列
HSSFCell cell0 = row0.createCell(0);
cell0.setCellValue(new HSSFRichTextString("导入失败模板"));
cell0.setCellStyle(headstyle);
/**
* 合并单元格
* 第一个参数:第一个单元格的行数(从0开始)
* 第二个参数:第二个单元格的行数(从0开始)
* 第三个参数:第一个单元格的列数(从0开始)
* 第四个参数:第二个单元格的列数(从0开始)
*/
CellRangeAddress range = new CellRangeAddress(0, 0, 0, 7);
sheet.addMergedRegion(range);

// 第二行
HSSFRow row2 = sheet.createRow(1);
row2.setHeight((short) 750);
HSSFCell cell = row2.createCell(0);
cell.setCellValue(new HSSFRichTextString("责任者"));
cell.setCellStyle(columnHeadStyle);
cell = row2.createCell(1);
cell.setCellValue(new HSSFRichTextString("成熟度排序"));
cell.setCellStyle(columnHeadStyle);
cell = row2.createCell(2);
cell.setCellValue(new HSSFRichTextString("事项"));
cell.setCellStyle(columnHeadStyle);
cell = row2.createCell(3);
cell.setCellValue(new HSSFRichTextString("前次会议要求/n/新项目的项目概要"));
cell.setCellStyle(columnHeadStyle);
cell = row2.createCell(4);
cell.setCellValue(new HSSFRichTextString("上周工作进展"));
cell.setCellStyle(columnHeadStyle);
cell = row2.createCell(5);
cell.setCellValue(new HSSFRichTextString("本周工作计划"));
cell.setCellStyle(columnHeadStyle);
cell = row2.createCell(6);
cell.setCellValue(new HSSFRichTextString("问题和建议"));
cell.setCellStyle(columnHeadStyle);
cell = row2.createCell(7);
cell.setCellValue(new HSSFRichTextString("备 注"));
cell.setCellStyle(columnHeadStyle);

String filename = "生成的excel.xls";//设置下载时客户端Excel的名称
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition", "attachment;filename=" + filename);
OutputStream ouputStream = response.getOutputStream();
workbook.write(ouputStream);
ouputStream.flush();
ouputStream.close();
//这个是把输出流转成输入流
// ByteArrayOutputStream ouputStream = new ByteArrayOutputStream();
// workbook.write(ouputStream);
// InputStream inputStream = new ByteArrayInputStream(ouputStream.toByteArray());
// ouputStream.flush();
// ouputStream.close();
} catch (Exception e) {
log.error("e is:",e);
}
}
posted @ 2022-03-26 00:56  穿越时空的纸鸢  阅读(52)  评论(1)    收藏  举报