/**
* 导出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<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 = sheet.createRow(rowNum++);
tempRow.setHeight((short) 500);
// 循环单元格填入数据
for (int j = 0; j < 7; j++) {
HSSFCell tempCell = tempRow.createCell(j);
tempCell.setCellStyle(contentStyle);
String tempValue;
if (j == 0) {
// 乡镇、街道名称
tempValue = excelData.get("name").toString();
} else if (j == 1) {
// 登记数(人)
tempValue = excelData.get("r1").toString();
} else if (j == 2) {
// 办证总数(人)
tempValue = excelData.get("r2").toString();
} else if (j == 3) {
// 办证率(%)
tempValue = excelData.get("r3").toString();
} else if (j == 4) {
// 登记户数(户)
tempValue = excelData.get("r4").toString();
} else if (j == 5) {
// 签订数(份)
tempValue = excelData.get("r5").toString();
} else {
// 备注
tempValue = excelData.get("r6").toString();
}
tempCell.setCellValue(tempValue);
}
} // 注释行
HSSFRow remark = sheet.createRow(rowNum++);
remark.setHeight((short) 500);
String[] row_remark = {"注:表中的“办证率=办证总数÷登记数×100%”", "", "", "", "", "", ""};
for (int i = 0; i < row_remark.length; i++) {
HSSFCell tempCell = remark.createCell(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 = sheet.createRow(rowNum++);
foot.setHeight((short) 500);
String[] row_foot = {"审核人:", "", "填表人:", "", "填表时间:", "", ""};
for (int i = 0; i < row_foot.length; i++) {
HSSFCell tempCell = foot.createCell(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 = this.getResponse();
String fileName = "报表名称.xls"; try {
response.setCharacterEncoding("UTF-8");
response.setHeader("content-Type", "application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
OutputStream stream = response.getOutputStream();
if (null != wb && null != stream) {
wb.write(stream);// 将数据写出去
wb.close();
stream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}

导出结果如图

POI导出复杂Excel,合并单元格(2)的更多相关文章

  1. Java poi导出设置 Excel某些单元格不可编辑

    小白的总结,大神勿喷:需要转载请说明出处,如果有什么问题,欢迎留言 一.需求: 1.某一列 .某一行或某些单元格不可编辑,其他列可以编辑 二.期间遇到的问题 1.无法设置成不可编辑 2.设置为不可编辑 ...

  2. asp.net C#取Excel 合并单元格内容

    asp教程.net c#取excel 合并单元格内容读取excel数据,填充dataset// 连接字符串 string xlspath = server.mappath("~/www.11 ...

  3. 使用POI创建word表格合并单元格兼容wps

    poi创建word表格合并单元格代码如下: /** * @Description: 跨列合并 */ public void mergeCellsHorizontal(XWPFTable table, ...

  4. java poi导出Excel合并单元格并设置边框

    import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; i ...

  5. poi导出excel合并单元格(包括列合并、行合并)

    1 工程所需jar包如下:commons-codec-1.5.jarcommons-logging-1.1.jarlog4j-1.2.13.jarjunit-3.8.1.jarpoi-3.9-2012 ...

  6. poi excel 合并单元格

    结论:final CellRangeAddress cra = new CellRangeAddress(rowId, rowId + rowSkip,        colId, colId + c ...

  7. 前端Excel表格导入导出,包括合并单元格,表格自定义样式等

    表格数据导入 读取导入Excel表格数据这里采用的是 xlsx 插件 npm i xlsx 读取excel需要通过 XLSX.read(data, {type: type}) 方法来实现,返回一个叫W ...

  8. npoi导出excel合并单元格

    需要引用NPOI.dll程序集和Ionic.Zip.dll程序集 string[] headerRowName = { "序号", "地市", "镇街 ...

  9. NPOI之Excel——合并单元格、设置样式、输入公式、设置筛选等

    首先建立一个空白的工作簿用作测试,并在其中建立空白工作表,在表中建立空白行,在行中建立单元格,并填入内容: //建立空白工作簿 IWorkbook workbook = new HSSFWorkboo ...

  10. NPOI之Excel——合并单元格、设置样式、输入公式

    首先建立一个空白的工作簿用作测试,并在其中建立空白工作表,在表中建立空白行,在行中建立单元格,并填入内容: //建立空白工作簿 IWorkbook workbook = new HSSFWorkboo ...

随机推荐

  1. 『现学现忘』Git基础 — 10、配置Git用户签名说明

    目录 1.为什么要创建用户签名 2.为什么要在Git中配置这些信息 3.创建用户签名的方式 4.总结 1.为什么要创建用户签名 作为版本控制系统的客户端,每台客户机对版本库的所有提交操作,都需要注明操 ...

  2. v74.01 鸿蒙内核源码分析(编码方式篇) | 机器指令是如何编码的 | 百篇博客分析OpenHarmony源码

    本篇关键词:指令格式.条件域.类型域.操作域.数据指令.访存指令.跳转指令.SVC(软件中断) 内核汇编相关篇为: v74.01 鸿蒙内核源码分析(编码方式) | 机器指令是如何编码的 v75.03 ...

  3. 一个实战让你搞懂Dockerfile

    摘要 在认识Dockerfile的基础功能之后,即一个用基础镜像来构建新镜像的文本文件,就需要在实际工作中使用其灵活便利的操作来提升我们的工作效率了,这里演示在Tomcat里运行一个程序的过程,以此来 ...

  4. XCTF练习题---MISC---Excaliflag

    XCTF练习题---MISC---Excaliflag flag:3DS{Gr4b_Only_th1s_B1ts} 解题步骤: 1.观察题目,下载附件 2.拿到手以后是一张图片,话不多说,直接上Ste ...

  5. 论文解读(GMT)《Accurate Learning of Graph Representations with Graph Multiset Pooling》

    论文信息 论文标题:Accurate Learning of Graph Representations with Graph Multiset Pooling论文作者:Jinheon Baek, M ...

  6. CSS常用技术

    1.处理图片底部 5px 间距 <style> body {background: #2d97db;} .imgBox { background: #fff; font-size: 0; ...

  7. numpy学习笔记Ⅰ

    一直被numpy和matplotlib困扰,打算好好学习一下,也是从自己的观点,学对自己帮助最大的部分 主要参考<https: www.runoob.com="" numpy ...

  8. SpringBoot接入两套kafka集群

    引入依赖 compile 'org.springframework.kafka:spring-kafka' 第一套kafka配置 package myapp.kafka; import lombok. ...

  9. VMware 虚拟机图文安装和配置 Rocky Linux 8.5 教程

    前言 2020 年,CentOS 宣布:计划未来将重心从 CentOS Linux 转移到 CentOS Stream.CentOS 8 的生命周期已于 2021 年 12 月 31 日终止,而 Ce ...

  10. mybatis plus 更新字段的时候设置为 null 后不生效

    mybatis plus 将属性设置为 null 值会被忽略,最终生成的 sql 中不会有 set field = null(可能是某些情况) mybatis-plus 更新字段的时候设置为 null ...