HSSFWorkbook xssfWorkbook = new HSSFWorkbook(); //创建工作表对象Sheet sheet = xssfWorkbook.createSheet(); //创建工作表HSSFCellStyle columnTopStyle = this.getColumnTopStyle(xssfWorkbook);// 获取列头样式对象columnTopStyle.setWrapText(true);HSSFCellStyle cellStyle = xssfWorkbook.createCellStyle();//新建单元格样式//边框cellStyle.setBorderBottom(BorderStyle.THIN);; //下边框  cellStyle.setBorderLeft(BorderStyle.THIN);//左边框  cellStyle.setBorderTop(BorderStyle.THIN);//上边框  cellStyle.setBorderRight(BorderStyle.THIN);//右边框HSSFCellStyle style = this.getStyle(xssfWorkbook); // 获取单元格样式对象sheet.addMergedRegion(new CellRangeAddress(1, 3, 0, 0));//0--8sheet.addMergedRegion(new CellRangeAddress(1, 3, 1, 1));//9--14sheet.addMergedRegion(new CellRangeAddress(1, 3, 2, 2));//15--20Row bthead = sheet.createRow(1);cyly.add("当月合计");Row bt = sheet.createRow(2);// 产生表格标题行Cell cellhead2 = bt.createCell(0);cellhead2.setCellStyle(cellStyle); //style为带边框的样式 上面有定义cellhead2.setCellValue("");Cell createCell = bt.createCell(1);createCell.setCellStyle(cellStyle); //style为带边框的样式 上面有定义createCell.setCellValue("");Row bt3 = sheet.createRow(3);Cell cellhead3 = bt3.createCell(0);cellhead3.setCellStyle(cellStyle); //style为带边框的样式 上面有定义cellhead3.setCellValue("");Cell createCell2 = bt3.createCell(1);createCell2.setCellStyle(cellStyle); //style为带边框的样式 上面有定义createCell2.setCellValue("");int t1 = 0;int t2 = 0;int t3 = 0;for (int i = 0; i < cyly.size(); i++) {t1 = (i+1)*3;t2 = (i+1)*3+1;t3 = (i+2)*3-1;sheet.addMergedRegion(new CellRangeAddress(2, 2, t1,t3 ));Cell cell = bt.createCell(t1);cell.setCellType(CellType.STRING); // 设置列头单元格的数据类型cell.setCellStyle(columnTopStyle); // 设置列头单元格样式HSSFRichTextString text = new HSSFRichTextString(cyly.get(i));cell.setCellValue(text);Cell createCell3 = bt.createCell(t2);createCell3.setCellStyle(cellStyle); //style为带边框的样式 上面有定义createCell3.setCellValue("");Cell createCell4 = bt.createCell(t3);createCell4.setCellStyle(cellStyle); //style为带边框的样式 上面有定义createCell4.setCellValue("");Cell cellSl = bt3.createCell(t1);cellSl.setCellType(CellType.STRING); // 设置列头单元格的数据类型cellSl.setCellStyle(columnTopStyle); // 设置列头单元格样式HSSFRichTextString textSl = new HSSFRichTextString("专利预审服务受理(件)");cellSl.setCellValue(textSl);Cell cellHg = bt3.createCell(t2);cellHg.setCellType(CellType.STRING); // 设置列头单元格的数据类型cellHg.setCellStyle(columnTopStyle); // 设置列头单元格样式HSSFRichTextString textHg = new HSSFRichTextString("专利预审合格量(件)");cellHg.setCellValue(textHg);Cell cellSq = bt3.createCell(t3);cellSq.setCellType(CellType.STRING); // 设置列头单元格的数据类型cellSq.setCellStyle(columnTopStyle); // 设置列头单元格样式HSSFRichTextString textSq = new HSSFRichTextString("专利授权量(件)");cellSq.setCellValue(textSq);}//t3表示预审服务量的最后一列sheet.addMergedRegion(new CellRangeAddress(1, 1, 3, t3));sheet.addMergedRegion(new CellRangeAddress(1, 1, t3+1, t3+2));for (int n = 0; n < rowName.length; n++) {Cell cellRowName = null;if(n==4) {cellRowName = bthead.createCell(t3+1);}else {cellRowName = bthead.createCell(n); // 创建列头对应个数的单元格}cellRowName.setCellType(CellType.STRING); // 设置列头单元格的数据类型   cellRowName.setCellStyle(columnTopStyle); // 设置列头单元格样式HSSFRichTextString text = new HSSFRichTextString(rowName[n]);cellRowName.setCellValue(text); // 设置列头单元格的值}Cell createCell3 = bthead.createCell(t3+2);createCell3.setCellStyle(cellStyle); //style为带边框的样式 上面有定义createCell3.setCellValue("");Cell createCell4 = bt3.createCell(t3+2);createCell4.setCellStyle(cellStyle); //style为带边框的样式 上面有定义createCell4.setCellValue("");//标题sheet.addMergedRegion(new CellRangeAddress(0,0,0,t3+2));  //36  Row head = sheet.createRow(0);head.setHeight((short) 750); //设置表格标题的高度Cell cellhead = head.createCell(0);cellhead.setCellType(CellType.STRING); // 设置列头单元格的数据类型   cellhead.setCellStyle(columnTopStyle);HSSFRichTextString texthead = new HSSFRichTextString(title);cellhead.setCellValue(texthead);for (int j = 1; j <= t3+2; j++) {cellhead = head.createCell(j);cellhead.setCellStyle(cellStyle); //style为带边框的样式 上面有定义cellhead.setCellValue("");}//预审周期sheet.addMergedRegion(new CellRangeAddress(2, 3, t3+1, t3+1));sheet.addMergedRegion(new CellRangeAddress(2, 3, t3+2, t3+2));Cell cell = bt.createCell(t3+1);cell.setCellType(CellType.STRING); // 设置列头单元格的数据类型cell.setCellStyle(columnTopStyle); // 设置列头单元格样式HSSFRichTextString text = new HSSFRichTextString("当月平均预审周期(天)");cell.setCellValue(text);Cell cell2 = bt.createCell(t3+2);cell2.setCellType(CellType.STRING); // 设置列头单元格的数据类型cell2.setCellStyle(columnTopStyle); // 设置列头单元格样式HSSFRichTextString text2 = new HSSFRichTextString("预审周期超7个工作日案件量(件)");cell2.setCellValue(text2);Row btXj = null;int n = 0;int m = 0;for (int j = 0; j < dataList.size(); j++) {n = (j+1)*4;m = (j+2)*4-1;sheet.addMergedRegion(new CellRangeAddress(n, m, 0, 0));sheet.addMergedRegion(new CellRangeAddress(n, m, t3+1, t3+1));Row btFm = sheet.createRow(n);Cell cellMot = btFm.createCell(0);cellMot.setCellType(CellType.STRING); // 设置列头单元格的数据类型cellMot.setCellStyle(columnTopStyle); // 设置列头单元格样式//当月平均预审周期(天) Cell cellAvg = btFm.createCell(t3+1);cellAvg.setCellType(CellType.STRING); // 设置列头单元格的数据类型cellAvg.setCellStyle(columnTopStyle); // 设置列头单元格样式HSSFRichTextString textAvg = new HSSFRichTextString(dataList.get(j).getAvg());cellAvg.setCellValue(textAvg);HSSFRichTextString textMot = null;String[] start = startM.split("-");String[] end = endM.split("-");if(start[0].equals(end[0])) {String mc = dataList.get(j).getMc();textMot = new HSSFRichTextString(mc.split("年")[1]);}else {textMot = new HSSFRichTextString(dataList.get(j).getMc());}cellMot.setCellValue(textMot);Cell cellFm = btFm.createCell(1);cellFm.setCellType(CellType.STRING); // 设置列头单元格的数据类型cellFm.setCellStyle(columnTopStyle); // 设置列头单元格样式HSSFRichTextString textFm = new HSSFRichTextString("发明");cellFm.setCellValue(textFm);Row btSyxx = sheet.createRow(n+1);Cell cellSyxx = btSyxx.createCell(1);cellSyxx.setCellType(CellType.STRING); // 设置列头单元格的数据类型cellSyxx.setCellStyle(columnTopStyle); // 设置列头单元格样式HSSFRichTextString textSyxx = new HSSFRichTextString("实用新型");cellSyxx.setCellValue(textSyxx);Row btWg = sheet.createRow(n+2);Cell cellWg = btWg.createCell(1);cellWg.setCellType(CellType.STRING); // 设置列头单元格的数据类型cellWg.setCellStyle(columnTopStyle); // 设置列头单元格样式HSSFRichTextString textWg = new HSSFRichTextString("外观设计");cellWg.setCellValue(textWg);btXj = sheet.createRow(n+3);Cell cellXj = btXj.createCell(1);cellXj.setCellType(CellType.STRING); // 设置列头单元格的数据类型cellXj.setCellStyle(columnTopStyle); // 设置列头单元格样式HSSFRichTextString textXj = new HSSFRichTextString("小计");cellXj.setCellValue(textXj);List<IppcYsMonthWorkTjTwoResponse> data = dataList.get(j).getData();for (int i = 0; i < data.size(); i++) {List<String> list = data.get(i).getData();for (int k = 0; k < list.size(); k++) {if(i==0) {Cell cellFmNum = btFm.createCell(2+k);cellFmNum.setCellType(CellType.STRING); // 设置列头单元格的数据类型cellFmNum.setCellStyle(columnTopStyle); // 设置列头单元格样式HSSFRichTextString textFmNum = new HSSFRichTextString(list.get(k));cellFmNum.setCellValue(textFmNum);}else if(i==1) {Cell cellSyxxNum = btSyxx.createCell(2+k);cellSyxxNum.setCellType(CellType.STRING); // 设置列头单元格的数据类型cellSyxxNum.setCellStyle(columnTopStyle); // 设置列头单元格样式HSSFRichTextString textSyxxNum = new HSSFRichTextString(list.get(k));cellSyxxNum.setCellValue(textSyxxNum);}else if(i==2) {Cell cellWgNum = btWg.createCell(2+k);cellWgNum.setCellType(CellType.STRING); // 设置列头单元格的数据类型cellWgNum.setCellStyle(columnTopStyle); // 设置列头单元格样式HSSFRichTextString textWgNum = new HSSFRichTextString(list.get(k));cellWgNum.setCellValue(textWgNum);}else if(i==3) {Cell cellXjNum = btXj.createCell(2+k);cellXjNum.setCellType(CellType.STRING); // 设置列头单元格的数据类型cellXjNum.setCellStyle(columnTopStyle); // 设置列头单元格样式HSSFRichTextString textXjNum = new HSSFRichTextString(list.get(k));cellXjNum.setCellValue(textXjNum);}}//预审周期超7个工作日案件量(件)if(i==0) {Cell cellOver7 = btFm.createCell(t3+2);cellOver7.setCellType(CellType.STRING); // 设置列头单元格的数据类型cellOver7.setCellStyle(columnTopStyle); // 设置列头单元格样式HSSFRichTextString textOver7 = new HSSFRichTextString(data.get(i).getOver7());cellOver7.setCellValue(textOver7);}else if(i==1) {Cell cellOver7Syxxs = btSyxx.createCell(t3+2);cellOver7Syxxs.setCellType(CellType.STRING); // 设置列头单元格的数据类型cellOver7Syxxs.setCellStyle(columnTopStyle); // 设置列头单元格样式HSSFRichTextString textOver7Syxxs = new HSSFRichTextString(data.get(i).getOver7());cellOver7Syxxs.setCellValue(textOver7Syxxs);}else if(i==2) {Cell cellOver7Wgs = btWg.createCell(t3+2);cellOver7Wgs.setCellType(CellType.STRING); // 设置列头单元格的数据类型cellOver7Wgs.setCellStyle(columnTopStyle); // 设置列头单元格样式HSSFRichTextString textOver7Wgs = new HSSFRichTextString(data.get(i).getOver7());cellOver7Wgs.setCellValue(textOver7Wgs);}else if(i==3) {Cell cellOver7Xjs = btXj.createCell(t3+2);cellOver7Xjs.setCellType(CellType.STRING); // 设置列头单元格的数据类型cellOver7Xjs.setCellStyle(columnTopStyle); // 设置列头单元格样式HSSFRichTextString textOver7Xjs = new HSSFRichTextString(data.get(i).getOver7());cellOver7Xjs.setCellValue(textOver7Xjs);}}}Cell createCell5 = btXj.createCell(0);createCell5.setCellStyle(cellStyle); //style为带边框的样式 上面有定义createCell5.setCellValue("");Cell createCell6 = btXj.createCell(t3+1);createCell6.setCellStyle(cellStyle); //style为带边框的样式 上面有定义createCell6.setCellValue("");// 设置字体HSSFFont font = xssfWorkbook.createFont();// 设置字体大小font.setFontHeightInPoints((short) 11);// 字体加粗font.setBold(true);// 设置字体名字font.setFontName("Courier New");HSSFCellStyle styles = xssfWorkbook.createCellStyle();styles.setAlignment(HorizontalAlignment.LEFT);styles.setFont(font);sheet.addMergedRegion(new CellRangeAddress(m+1, m+1, 0, 12));Row createRow = sheet.createRow(m+1);Cell createCell7 = createRow.createCell(0);createCell7.setCellType(CellType.STRING); // 设置列头单元格的数据类型createCell7.setCellStyle(styles); // 设置列头单元格样式HSSFRichTextString textJS = new HSSFRichTextString("xxxxxxxxxxxxxxxxxxx");createCell7.setCellValue(textJS);sheet.addMergedRegion(new CellRangeAddress(m+2, m+2, 0, 12));Row createRow2 = sheet.createRow(m+2);Cell createCell8 = createRow2.createCell(0);createCell8.setCellType(CellType.STRING); // 设置列头单元格的数据类型createCell8.setCellStyle(styles); // 设置列头单元格样式HSSFRichTextString textSL = new HSSFRichTextString("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");createCell8.setCellValue(textSL);sheet.addMergedRegion(new CellRangeAddress(m+3, m+3, 0, 12));Row createRow3 = sheet.createRow(m+3);Cell createCell9 = createRow3.createCell(0);createCell9.setCellType(CellType.STRING); // 设置列头单元格的数据类型createCell9.setCellStyle(styles); // 设置列头单元格样式HSSFRichTextString textHG = new HSSFRichTextString("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");createCell9.setCellValue(textHG);sheet.addMergedRegion(new CellRangeAddress(m+4, m+4, 0, 12));Row createRow4 = sheet.createRow(m+4);Cell createCell10 = createRow4.createCell(0);createCell10.setCellType(CellType.STRING); // 设置列头单元格的数据类型createCell10.setCellStyle(styles); // 设置列头单元格样式HSSFRichTextString textSQ = new HSSFRichTextString("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");createCell10.setCellValue(textSQ);sheet.addMergedRegion(new CellRangeAddress(m+5, m+5, 0, 12));Row createRow5 = sheet.createRow(m+5);Cell createCell11 = createRow5.createCell(0);createCell11.setCellType(CellType.STRING); // 设置列头单元格的数据类型createCell11.setCellStyle(styles); // 设置列头单元格样式HSSFRichTextString textZQ = new HSSFRichTextString("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");createCell11.setCellValue(textZQ);sheet.createFreezePane(0, 4, 0, 4);//冻结for (int j = 3; j <= t3+2; j++) {sheet.setColumnWidth(j, 10*256);}sheet.setDefaultColumnWidth(10);xssfWorkbook.write(out);

