复杂的POI导出Excel表格(多⾏表头、合并单元格)
poi导出excel有两种⽅式:
第⼀种:从⽆到有的创建整个excel,通过HSSFWorkbook,HSSFSheet HSSFCell, 等对象⼀步⼀步的创建出⼯作簿,sheet,和单元格,并添加样式,数据等。
第⼆种:通过excel.xls 模板的⽅式,⾃⼰在桌⾯创建⼀个excel, 然后修改这个excel为模板,复制到项⽬中(我是放在根⽬录下),再然后读取模板,修改模板,给模板填充数据,最后把模板写⼊到另外⼀个excel2.xls中(硬盘中的)。 按我⾃⼰的理解,这种⽅式只适合,需要导出的内容是固定格式的,只需要填充⼀次数据的 情况。⽐如简历。
本⽂内容如下:
1. 使⽤ HSSFWorkbook 对象 实现excel导出。⼀般是导出excel2003
2. 使⽤ XSSFWorkbook 对象实现excel导出。 ⼀般是导出excel2007
3. 使⽤ SXSSFWorkbook 对象实现excel导出。 ⼀般是导出百万级数据的excel
4. 使⽤ template.xls 格式模板,实现excel导出。 ⼀般是导出有固定字段的excel
————————————————
本⽂介绍 HSSFWorkbook 导出Excel多⾏表头、合并单元格的表格
Java代码如下:
/**
* 导出excel (HSSFWorkbook)
*/
public void exportExcel() {
/** 第⼀步,创建⼀个Workbook,对应⼀个Excel⽂件 */
HSSFWorkbook wb = new HSSFWorkbook();
/** 第⼆步,在Workbook中添加⼀个sheet,对应Excel⽂件中的sheet */
HSSFSheet sheet = wb.createSheet("excel导出标题");
/
** 第三步,设置样式以及字体样式*/
HSSFCellStyle titleStyle = createTitleCellStyle(wb);
HSSFCellStyle headerStyle = createHeadCellStyle(wb);
HSSFCellStyle contentStyle = createContentCellStyle(wb);
/** 第四步,创建标题 ,合并标题单元格 */
// ⾏号
int rowNum = 0;
// 创建第⼀页的第⼀⾏,索引从0开始
HSSFRow row0 = ateRow(rowNum++);
row0.setHeight((short) 800);// 设置⾏⾼
String title = "excel导出标题";
HSSFCell c00 = ateCell(0);
c00.setCellValue(title);
c00.setCellStyle(titleStyle);
// 合并单元格,参数依次为起始⾏,结束⾏,起始列,结束列(索引0开始)
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 6));//标题合并单元格操作,6为总列数
// 第⼆⾏
HSSFRow row1 = ateRow(rowNum++);
row1.setHeight((short) 500);
String[] row_first = {"填表单位:", "", "", "", "", " 2019年第2季度 ", ""};
for (int i = 0; i < row_first.length; i++) {
HSSFCell tempCell = ateCell(i);
tempCell.setCellStyle(headerStyle);
if (i == 0) {
if (i == 0) {
tempCell.setCellValue(row_first[i] + "测试单位");
} else if (i == 5) {
tempCell.setCellStyle(headerStyle);
tempCell.setCellValue(row_first[i]);
} else {
tempCell.setCellValue(row_first[i]);
}
}
/
/ 合并
sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 4));
sheet.addMergedRegion(new CellRangeAddress(1, 1, 5, 6));
//第三⾏
HSSFRow row2 = ateRow(rowNum++);
row2.setHeight((short) 700);
String[] row_second = {"名称", "采集情况", "", "", "登记情况", "", "备注"};
for (int i = 0; i < row_second.length; i++) {
HSSFCell tempCell = ateCell(i);
tempCell.setCellValue(row_second[i]);
tempCell.setCellStyle(headerStyle);
}
// 合并
sheet.addMergedRegion(new CellRangeAddress(2, 3, 0, 0));//名称
sheet.addMergedRegion(new CellRangeAddress(2, 2, 1, 3));//⼈数情况
sheet.addMergedRegion(new CellRangeAddress(2, 2, 4, 5));//登记情况
sheet.addMergedRegion(new CellRangeAddress(2, 3, 6, 6));//备注
//第三⾏
HSSFRow row3 = ateRow(rowNum++);
row3.setHeight((short) 700);
String[] row_third = {"", "登记数(⼈)", "办证总数(⼈)", "办证率(%)", "登记户数(户)", "签订数(份)", ""};
for (int i = 0; i < row_third.length; i++) {
HSSFCell tempCell = ateCell(i);
tempCell.setCellValue(row_third[i]);
tempCell.setCellStyle(headerStyle);
}
//循环每⼀⾏数据
List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>(); //查询出来的数据 Map<String,Object> map = new HashMap<String,Object>();
map.put("name", "测试名称1");
map.put("r1", "111");
map.put("r2", "222");
map.put("r3", "333");
map.put("r4", "444");
map.put("r5", "555");
map.put("r6", "666");
dataList.add(map);
dataList.add(map);//加多⼀条list
for (Map<String, Object> excelData : dataList) {
HSSFRow tempRow = ateRow(rowNum++);
tempRow.setHeight((short) 500);
// 循环单元格填⼊数据
for (int j = 0; j < 7; j++) {
HSSFCell tempCell = ateCell(j);
tempCell.setCellStyle(contentStyle);
String tempValue;
if (j == 0) {
// 乡镇、街道名称
tempValue = ("name").toString();
} else if (j == 1) {
} else if (j == 1) {
// 登记数(⼈)
tempValue = ("r1").toString();
} else if (j == 2) {
// 办证总数(⼈)
tempValue = ("r2").toString();
} else if (j == 3) {
// 办证率(%)
tempValue = ("r3").toString();
} else if (j == 4) {
// 登记户数(户)
tempValue = ("r4").toString();
} else if (j == 5) {
// 签订数(份)
tempValue = ("r5").toString();
} else {
// 备注
tempValue = ("r6").toString();
}
tempCell.setCellValue(tempValue);
}
}
// 注释⾏
HSSFRow remark = ateRow(rowNum++);
remark.setHeight((short) 500);
String[] row_remark = {"注:表中的“办证率=办证总数÷登记数×100%”", "", "", "", "", "", ""};
for (int i = 0; i < row_remark.length; i++) {
HSSFCell tempCell = ateCell(i);
if (i == 0) {
tempCell.setCellStyle(headerStyle);
} else {
tempCell.setCellStyle(contentStyle);
}
tempCell.setCellValue(row_remark[i]);
}
int remarkRowNum = dataList.size() + 4;
sheet.addMergedRegion(new CellRangeAddress(remarkRowNum, remarkRowNum, 0, 6));//注释⾏合并单元格
// 尾⾏
HSSFRow foot = ateRow(rowNum++);
foot.setHeight((short) 500);
String[] row_foot = {"审核⼈:", "", "填表⼈:", "", "填表时间:", "", ""};
for (int i = 0; i < row_foot.length; i++) {
HSSFCell tempCell = ateCell(i);
tempCell.setCellStyle(contentStyle);
if (i == 0) {
tempCell.setCellValue(row_foot[i] + "张三");
} else if (i == 2) {
tempCell.setCellValue(row_foot[i] + "李四");
} else if (i == 4) {
tempCell.setCellValue(row_foot[i] + "xxxx");
} else {
tempCell.setCellValue(row_foot[i]);
}
}
int footRowNum = dataList.size() + 5;
// 注
sheet.addMergedRegion(new CellRangeAddress(footRowNum, footRowNum, 0, 1));
sheet.addMergedRegion(new CellRangeAddress(footRowNum, footRowNum, 2, 3));
sheet.addMergedRegion(new CellRangeAddress(footRowNum, footRowNum, 4, 6));
//导出
HttpServletResponse response = Response();
HttpServletResponse response = Response();
String fileName = "报表名称.xls";
try {
fileName = new Bytes("UTF-8"), "ISO-8859-1");
response.setHeader("Content-disposition", "attachment;filename=\"" + fileName + "\"");
OutputStream stream = OutputStream();
if (null != wb && null != stream) {
wb.write(stream);// 将数据写出去
wb.close();
stream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
标题样式代码
/**
* 创建标题样式
* @param wb
* @return
*/
private static HSSFCellStyle createTitleCellStyle(HSSFWorkbook wb) {
HSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setAlignment(HorizontalAlignment.CENTER);//⽔平居中
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直对齐
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
cellStyle.setFillForegroundColor(IndexedColors.GREY_Index());//背景颜⾊
HSSFFont headerFont1 = (HSSFFont) wb.createFont(); // 创建字体样式
headerFont1.setBold(true); //字体加粗
headerFont1.setFontName("⿊体"); // 设置字体类型
headerFont1.setFontHeightInPoints((short) 15); // 设置字体⼤⼩
cellStyle.setFont(headerFont1); // 为标题样式设置字体样式
return cellStyle;
}
表头代码
/**
* 创建表头样式
* @param wbhtmlborder
* @return
*/
private static HSSFCellStyle createHeadCellStyle(HSSFWorkbook wb) {
HSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setWrapText(true);// 设置⾃动换⾏
cellStyle.setFillForegroundColor(IndexedColors.GREY_Index());//背景颜⾊ cellStyle.setAlignment(HorizontalAlignment.CENTER); //⽔平居中
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); //垂直对齐
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
cellStyle.setBottomBorderColor(IndexedColors.BLACK.index);
cellStyle.setBorderBottom(BorderStyle.THIN); //下边框
cellStyle.setBorderLeft(BorderStyle.THIN); //左边框
cellStyle.setBorderRight(BorderStyle.THIN); //右边框
cellStyle.setBorderTop(BorderStyle.THIN); //上边框
HSSFFont headerFont = (HSSFFont) wb.createFont(); // 创建字体样式
headerFont.setBold(true); //字体加粗
headerFont.setFontName("⿊体"); // 设置字体类型
headerFont.setFontHeightInPoints((short) 12); // 设置字体⼤⼩
cellStyle.setFont(headerFont); // 为标题样式设置字体样式
return cellStyle;
}
内容样式代码:
/**
* 创建内容样式
* @param wb
* @return
*/
private static HSSFCellStyle createContentCellStyle(HSSFWorkbook wb) {
HSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);// 垂直居中
cellStyle.setAlignment(HorizontalAlignment.CENTER);// ⽔平居中
cellStyle.setWrapText(true);// 设置⾃动换⾏
cellStyle.setBorderBottom(BorderStyle.THIN); //下边框
cellStyle.setBorderLeft(BorderStyle.THIN); //左边框
cellStyle.setBorderRight(BorderStyle.THIN); //右边框
cellStyle.setBorderTop(BorderStyle.THIN); //上边框
// ⽣成12号字体
HSSFFont font = wb.createFont();
font.setColor((short)8);
font.setFontHeightInPoints((short) 12);
cellStyle.setFont(font);
return cellStyle;
}
Excel表格⽰例:(宽度,样式可以⾃⾏调整)
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论