POI导出复杂Excel,合并单元格(2)

/**
* 导出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)的更多相关文章
- Java poi导出设置 Excel某些单元格不可编辑
小白的总结,大神勿喷:需要转载请说明出处,如果有什么问题,欢迎留言 一.需求: 1.某一列 .某一行或某些单元格不可编辑,其他列可以编辑 二.期间遇到的问题 1.无法设置成不可编辑 2.设置为不可编辑 ...
- asp.net C#取Excel 合并单元格内容
asp教程.net c#取excel 合并单元格内容读取excel数据,填充dataset// 连接字符串 string xlspath = server.mappath("~/www.11 ...
- 使用POI创建word表格合并单元格兼容wps
poi创建word表格合并单元格代码如下: /** * @Description: 跨列合并 */ public void mergeCellsHorizontal(XWPFTable table, ...
- java poi导出Excel合并单元格并设置边框
import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; i ...
- poi导出excel合并单元格(包括列合并、行合并)
1 工程所需jar包如下:commons-codec-1.5.jarcommons-logging-1.1.jarlog4j-1.2.13.jarjunit-3.8.1.jarpoi-3.9-2012 ...
- poi excel 合并单元格
结论:final CellRangeAddress cra = new CellRangeAddress(rowId, rowId + rowSkip, colId, colId + c ...
- 前端Excel表格导入导出,包括合并单元格,表格自定义样式等
表格数据导入 读取导入Excel表格数据这里采用的是 xlsx 插件 npm i xlsx 读取excel需要通过 XLSX.read(data, {type: type}) 方法来实现,返回一个叫W ...
- npoi导出excel合并单元格
需要引用NPOI.dll程序集和Ionic.Zip.dll程序集 string[] headerRowName = { "序号", "地市", "镇街 ...
- NPOI之Excel——合并单元格、设置样式、输入公式、设置筛选等
首先建立一个空白的工作簿用作测试,并在其中建立空白工作表,在表中建立空白行,在行中建立单元格,并填入内容: //建立空白工作簿 IWorkbook workbook = new HSSFWorkboo ...
- NPOI之Excel——合并单元格、设置样式、输入公式
首先建立一个空白的工作簿用作测试,并在其中建立空白工作表,在表中建立空白行,在行中建立单元格,并填入内容: //建立空白工作簿 IWorkbook workbook = new HSSFWorkboo ...
随机推荐
- ArcGIS使用技巧(一)——数据存储
新手,若有错误还请指正! 日常接触ArcGIS较多,发现好多人虽然也在用ArcGIS,但一些基础的小技巧并不知道,写下来希望对大家有所帮助. ArcGIS默认的存储数据库是在C盘(图1),不修改存储数 ...
- Day 004:PAT练习--1033 旧键盘打字 (20 分)
题目要求如下: 我一开始理解的题意:第一行给出的是坏掉的键,这里的规则应该是这样的: 1."对应英文字母的坏键以大写给出",若有字母,则与其相关的字母全部不能输出,不论是大 ...
- js实现图片懒加载原理
原理 图片懒加载是前端页面优化的一种方式,在页面中有很多图片的时候,图片加载就需要很多时间,很耗费服务器性能,不仅影响渲染速度还会浪费带宽,为了解决这个问题,提高用户体验,所以就出现了懒加载这种方式来 ...
- 使用 Vert.X Future/Promise 编写异步代码
Future 和 Promise 是 Vert.X 4.0中的重要角色,贯穿了整个 Vert.X 框架.掌握 Future/Promise 的用法,是用好 Vert.X.编写高质量异步代码的基础.本文 ...
- [论文] FRCRN:利用频率递归提升特征表征的单通道语音增强
本文介绍了ICASSP2022 DNS Challenge第二名阿里和新加坡南阳理工大学的技术方案,该方案针对卷积循环网络对频率特征的提取高度受限于卷积编解码器(Convolutional Encod ...
- 通过代码解释什么是API,什么是SDK?
这个问题说来惭愧,读书时找实习面的第一家公司,问的第一个问题就是这个. 当时我没能说清楚,回去之后就上百度查.结果查了很久还是看不懂,然后就把这个问题搁置了. 谁知道毕业正式工作后,又再一次地面对了这 ...
- 手脱MoleBox(2.3.3-2.6.4)
1.查壳 2.找到OEP 对第二个Call使用ESP定律,再跳转后的位置进入第一个Call,这里就是OEP了,在这里直接dump的话会失败,那是因为MoleBox壳对IAT进行二次跳转,我们先在OEP ...
- css实现元素淡入淡出
@-webkit-keyframes fadeIn { 0% { opacity: 0; /*初始状态 透明度为0*/ } 50% { opacity: 0; /*中间状态 透明度为0*/ } 100 ...
- 好客租房30-事件绑定this指向(箭头函数)
1箭头函数 利用箭头函数自身不绑定this的特点 //导入react import React from 'react' import ReactDOM from 'rea ...
- 133_Power BI 报表服务器2020年1月版本更新亮点
博客:www.jiaopengzi.com 焦棚子的文章目录 请点击下载附件 一个很长的春节假期后,居家办公. 升级了Power BI 报表服务器(2020年1月版本). 具体的升级内容见官网博客: ...