效果图

核心代码

其中最核心的代码如下:

HSSFWorkbook xssfWorkbook = new HSSFWorkbook(); //创建工作表对象
Sheet sheet = xssfWorkbook.createSheet(); //创建工作表HSSFCellStyle styles = xssfWorkbook.createCellStyle();
styles.setAlignment(HorizontalAlignment.LEFT);
styles.setFont(font);//通过下面这两行就可以锁定到具体的单元格了,之后就可以对其进行设置字体、样式等。
Row createRow5 = sheet.createRow(m+5);//创建行,其中参数为要创建的行数,从0开始算
Cell createCell11 = createRow5.createCell(0);//创建列,其中参数为要创建的列数,从0开始算createCell11.setCellType(CellType.STRING); // 设置列头单元格的数据类型
createCell11.setCellStyle(styles); // 设置列头单元格样式
HSSFRichTextString textZQ = new HSSFRichTextString("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
createCell11.setCellValue(textZQ);//向单元格中存值

大体思路就是通过sheet对象创建对应的行,再通过行创建对应的列,然后就可以存值了。

如果想设置样式,可以通过createCellStyle()先获取样式对象,之后就可以通过该对象设置想要的样式。如上面的styles.setAlignment(HorizontalAlignment.LEFT);styles.setFont(font);。其中font是字体。

想要的样式创建好后,就可以通过createCell11.setCellStyle(styles);方法来设置对应的样式了。

其中还有冻结createFreezePane()、合并单元格sheet.addMergedRegion(new CellRangeAddress(m+5, m+5, 0, 12));等方法可以通过官方文档或者查阅资料来获得。

java poi导出Excel表格(动态表头)相关推荐

  1. java poi导出Excel表格超大数据量解决方案

    Java实现导出excel表格功能,大部分都会使用apache poi,apache poi API 地址 POI之前的版本不支持大数据量处理,如果数据过多则经常报OOM错误,有时候调整JVM大小效果 ...

  2. excel导出java不完整_有关Java POI导出excel表格中,单元格合并之后显示不全的解决方法。...

    我在table变换excel之后发现合并的单元格不能显示全部边框,在网上找了半天解决方案,终于解决了.具体解决代码如下; /** * 设置合并单元格的边框样式 * * @param sheet 当前表 ...

  3. java实现这个无表头空表格,《java程序导出excel表格是空白的没有数据?》 java怎么导入数据库...

    java程序导出excel表格是空白的没有数据? 看一下是否可以链接上数据库,或者查看一下链接的数据库是否有数据 java从数据库中导出excel poi 建议你使用pageoffice. java ...

  4. java中poi导出Excel表格(前台流文件接收)

    java中poi导出Excel表格,前端以流的方式接收,而非直接生成文件再下载,解决多台服务器部署后,路径地址不统一导致的下载问题. 生成Excel示例图: 2.代码说明 ① 在上次的基础上增加了底部 ...

  5. Java web项目利用POI导出EXCEL表格

    SSH2 POI导出EXCEL表格 1.首先导入poi的jar包 HSSFWorkbook :工作簿,代表一个excel的整个文档 HSSFSheet:工作表 HSSFRow :行 HSSFCell: ...

  6. java 导入导出excel表格

    java 导入导出excel表格 业务上有需求上传excel表格并读取内容,本文记录一下该方法 表格导入 引入相应的工具包 <dependency><groupId>cn.af ...

  7. java实现导出Excel多行表头复杂模板

    java实现导出Excel多行表头复杂模板 一般我们都会选择poi来导出,选择一个比较好的ExcelUtils 但是对于初学者而言不了解poi的,还需从poi API文档去学习,如果是自学的话更好,如 ...

  8. java poi 导出excel不能超过65536行

    java poi 导出excel不能超过65536行 报这个异常 Exception in thread "main" java.lang.IllegalArgumentExcep ...

  9. POI导出Excel表格,去掉数字框的左上角绿色的小三角

    在用POI导出Excel表格,数字框的左上角有个绿色的小三角,看着很不舒服,如下图所示: 目的:当单元格为数字时,不要转为文本输出,就直接按数字格式输出,去掉左上角的绿色小三角形. 解决办法:在网上找 ...

最新文章

  1. 使用keras的cifar10.load_data()总是会自动下载问题的解决
  2. 信息系统项目管理师考试公式都在这里了
  3. 如何动态添加修改删除定时任务
  4. Tensorflow之安装
  5. 给Android程序员的一些面试建议,帮你突破瓶颈
  6. console_init_r()函数分析
  7. 半年成java大佬_通过自学60天成为java大佬 第一天 知识点总结 数据类型
  8. c语言事件结构体,C语言结构体史上最详细的讲解
  9. FPGA开平方的实现
  10. 【转】关于DLL的一点介绍
  11. python pip下载安装教程_windows下python安装pip图文教程
  12. arcgis软件界面字体太小
  13. 如何玩转抖音吸粉引流,老路子新热点照样1000+
  14. 在字节实习8个月后,成功转正
  15. 鹰眼摄像头(OV7725)的使用
  16. 面试题 10.11. 峰与谷-快速排序
  17. GWAS-性状间相关性图的绘制
  18. “无线大连”喷薄欲出
  19. .Audio Focus机制以及AudioManager的使用
  20. 每周全球科技十大新闻(2021.7.5-7.11)

热门文章

  1. 运维老兵对运维中常见技术类问题剖析
  2. 笔记本外接显示器后分辨率无法调回到正常模式
  3. netstat查看Linux端口状态
  4. 求图中的最大独立集或最大团(UVA 193)
  5. 毕业设计-基于 MATLAB 的工业机器人运动学分析与实现
  6. gSOAP 入门实践(一)
  7. 最优控制问题matlab编程实例,第十二章用matlab解最优控制问题及应用实例.ppt
  8. 第09讲 推断未知:统计推断的基本框架
  9. vue中使用qrcode生成二维码并实现打印功能
  10. 金蝶Kis旗舰版不能反过账?