在前面一篇文章使用HSSFWorkbook导出、操作excel中我们学会了使用HSSFWorkbook导出简单的excel表格,这篇文章分享的是使用POI的HSSFWorkbook导出信息采集表样式的excel。
在项目中,关于一个商品或者是对象的许多属性客户希望能直观地显示并且方便存档,于是便需要导出这种格式的表格,这种表格和我们生活中的申请表格、个人信息、简历都很相似,他的特点是格式不固定,涉及很多单元格合并,包括行的合并单元格,列的合并单元格,最终的导出excel表格效果如下:
HSSFWorkbook如何合并单元格
方法:
// 合并单元格
CellRangeAddress(int firstRow, int lastRow, int firstCol, int lastCol)
参数说明:
参数分别表示:起始行号,终止行号, 起始列号,终止列号
行号、列号分别从0开始,因此第一行标题合并单元格就是
CellRangeAddress range = new CellRangeAddress(0, 0, 0, 7);
sheet.addMergedRegion(range);
因为每个单元格的内容、格式都不一样,无法用循环解决,所以导出方法比较臃肿,表格里导出的数据在实际开发中一般是从数据库查询出来的信息并通过实体类对象访问,这里为了演示是写死的测试数据,代码如下:
@RequestMapping(value = "/outExcel", produces = "application/json;charset=UTF-8", method = { RequestMethod.GET})
public void outExcel(HttpServletRequest request,HttpServletResponse response) {
try {
String fileName = request.getParameter("fileName");
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet();
int columnSize = 8;
//设置列宽
for(int i = 0;i< columnSize;i++ ){
sheet.setColumnWidth(i, 5000);
}
// 设置字体
HSSFFont headfont = workbook.createFont();
headfont.setFontName("宋体");
// 字体大小
headfont.setFontHeightInPoints((short) 22);
// 加粗
headfont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
//表格大标题字体样式,居中、加粗
HSSFCellStyle headstyle = workbook.createCellStyle();
headstyle.setFont(headfont);
// 左右居中
headstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// 上下居中
headstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
headstyle.setLocked(true);
// 自动换行
headstyle.setWrapText(true);
headstyle.setBorderLeft((short) 1);
headstyle.setLeftBorderColor(HSSFColor.BLACK.index);
headstyle.setRightBorderColor(HSSFColor.BLACK.index);
headstyle.setBorderRight((short) 1);
// 加粗字体样式
HSSFFont columnHeadFont = workbook.createFont();
columnHeadFont.setFontName("宋体");
columnHeadFont.setFontHeightInPoints((short) 12);
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.setTopBorderColor(HSSFColor.BLACK.index);
columnHeadStyle.setBorderTop((short) 1);
columnHeadStyle.setLeftBorderColor(HSSFColor.BLACK.index);
columnHeadStyle.setBorderLeft((short) 1);
columnHeadStyle.setRightBorderColor(HSSFColor.BLACK.index);
columnHeadStyle.setBorderRight((short) 1);
columnHeadStyle.setBottomBorderColor(HSSFColor.BLACK.index);
columnHeadStyle.setBorderBottom((short) 1);
HSSFFont font = workbook.createFont();
font.setFontName("宋体");
font.setFontHeightInPoints((short) 12);
//单元格字体居中样式
HSSFCellStyle centerstyle = workbook.createCellStyle();
centerstyle.setFont(font);
centerstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
centerstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
centerstyle.setWrapText(true);
centerstyle.setLeftBorderColor(HSSFColor.BLACK.index);
centerstyle.setBorderLeft((short) 1);
centerstyle.setTopBorderColor(HSSFColor.BLACK.index);
centerstyle.setBorderTop((short) 1);
centerstyle.setRightBorderColor(HSSFColor.BLACK.index);
centerstyle.setBorderRight((short) 1);
centerstyle.setBottomBorderColor(HSSFColor.BLACK.index);
centerstyle.setBorderBottom((short) 1);
centerstyle.setFillForegroundColor(HSSFColor.WHITE.index);
//单元格字体居左样式
HSSFCellStyle leftstyle = workbook.createCellStyle();
leftstyle.setFont(font);
leftstyle.setAlignment(HSSFCellStyle.ALIGN_LEFT);
leftstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
leftstyle.setWrapText(true);
leftstyle.setLeftBorderColor(HSSFColor.BLACK.index);
leftstyle.setBorderLeft((short) 1);
leftstyle.setTopBorderColor(HSSFColor.BLACK.index);
leftstyle.setBorderTop((short) 1);
leftstyle.setRightBorderColor(HSSFColor.BLACK.index);
leftstyle.setBorderRight((short) 1);
leftstyle.setBottomBorderColor(HSSFColor.BLACK.index);
leftstyle.setBorderBottom((short) 1);
leftstyle.setFillForegroundColor(HSSFColor.WHITE.index);
//单元格标题、居左、字体加粗样式
HSSFCellStyle leftHeadstyle = workbook.createCellStyle();
leftHeadstyle.setFont(columnHeadFont);
leftHeadstyle.setAlignment(HSSFCellStyle.ALIGN_LEFT);
leftHeadstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
leftHeadstyle.setWrapText(true);
leftHeadstyle.setLeftBorderColor(HSSFColor.BLACK.index);
leftHeadstyle.setBorderLeft((short) 1);
leftHeadstyle.setTopBorderColor(HSSFColor.BLACK.index);
leftHeadstyle.setBorderTop((short) 1);
leftHeadstyle.setRightBorderColor(HSSFColor.BLACK.index);
leftHeadstyle.setBorderRight((short) 1);
leftHeadstyle.setBottomBorderColor(HSSFColor.BLACK.index);
leftHeadstyle.setBorderBottom((short) 1);
leftHeadstyle.setFillForegroundColor(HSSFColor.WHITE.index);
// 创建第一行
HSSFRow row0 = sheet.createRow(0);
// 设置行高
row0.setHeight((short) 900);
// 创建第一列
HSSFCell cell = row0.createCell(0);
cell.setCellValue(new HSSFRichTextString("个人信息采集表"));
cell.setCellStyle(headstyle);
for(int i = 1;i< columnSize;i++ ){
cell = row0.createCell(i);
cell.setCellStyle(headstyle);
}
// 合并单元格
CellRangeAddress range = new CellRangeAddress(0, 0, 0, 7);
sheet.addMergedRegion(range);
// 创建第二行
HSSFRow row1 = sheet.createRow(1);
cell = row1.createCell(0);
cell.setCellValue(new HSSFRichTextString("姓名"));
row1.setHeight((short) 700);
cell.setCellStyle(columnHeadStyle);
cell = row1.createCell(1);
cell.setCellValue("张三");
cell.setCellStyle(centerstyle);
cell = row1.createCell(2);
cell.setCellValue(new HSSFRichTextString("性别"));
cell.setCellStyle(columnHeadStyle);
cell = row1.createCell(3);
cell.setCellValue("女");
cell.setCellStyle(centerstyle);
cell = row1.createCell(4);
cell.setCellValue(new HSSFRichTextString("文/理科"));
cell.setCellStyle(columnHeadStyle);
cell = row1.createCell(5);
cell.setCellValue("理科");
cell.setCellStyle(centerstyle);
cell = row1.createCell(6);
cell.setCellStyle(centerstyle);
cell = row1.createCell(7);
cell.setCellStyle(centerstyle);
range = new CellRangeAddress(1, 1, 5, 7);
sheet.addMergedRegion(range);
// 第三行
HSSFRow row2 = sheet.createRow(2);
row2.setHeight((short) 700);
cell = row2.createCell(0);
cell.setCellValue(new HSSFRichTextString("出生日期"));
cell.setCellStyle(columnHeadStyle);
cell = row2.createCell(1);
cell.setCellValue("1998年12月20日");
cell.setCellStyle(centerstyle);
cell = row2.createCell(2);
cell.setCellValue(new HSSFRichTextString("民族"));
cell.setCellStyle(columnHeadStyle);
cell = row2.createCell(3);
cell.setCellValue("汉族");
cell.setCellStyle(centerstyle);
cell = row2.createCell(4);
cell.setCellValue(new HSSFRichTextString("所在省份"));
cell.setCellStyle(columnHeadStyle);
cell = row2.createCell(5);
cell.setCellValue("北京");
cell.setCellStyle(centerstyle);
cell = row2.createCell(6);
cell.setCellStyle(centerstyle);
cell = row2.createCell(7);
cell.setCellStyle(centerstyle);
// 合并单元格
range = new CellRangeAddress(2, 2, 5, 7);
sheet.addMergedRegion(range);
// 第四行
HSSFRow row3 = sheet.createRow(3);
row3.setHeight((short) 700);
cell = row3.createCell(0);
cell.setCellValue(new HSSFRichTextString("所在城市"));
cell.setCellStyle(columnHeadStyle);
cell = row3.createCell(1);
cell.setCellValue("北京市");
cell.setCellStyle(centerstyle);
cell = row3.createCell(2);
cell.setCellValue(new HSSFRichTextString("分数"));
cell.setCellStyle(columnHeadStyle);
cell = row3.createCell(3);
cell.setCellValue(580);
cell.setCellStyle(centerstyle);
cell = row3.createCell(4);
cell.setCellValue(new HSSFRichTextString("年龄"));
cell.setCellStyle(columnHeadStyle);
cell = row3.createCell(5);
cell.setCellValue("16");
cell.setCellStyle(centerstyle);
cell = row3.createCell(6);
cell.setCellStyle(centerstyle);
cell = row3.createCell(7);
cell.setCellStyle(centerstyle);
range = new CellRangeAddress(3, 3, 5, 7);
sheet.addMergedRegion(range);
// 第5行
HSSFRow row4 = sheet.createRow(4);
row4.setHeight((short) 700);
cell = row4.createCell(0);
cell.setCellValue(new HSSFRichTextString("是否色盲"));
cell.setCellStyle(columnHeadStyle);
cell = row4.createCell(1);
cell.setCellValue("否");
cell.setCellStyle(centerstyle);
cell = row4.createCell(2);
cell.setCellValue(new HSSFRichTextString("身高"));
cell.setCellStyle(columnHeadStyle);
cell = row4.createCell(3);
cell.setCellValue(175);
cell.setCellStyle(centerstyle);
cell = row4.createCell(4);
cell.setCellValue(new HSSFRichTextString("体重"));
cell.setCellStyle(columnHeadStyle);
cell = row4.createCell(5);
cell.setCellValue(130);
cell.setCellStyle(centerstyle);
cell = row4.createCell(6);
cell.setCellStyle(centerstyle);
cell = row4.createCell(7);
cell.setCellStyle(centerstyle);
range = new CellRangeAddress(4, 4, 5, 7);
sheet.addMergedRegion(range);
// 第6行
HSSFRow row5 = sheet.createRow(5);
row5.setHeight((short) 700);
cell = row5.createCell(0);
cell.setCellValue(new HSSFRichTextString("身份证号"));
cell.setCellStyle(columnHeadStyle);
cell = row5.createCell(1);
cell.setCellValue("***");
cell.setCellStyle(centerstyle);
cell = row5.createCell(2);
cell = row5.createCell(3);
range = new CellRangeAddress(5, 5, 1, 3);
sheet.addMergedRegion(range);
cell.setCellStyle(centerstyle);
cell = row5.createCell(4);
cell.setCellValue(new HSSFRichTextString("邮箱"));
cell.setCellStyle(columnHeadStyle);
cell = row5.createCell(5);
cell.setCellValue("***");
cell.setCellStyle(centerstyle);
cell = row5.createCell(6);
cell.setCellStyle(centerstyle);
cell = row5.createCell(7);
cell.setCellStyle(centerstyle);
range = new CellRangeAddress(5, 5, 5, 7);
sheet.addMergedRegion(range);
// 第7行
HSSFRow row6 = sheet.createRow(6);
row6.setHeight((short) 700);
cell = row6.createCell(0);
cell.setCellValue(new HSSFRichTextString("个人爱好:"));
cell.setCellStyle(columnHeadStyle);
cell = row6.createCell(1);
cell.setCellValue("***");
cell.setCellStyle(centerstyle);
for(int i = 2;i< columnSize;i++ ){
cell = row6.createCell(i);
cell.setCellStyle(centerstyle);
}
range = new CellRangeAddress(6, 6, 1, 7);
sheet.addMergedRegion(range);
// 第8行
HSSFRow row7 = sheet.createRow(7);
row7.setHeight((short) 700);
cell = row7.createCell(0);
cell.setCellValue(new HSSFRichTextString("父亲联系电话"));
cell.setCellStyle(columnHeadStyle);
cell = row7.createCell(1);
cell.setCellValue("***");
cell.setCellStyle(centerstyle);
cell = row7.createCell(2);
cell.setCellStyle(centerstyle);
cell = row7.createCell(3);
range = new CellRangeAddress(7, 7, 1, 3);
sheet.addMergedRegion(range);
cell.setCellStyle(centerstyle);
cell = row7.createCell(4);
cell.setCellValue(new HSSFRichTextString("职业"));
cell.setCellStyle(columnHeadStyle);
cell = row7.createCell(5);
cell.setCellValue("***");
cell.setCellStyle(centerstyle);
cell = row7.createCell(6);
cell.setCellStyle(centerstyle);
cell = row7.createCell(7);
range = new CellRangeAddress(7, 7, 5, 7);
sheet.addMergedRegion(range);
// 第9行
HSSFRow row8 = sheet.createRow(8);
row8.setHeight((short) 700);
cell = row8.createCell(0);
cell.setCellValue(new HSSFRichTextString("母亲联系电话"));
cell.setCellStyle(columnHeadStyle);
cell = row8.createCell(1);
cell.setCellValue("***");
cell.setCellStyle(centerstyle);
cell = row8.createCell(2);
cell = row8.createCell(3);
// 合并单元格
range = new CellRangeAddress(8, 8, 1, 3);
sheet.addMergedRegion(range);
cell.setCellStyle(centerstyle);
cell = row8.createCell(4);
cell.setCellValue(new HSSFRichTextString("职业"));
cell.setCellStyle(columnHeadStyle);
cell = row8.createCell(5);
cell.setCellValue("***");
cell.setCellStyle(centerstyle);
cell = row8.createCell(6);
cell.setCellStyle(centerstyle);
cell = row8.createCell(7);
cell.setCellStyle(centerstyle);
range = new CellRangeAddress(8, 8, 5, 7);
sheet.addMergedRegion(range);
// 创建第10行
HSSFRow row9 = sheet.createRow(9);
cell = row9.createCell(0);
cell.setCellValue(new HSSFRichTextString("是否独生子女"));
row9.setHeight((short) 850);
cell.setCellStyle(columnHeadStyle);
cell = row9.createCell(1);
cell.setCellValue("否");
cell.setCellStyle(centerstyle);
cell = row9.createCell(2);
cell.setCellValue(new HSSFRichTextString("是否军人后裔"));
cell.setCellStyle(columnHeadStyle);
cell = row9.createCell(3);
cell.setCellValue("否");
cell.setCellStyle(centerstyle);
cell = row9.createCell(4);
cell.setCellValue(new HSSFRichTextString("是否低保家庭"));
cell.setCellStyle(columnHeadStyle);
cell = row9.createCell(5);
cell.setCellValue("否");
cell.setCellStyle(centerstyle);
cell = row9.createCell(6);
cell.setCellValue(new HSSFRichTextString("其他"));
cell.setCellStyle(columnHeadStyle);
range = new CellRangeAddress(9, 11, 6, 6);
sheet.addMergedRegion(range);
cell = row9.createCell(7);
cell.setCellValue("暂无");
cell.setCellStyle(centerstyle);
range = new CellRangeAddress(9, 11, 7, 7);
sheet.addMergedRegion(range);
// 创建第11行
HSSFRow row10 = sheet.createRow(10);
cell = row10.createCell(0);
cell.setCellValue(new HSSFRichTextString("是否打算出国"));
row10.setHeight((short) 700);
cell.setCellStyle(columnHeadStyle);
cell = row10.createCell(1);
cell.setCellValue("否");
cell.setCellStyle(centerstyle);
cell = row10.createCell(2);
cell.setCellValue(new HSSFRichTextString("是否艺术生"));
cell.setCellStyle(columnHeadStyle);
cell = row10.createCell(3);
cell.setCellValue("否");
cell.setCellStyle(centerstyle);
cell = row10.createCell(4);
cell.setCellValue(new HSSFRichTextString("是否打算考研"));
cell.setCellStyle(columnHeadStyle);
cell = row10.createCell(5);
cell.setCellValue("否");
cell.setCellStyle(centerstyle);
cell = row10.createCell(6);
cell.setCellStyle(centerstyle);
cell = row10.createCell(7);
cell.setCellStyle(centerstyle);
// 创建第12行
HSSFRow row11 = sheet.createRow(11);
cell = row11.createCell(0);
cell.setCellValue(new HSSFRichTextString("是否少数民族"));
row11.setHeight((short) 850);
cell.setCellStyle(columnHeadStyle);
cell = row11.createCell(1);
cell.setCellValue("否");
cell.setCellStyle(centerstyle);
cell = row11.createCell(2);
cell.setCellValue(new HSSFRichTextString("是否已经报考"));
cell.setCellStyle(columnHeadStyle);
cell = row11.createCell(3);
cell.setCellValue("否");
cell.setCellStyle(centerstyle);
cell = row11.createCell(4);
cell.setCellValue(new HSSFRichTextString("是否已经入学"));
cell.setCellStyle(columnHeadStyle);
cell = row11.createCell(5);
cell.setCellValue("否");
cell.setCellStyle(centerstyle);
cell = row11.createCell(6);
cell.setCellStyle(centerstyle);
cell = row11.createCell(7);
cell.setCellStyle(centerstyle);
// 创建第13行
HSSFRow row12 = sheet.createRow(12);
cell = row12.createCell(0);
cell.setCellValue(new HSSFRichTextString("个人爱好"));
row12.setHeight((short) 700);
cell.setCellStyle(leftHeadstyle);
for(int i = 1;i< columnSize;i++ ){
cell = row12.createCell(i);
cell.setCellStyle(leftHeadstyle);
}
range = new CellRangeAddress(12, 12, 0, 7);
sheet.addMergedRegion(range);
// 创建第14行
HSSFRow row13 = sheet.createRow(13);
cell = row13.createCell(0);
cell.setCellValue("***");
row13.setHeight((short) 700);
cell.setCellStyle(leftstyle);
for(int i = 1;i< columnSize;i++ ){
cell = row13.createCell(i);
cell.setCellStyle(leftstyle);
}
// 合并单元格
range = new CellRangeAddress(13, 13, 0, 7);
sheet.addMergedRegion(range);
// 创建第15行
HSSFRow row14 = sheet.createRow(14);
cell = row14.createCell(0);
cell.setCellValue(new HSSFRichTextString("学习经历"));
row14.setHeight((short) 700);
cell.setCellStyle(leftHeadstyle);
for(int i = 1;i< columnSize;i++ ){
cell = row14.createCell(i);
cell.setCellStyle(leftHeadstyle);
}
// 合并单元格
range = new CellRangeAddress(14, 14, 0, 7);
sheet.addMergedRegion(range);
// 创建第16行
HSSFRow row15 = sheet.createRow(15);
cell = row15.createCell(0);
cell.setCellValue("***");
row15.setHeight((short) 700);
cell.setCellStyle(leftstyle);
for(int i = 1;i< columnSize;i++ ){
cell = row15.createCell(i);
cell.setCellStyle(leftstyle);
}
// 合并单元格
range = new CellRangeAddress(15, 15, 0, 7);
sheet.addMergedRegion(range);
// 创建第17行
HSSFRow row16 = sheet.createRow(16);
cell = row16.createCell(0);
cell.setCellValue(new HSSFRichTextString("工作实践经历"));
row16.setHeight((short) 700);
cell.setCellStyle(leftHeadstyle);
for(int i = 1;i< columnSize;i++ ){
cell = row16.createCell(i);
cell.setCellStyle(leftHeadstyle);
}
// 合并单元格
range = new CellRangeAddress(16, 16, 0, 7);
sheet.addMergedRegion(range);
// 创建第18行
HSSFRow row17 = sheet.createRow(17);
cell = row17.createCell(0);
cell.setCellValue("***");
row17.setHeight((short) 700);
cell.setCellStyle(leftstyle);
for(int i = 1;i< columnSize;i++ ){
cell = row17.createCell(i);
cell.setCellStyle(leftstyle);
}
// 合并单元格
range = new CellRangeAddress(17, 17, 0, 7);
sheet.addMergedRegion(range);
// 创建第19行
HSSFRow row18 = sheet.createRow(18);
cell = row18.createCell(0);
cell.setCellValue(new HSSFRichTextString("职业规划"));
row18.setHeight((short) 700);
cell.setCellStyle(leftHeadstyle);
for(int i = 1;i< columnSize;i++ ){
cell = row18.createCell(i);
cell.setCellStyle(leftHeadstyle);
}
// 合并单元格
range = new CellRangeAddress(18, 18, 0, 7);
sheet.addMergedRegion(range);
// 创建第20行
HSSFRow row19 = sheet.createRow(19);
cell = row19.createCell(0);
cell.setCellValue("***");
row19.setHeight((short) 700);
cell.setCellStyle(leftstyle);
for(int i = 1;i< columnSize;i++ ){
cell = row19.createCell(i);
cell.setCellStyle(leftstyle);
}
// 合并单元格
range = new CellRangeAddress(19, 19, 0, 7);
sheet.addMergedRegion(range);
// 创建第21行
HSSFRow row20 = sheet.createRow(20);
cell = row20.createCell(0);
cell.setCellValue(new HSSFRichTextString("补充说明"));
row20.setHeight((short) 850);
cell.setCellStyle(columnHeadStyle);
cell = row20.createCell(1);
cell.setCellValue("***");
cell.setCellStyle(centerstyle);
for(int i = 2;i< columnSize;i++ ){
cell = row20.createCell(i);
cell.setCellStyle(centerstyle);
}
// 合并单元格
range = new CellRangeAddress(20, 20, 1, 7);
sheet.addMergedRegion(range);
OutputStream os = response.getOutputStream();
response.setContentType("application/msexcel");
response.setHeader("Content-disposition",
"attachment; filename=" + new String(fileName.getBytes("GB2312"), "8859_1") + ".xls");
workbook.write(os);
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
网页页面导出按钮请求方法:
导出
原文作者技术博客:https://www.jianshu.com/u/ac4daaeecdfe
95后前端妹子一枚,爱阅读,爱交友,将工作中遇到的问题记录在这里,希望给每一个看到的你能带来一点帮助。
欢迎留言交流。