POI导出复杂Excel,合并单元格(1)
/**
* 导出复杂excel 合并单元格 (HSSFWorkbook)
*/
@GetMapping("/testHSSFWorkbook.do")
public void testExport1(HttpServletResponse response)
throws Exception
{
/** 第一步,创建一个Workbook,对应一个Excel文件 */
HSSFWorkbook wb = new HSSFWorkbook(); /** 第二步,在Workbook中添加一个sheet,对应Excel文件中的sheet */
HSSFSheet sheet = wb.createSheet("南京市城镇社会保险参保人员花名册"); /** 第三步,设置样式以及字体样式*/
HSSFCellStyle titleStyle = createTitleCellStyle(wb);
HSSFCellStyle headerStyle = createHeadCellStyle(wb);
HSSFCellStyle contentStyle = createContentCellStyle(wb); /** 第四步,创建标题 ,合并标题单元格 */
// 行号
int rowNum = 0;
// 总列数
int totalColumn = 10;
// 创建第一页的第一行,索引从0开始
HSSFRow row0 = sheet.createRow(rowNum++);
row0.setHeight((short) 800);// 设置行高 String title = "南京市城镇社会保险参保人员花名册";
HSSFCell c00 = row0.createCell(0);
c00.setCellValue(title);
c00.setCellStyle(titleStyle);
// 合并单元格,参数依次为起始行,结束行,起始列,结束列 (索引0开始)
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, totalColumn));//标题合并单元格操作,11+1为总列数 // 第二行
HSSFRow row1 = sheet.createRow(rowNum++);
row1.setHeight((short) 500);
String[] row_first = {"单位名称(公章):", "", "", "", "", "", "", "", "劳动和社保保障证号(单位代码):", "", ""};
for (int i = 0; i < row_first.length; i++)
{
HSSFCell tempCell = row1.createCell(i);
tempCell.setCellStyle(headerStyle);
tempCell.setCellValue(row_first[i]);
} //第三行
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);
} //循环每一行数据
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);
}
} // 尾行备注1
HSSFRow foot1 = sheet.createRow(rowNum++);
foot1.setHeight((short) 300);
String[] row_foot1 = {"备注: 1、“参加工作时间”:指首次参加工作时间。", "", "", "", "", "", "",
"", "5、机关事业单位人员应注明是否参加养老保险。", "", ""};
for (int i = 0; i < row_foot1.length; i++)
{
HSSFCell tempCell = foot1.createCell(i);
tempCell.setCellValue(row_foot1[i]);
}
int remarkRowNum1 = dataList.size() + 3;
// 合并
sheet.addMergedRegion(new CellRangeAddress(remarkRowNum1, remarkRowNum1, 0, 2));//备注1
sheet.addMergedRegion(new CellRangeAddress(remarkRowNum1, remarkRowNum1, 8, 10));//备注5 // 尾行备注2
HSSFRow foot2 = sheet.createRow(rowNum++);
foot2.setHeight((short) 300);
String[] row_foot2 = {" 2、“进本单位参保时间”:指在本单位缴费起始时间。", "", "", "", "", "", "",
"", "6、本表一式两份,社会保险经办机构、缴费单位各留一份", "", ""};
for (int i = 0; i < row_foot2.length; i++)
{
HSSFCell tempCell = foot2.createCell(i);
tempCell.setCellValue(row_foot2[i]);
}
int remarkRowNum2 = dataList.size() + 4;
// 合并
sheet.addMergedRegion(new CellRangeAddress(remarkRowNum2, remarkRowNum2, 0, 2));//备注1
sheet.addMergedRegion(new CellRangeAddress(remarkRowNum2, remarkRowNum2, 8, 10));//备注5 // 尾行备注3
HSSFRow foot3 = sheet.createRow(rowNum++);
foot3.setHeight((short) 300);
String[] row_foot3 = {" 3、“月缴费基数”:指月平均收入总和。", "", "", "", "", "", "",
"", "7、社会保险政策咨询电话:12333", "", ""};
for (int i = 0; i < row_foot3.length; i++)
{
HSSFCell tempCell = foot3.createCell(i);
tempCell.setCellValue(row_foot3[i]);
}
int remarkRowNum3 = dataList.size() + 5;
// 合并
sheet.addMergedRegion(new CellRangeAddress(remarkRowNum3, remarkRowNum3, 0, 2));//备注1
sheet.addMergedRegion(new CellRangeAddress(remarkRowNum3, remarkRowNum3, 8, 10));//备注5 // 尾行备注4
HSSFRow foot4 = sheet.createRow(rowNum++);
foot4.setHeight((short) 300);
String[] row_foot4 = {" 4、跨社会保险结算年度办理补缴时须出具劳动合同原件、工资报表原件等相关资料。",
"", "", "", "", "", "", "", "8、南京人力资源和社会保障网址:www.njhrss.gov.cn", "", ""};
for (int i = 0; i < row_foot4.length; i++)
{
HSSFCell tempCell = foot4.createCell(i);
tempCell.setCellValue(row_foot4[i]);
}
int remarkRowNum4 = dataList.size() + 6;
// 合并
sheet.addMergedRegion(new CellRangeAddress(remarkRowNum4, remarkRowNum4, 0, 2));//备注1
sheet.addMergedRegion(new CellRangeAddress(remarkRowNum4, remarkRowNum4, 8, 10));//备注5 // 最后一行
HSSFRow foot = sheet.createRow(rowNum++);
foot.setHeight((short) 300);
String[] row_foot = {"单位负责人:", "", "填报人:", "", "联系电话:", "", "", "", "", "填报日期: 年 月 日", ""};
for (int i = 0; i < row_foot.length; i++)
{
HSSFCell tempCell = foot.createCell(i);
tempCell.setCellValue(row_foot[i]);
} for (int i = 0; i <= totalColumn; i++)
{
sheet.autoSizeColumn((short)i,true); //调整列宽
} //导出
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,合并单元格(1)的更多相关文章
- 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 ...
随机推荐
- 2021.11.02 eleveni的水省选题的记录
2021.11.02 eleveni的水省选题的记录 因为eleveni比较菜,所以eleveni决定从绿题开始水 --实际上菜菜的eleveni连绿题都不一定能水过/忍不住哭了 [P2217 HAO ...
- 被人DDoS攻击了,分析一下原理和防护
一.行业现象 1.1 为什么要攻击? 常见的,一个是同行恶意竞争,一个是敲诈勒索. 无论是传统行业的线下门店,还是互联网行业的门户网站.APP产品,都存在着竞争关系,争相获得更多客源,究其目的,无非是 ...
- Markdown学习-Typora
author:涂勇军 标题 (#加一个空格)一级标题 (##加一个空格)二级标题 (###加一个空格)三级标题 (####加一个空格)四级标题 字体 加粗:** hello,World **(快捷键是 ...
- 开发一款让我们慢慢变好的微信小程序
1. 前言 朋友,你还记得你想学编程最初的目的是什么吗? 先说说我的吧,我最初想学编程的目的只有一点,感觉编程很酷,会写代码的人很厉害!.随着后面参加工作,我马上产生了让我能够在编程这条路上继续走下去 ...
- 关于JS精度缺失问题
问题描述 在Java后端传一个比较大的Long值的时候 前端接收值的时候会出现精度的缺失: 解决办法 添加一个转换类 点击查看代码 public class JacksonObjectMapper e ...
- 评价管理后台PC端
1.css动画效果 --2020.12.26 2.remove() --2020.12.28 3.执行顺序 --2020.12.30 4.联动 --2021.01.06 5.奥利给~ --202 ...
- UART串口及Linux实现
UART,全称Universal Asynchronous Receiver Transmitter,通用异步收发器,俗称串口.作为最常用的通信接口之一,从8位单片机到64位SoC,一般都会提供UAR ...
- Spring Ioc源码分析系列--Ioc容器BeanFactoryPostProcessor后置处理器分析
Spring Ioc源码分析系列--Ioc容器BeanFactoryPostProcessor后置处理器分析 前言 上一篇文章Spring Ioc源码分析系列--Ioc源码入口分析已经介绍到Ioc容器 ...
- 98. 验证二叉搜索树 前序遍历解法以及后续遍历解法(go语言)
leetcode题目 98. 验证二叉搜索树 前序遍历 最简洁的答案版本,由于先判断的是根节点,所以直接判断当前root的值v,是否满足大于左子树最大,小于右子树最小,然后再遍历左子树,右子树是否是这 ...
- (原创)[C#] MEF 主程序与插件加载不同版本的DLL
一.前言 MEF(Managed Extensibility Framework),是轻量级的插件框架.使用简单,功能强大.详细介绍见MSDN,本文不再赘述. 在使用MEF时,会遇到这样一种场景: 主 ...