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 @   穿越时空的纸鸢  阅读(40)  评论(1编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示