/**
* 导出excel (HSSFWorkbook)
*/
@GetMapping("/testExport")
public void testExport1(HttpServletResponse response)
{
/** 第一步,创建一个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 = sheet.createRow(rowNum++);
row0.setHeight((short) 800);// 设置行高
String title = "excel导出标题";
HSSFCell c00 = row0.createCell(0);
c00.setCellValue(title);
c00.setCellStyle(titleStyle);
// 合并单元格,参数依次为起始行,结束行,起始列,结束列 (索引0开始)
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 6));//标题合并单元格操作,6为总列数
// 第二行
HSSFRow row1 = sheet.createRow(rowNum++);
row1.setHeight((short) 500);
String[] row_first = {"填表单位:", "", "", "", "", " 2019年第2季度 ", ""};
for (int i = 0; i < row_first.length; i++) {
HSSFCell tempCell = row1.createCell(i);
tempCell.setCellStyle(headerStyle);
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 = sheet.createRow(rowNum++);
row2.setHeight((short) 700);
String[] row_second = {"名称", "采集情况", "", "", "登记情况", "", "备注"};
for (int i = 0; i < row_second.length; i++) {
HSSFCell tempCell = row2.createCell(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 = sheet.createRow(rowNum++);
row3.setHeight((short) 700);
String[] row_third = {"", "登记数(人)", "办证总数(人)", "办证率(%)", "登记户数(户)", "签订数(份)", ""};
for (int i = 0; i < row_third.length; i++) {
HSSFCell tempCell = row3.createCell(i);
tempCell.setCellValue(row_third[i]);
tempCell.setCellStyle(headerStyle);
}
//循环每一行数据
List