当前位置: 首页 > news >正文

Excel导出纵向表格(poi)

导出表格头标签为纵向的Excel表格

1、service层

@Transactional(readOnly = true)
    public HSSFWorkbook projectExports(Integer curPage, Integer perPageSum, Integer projectId, String schemaId,
            HttpServletResponse response) throws InterruptedException, ExecutionException, Exception {

        // 分页
        Page page = new Page();
        page.setCurPage(curPage);
        page.setPerPageSum(perPageSum);
        RowBounds rowBounds = new RowBounds(page.getNextPage(), page.getPerPageSum());

   //获取多租户信息
        Users user = new Users();
        user.setSchemaId(schemaId);

        HSSFWorkbook workbook = new HSSFWorkbook();// 创建一个Excel文件

        // 样式设置
        HSSFCellStyle columnHeadStyle = (HSSFCellStyle) workbook.createCellStyle();
        columnHeadStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); // 背景
        columnHeadStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index); // 背景色
        columnHeadStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框
        columnHeadStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框
        columnHeadStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框
        columnHeadStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框
        columnHeadStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT);

        // 全局样式设置
        HSSFCellStyle allStyle = (HSSFCellStyle) workbook.createCellStyle();
        columnHeadStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); // 背景
        allStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());// 填充白色
        allStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框
        allStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框
        allStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框
        allStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框


        HSSFSheet sheet = workbook.createSheet("项目详情");// 创建一个Excel的Sheet

        ProjectDetailDto projectDetailDto = this.projectService.queryDetail(projectId, user);// 创建对象

        HSSFRow row0 = sheet.createRow(0);// 创建第一行
        HSSFCell cel0_1 = row0.createCell(0);// 创建第一行第一列
        cel0_1.setCellValue(new HSSFRichTextString("项目名称"));
        cel0_1.setCellStyle(columnHeadStyle);
        HSSFCell cel0_2 = row0.createCell(1);// 创建第一行第二列
        cel0_2.setCellValue(new HSSFRichTextString(projectDetailDto.getName()));
        cel0_2.setCellStyle(allStyle);
        HSSFCell cel0_3 = row0.createCell(2);
        cel0_3.setCellStyle(allStyle);
        HSSFCell cel0_4 = row0.createCell(3);
        cel0_4.setCellStyle(allStyle);
        HSSFCell cel0_5 = row0.createCell(4);
        cel0_5.setCellStyle(allStyle);

        HSSFRow row1 = sheet.createRow(1); // 创建第二行
        HSSFCell cell1_1 = row1.createCell(0); // 第二行第一列
        cell1_1.setCellValue(new HSSFRichTextString("客户名称"));
        cell1_1.setCellStyle(columnHeadStyle);
        HSSFCell cell1_2 = row1.createCell(1); // 第二行 第二列
        cell1_2.setCellValue(new HSSFRichTextString(projectDetailDto.getCustomer().getName()));
        cell1_2.setCellStyle(allStyle);
        HSSFCell cell1_3 = row1.createCell(2);
        cell1_3.setCellStyle(allStyle);
        HSSFCell cell1_4 = row1.createCell(3);
        cell1_4.setCellStyle(allStyle);
        HSSFCell cell1_5 = row1.createCell(4);
        cell1_5.setCellStyle(allStyle);

        HSSFRow row2 = sheet.createRow(2); // 创建第三行
        HSSFCell cell2_1 = row2.createCell(0); // 第三行第一列
        cell2_1.setCellValue(new HSSFRichTextString("项目状态"));
        cell2_1.setCellStyle(columnHeadStyle);
        HSSFCell cell2_2 = row2.createCell(1); // 第三行 第二列
        cell2_2.setCellValue(new HSSFRichTextString(projectDetailDto.getStateName()));
        cell2_2.setCellStyle(allStyle);
        HSSFCell cell2_3 = row2.createCell(2);
        cell2_3.setCellStyle(allStyle);
        HSSFCell cell2_4 = row2.createCell(3);
        cell2_4.setCellStyle(allStyle);
        HSSFCell cell2_5 = row2.createCell(4);
        cell2_5.setCellStyle(allStyle);

        HSSFRow row3 = sheet.createRow(3); // 创建第四行
        HSSFCell cell3_1 = row3.createCell(0); // 第四行第一列
        cell3_1.setCellValue(new HSSFRichTextString("项目总金额"));
        cell3_1.setCellStyle(columnHeadStyle);
        HSSFCell cell13_2 = row3.createCell(1); // 第四行 第二列
        cell13_2.setCellValue(new HSSFRichTextString(projectDetailDto.getAmount()));
        cell13_2.setCellStyle(allStyle);
        HSSFCell cell3_3 = row3.createCell(2);
        cell3_3.setCellStyle(allStyle);
        HSSFCell cell3_4 = row3.createCell(3);
        cell3_4.setCellStyle(allStyle);
        HSSFCell cell3_5 = row3.createCell(4);
        cell3_5.setCellStyle(allStyle);

        HSSFRow row4 = sheet.createRow(4); // 创建第五行
        HSSFCell cell4_1 = row4.createCell(0); // 第五行第一列
        cell4_1.setCellValue(new HSSFRichTextString("启动时间"));
        cell4_1.setCellStyle(columnHeadStyle);
        HSSFCell cell14_2 = row4.createCell(1); // 第五行 第二列
        cell14_2.setCellValue(new HSSFRichTextString(projectDetailDto.getStartTimeStr()));
        cell14_2.setCellStyle(allStyle);
        HSSFCell cell14_3 = row4.createCell(2);
        cell14_3.setCellStyle(allStyle);
        HSSFCell cell14_4 = row4.createCell(3);
        cell14_4.setCellStyle(allStyle);
        HSSFCell cell14_5 = row4.createCell(4);
        cell14_5.setCellStyle(allStyle);

        HSSFRow row5 = sheet.createRow(5); // 创建第六行
        HSSFCell cell5_1 = row5.createCell(0); // 第六行第一列
        cell5_1.setCellValue(new HSSFRichTextString("项目人员"));
        cell5_1.setCellStyle(columnHeadStyle);
        HSSFCell cell5_2 = row5.createCell(1);// 第六行 第二列
        if (projectDetailDto.getContacts().size() > 0) {
            cell5_2.setCellValue(new HSSFRichTextString(projectDetailDto.getContacts().get(0).getName()));
        }
        cell5_2.setCellStyle(allStyle);
        HSSFCell cell5_3 = row5.createCell(2);
        cell5_3.setCellStyle(allStyle);
        HSSFCell cell5_4 = row5.createCell(3);
        cell5_4.setCellStyle(allStyle);
        HSSFCell cell5_5 = row5.createCell(4);
        cell5_5.setCellStyle(allStyle);

        HSSFRow row6 = sheet.createRow(6); // 创建第七行
        HSSFCell cell6_1 = row6.createCell(0); // 第七行第一列
        cell6_1.setCellValue(new HSSFRichTextString("归属部门"));
        cell6_1.setCellStyle(columnHeadStyle);
        HSSFCell cell6_2 = row6.createCell(1);// 第七行 第二列
        if (projectDetailDto.getDeptOwner().size() != 0) {
            cell6_2.setCellValue(new HSSFRichTextString(projectDetailDto.getDeptOwner().get(0).getName()));
        }
        cell6_2.setCellStyle(allStyle);
        HSSFCell cell6_3 = row6.createCell(2);
        cell6_3.setCellStyle(allStyle);
        HSSFCell cell6_4 = row6.createCell(3);
        cell6_4.setCellStyle(allStyle);
        HSSFCell cell6_5 = row6.createCell(4);
        cell6_5.setCellStyle(allStyle);

        int rowCount = 7;//定义第七行为开始行
        HSSFRow row7 = sheet.createRow(rowCount); // 创建第八行
        HSSFCell row7_1 = row7.createCell(0);
        row7_1.setCellValue(new HSSFRichTextString("项目阶段"));
        row7_1.setCellStyle(columnHeadStyle);
        List<ProjectStepsDto> steps = projectDetailDto.getSteps();
        for (int i = 0; i < steps.size(); i++) {
            if (i == 0) {
                HSSFCell cell17_2 = row7.createCell(1);
                cell17_2.setCellValue(new HSSFRichTextString(steps.get(i).getMemo()));
                cell17_2.setCellStyle(allStyle);
                HSSFCell cell17_3 = row7.createCell(2);
                cell17_3.setCellValue(new HSSFRichTextString(steps.get(i).getTimeStr()));
                cell17_3.setCellStyle(allStyle);
                HSSFCell cell17_4 = row7.createCell(3);
                cell17_4.setCellStyle(allStyle);
                HSSFCell cell17_5 = row7.createCell(4);
                cell17_5.setCellStyle(allStyle);
                rowCount++;
            } else {
                HSSFRow forRow = sheet.createRow(rowCount);
                HSSFCell forRow_1 = forRow.createCell(0);
                forRow_1.setCellStyle(columnHeadStyle);
                HSSFCell forRow_2 = forRow.createCell(1);
                forRow_2.setCellValue(new HSSFRichTextString(steps.get(i).getMemo()));
                forRow_2.setCellStyle(allStyle);
                HSSFCell forRow_3 = forRow.createCell(2);
                forRow_3.setCellValue(new HSSFRichTextString(steps.get(i).getTimeStr()));
                forRow_3.setCellStyle(allStyle);

                HSSFCell forRow_4 = forRow.createCell(3);
                forRow_4.setCellStyle(allStyle);
                HSSFCell forRow_5 = forRow.createCell(4);
                forRow_5.setCellStyle(allStyle);
                rowCount++;
            }
        }
        if (steps.size() == 0) { // 如果steps没有数据 rowCount+1 表示显示标题 跳到下一行
            HSSFCell cell17_2 = row7.createCell(1);
            cell17_2.setCellStyle(allStyle);
            HSSFCell cell17_3 = row7.createCell(2);
            cell17_3.setCellStyle(allStyle);
            HSSFCell cell17_4 = row7.createCell(3);
            cell17_4.setCellStyle(allStyle);
            HSSFCell cell17_5 = row7.createCell(4);
            cell17_5.setCellStyle(allStyle);
            rowCount++;
        }

        HSSFRow rowPayment = sheet.createRow(rowCount); // 回款阶段
        HSSFCell rowPayment_1 = rowPayment.createCell(0);
        rowPayment_1.setCellValue(new HSSFRichTextString("回款阶段"));
        rowPayment_1.setCellStyle(columnHeadStyle);
        List<ProjectPaymentDto> Payments = projectDetailDto.getPayment();
        for (int j = 0; j < Payments.size(); j++) {
            if (j == 0) {
                HSSFCell cell1Payment_2 = rowPayment.createCell(1);
                cell1Payment_2.setCellValue(new HSSFRichTextString(Payments.get(j).getRate()));
                cell1Payment_2.setCellStyle(allStyle);
                HSSFCell cell1Payment_3 = rowPayment.createCell(2);
                cell1Payment_3.setCellValue(new HSSFRichTextString(Payments.get(j).getStateName()));
                cell1Payment_3.setCellStyle(allStyle);
                HSSFCell cell1Payment_4 = rowPayment.createCell(3);
                cell1Payment_4.setCellValue(new HSSFRichTextString(Payments.get(j).getTimeStr()));
                cell1Payment_4.setCellStyle(allStyle);

                HSSFCell cell1Payment_5 = rowPayment.createCell(4);
                cell1Payment_5.setCellStyle(allStyle);
                rowCount++;
            } else {
                HSSFRow forRow = sheet.createRow(rowCount);
                HSSFCell forCell_1 = forRow.createCell(0);
                forCell_1.setCellStyle(columnHeadStyle);
                HSSFCell forCell_2 = forRow.createCell(1);
                forCell_2.setCellValue(new HSSFRichTextString(Payments.get(j).getRate()));
                forCell_2.setCellStyle(allStyle);
                HSSFCell forCell_3 = forRow.createCell(2);
                forCell_3.setCellValue(new HSSFRichTextString(Payments.get(j).getStateName()));
                forCell_3.setCellStyle(allStyle);
                HSSFCell forCell_4 = forRow.createCell(3);
                forCell_4.setCellValue(new HSSFRichTextString(Payments.get(j).getTimeStr()));
                forCell_4.setCellStyle(allStyle);
                HSSFCell forCell_5 = forRow.createCell(4);
                forCell_5.setCellStyle(allStyle);
                rowCount++;
            }
        }
        if (Payments.size() == 0) { // 如果payments没有数据 rowCount+1 表示显示标题 跳到下一行
            HSSFCell cell1Payment_2 = rowPayment.createCell(1);
            cell1Payment_2.setCellStyle(allStyle);
            HSSFCell cell1Payment_3 = rowPayment.createCell(2);
            cell1Payment_3.setCellStyle(allStyle);
            HSSFCell cell1Payment_4 = rowPayment.createCell(3);
            cell1Payment_4.setCellStyle(allStyle);
            HSSFCell cell1Payment_5 = rowPayment.createCell(4);
            cell1Payment_5.setCellStyle(allStyle);
            rowCount++;
        }

        HSSFRow rowContacts = sheet.createRow(rowCount); // 联系人
        HSSFCell cellContacts_1 = rowContacts.createCell(0);
        cellContacts_1.setCellValue(new HSSFRichTextString("联系人"));
        cellContacts_1.setCellStyle(columnHeadStyle);
        List<ContactsListDto> Contacts = projectDetailDto.getContacts();
        for (int k = 0; k < Contacts.size(); k++) {
            List<ContactsInfoDto> contactsInfoList = Contacts.get(k).getContactsInfoList();
            String val = "";
            if (k == 0) {
                HSSFCell cellContacts_2 = rowContacts.createCell(1);
                cellContacts_2.setCellValue(new HSSFRichTextString(Contacts.get(k).getName()));
                cellContacts_2.setCellStyle(allStyle);
                HSSFCell cellContacts_3 = rowContacts.createCell(2);
                cellContacts_3.setCellValue(new HSSFRichTextString(Contacts.get(k).getDept()));
                cellContacts_3.setCellStyle(allStyle);
                HSSFCell cellContacts_4 = rowContacts.createCell(3);
                cellContacts_4.setCellValue(new HSSFRichTextString(Contacts.get(k).getRole()));
                cellContacts_4.setCellStyle(allStyle);
                for (int c = 0; c < contactsInfoList.size(); c++) {
                    val = val + "  " + contactsInfoList.get(c).getVal() + "  ";
                    val = val.substring(0, val.length() - 1);
                    HSSFCell cellContacts_5 = rowContacts.createCell(4);
                    cellContacts_5.setCellValue(new HSSFRichTextString(val));
                    cellContacts_5.setCellStyle(allStyle);
                }
                rowCount++;
            } else {
                HSSFRow forRow = sheet.createRow(rowCount);
                HSSFCell forCell_1 = forRow.createCell(0);
                forCell_1.setCellStyle(columnHeadStyle);
                HSSFCell forCell_2 = forRow.createCell(1);
                forCell_2.setCellValue(new HSSFRichTextString(Contacts.get(k).getName()));
                forCell_2.setCellStyle(allStyle);
                HSSFCell forCell_3 = forRow.createCell(2);
                forCell_3.setCellValue(new HSSFRichTextString(Contacts.get(k).getDept()));
                forCell_3.setCellStyle(allStyle);
                HSSFCell forCell_4 = forRow.createCell(3);
                forCell_4.setCellValue(new HSSFRichTextString(Contacts.get(k).getRole()));
                forCell_4.setCellStyle(allStyle);
                for (int c = 0; c < contactsInfoList.size(); c++) {
                    val = val + " " + contactsInfoList.get(c).getVal() + " ";
                    val = val.substring(0, val.length() - 1);
                    HSSFCell cellContacts_5 = rowContacts.createCell(4);
                    cellContacts_5.setCellValue(new HSSFRichTextString(val));
                    cellContacts_5.setCellStyle(allStyle);
                }
                rowCount++;
            }
        }
        if (Contacts.size() == 0) { // 如果contacts没有数据 rowCount+1 表示显示标题 跳到下一行
            HSSFCell cellContacts_2 = rowContacts.createCell(1);
            cellContacts_2.setCellStyle(allStyle);
            HSSFCell cellContacts_3 = rowContacts.createCell(2);
            cellContacts_3.setCellStyle(allStyle);
            HSSFCell cellContacts_4 = rowContacts.createCell(3);
            cellContacts_4.setCellStyle(allStyle);
            HSSFCell cellContacts_5 = rowContacts.createCell(4);
            cellContacts_5.setCellStyle(allStyle);
            rowCount++;
        }

        sheet.autoSizeColumn((short) 0);
        sheet.autoSizeColumn((short) 1);
        sheet.autoSizeColumn((short) 2);
        sheet.autoSizeColumn((short) 3);
        sheet.autoSizeColumn((short) 5);

        String filename = "项目详情.xls";// 设置下载时客户端Excel的名称
        filename = new String((filename).getBytes("GBK"), "ISO8859_1");
        response.setContentType("application/octet-stream;charset=UTF-8");
        response.addHeader("Content-Disposition", "attachment;filename=" + filename);
        OutputStream ouputStream = response.getOutputStream();
        workbook.write(ouputStream);
        ouputStream.flush();
        ouputStream.close();
        return workbook;
    }

 

转载于:https://www.cnblogs.com/lsz1349yw/p/poi20170515.html

相关文章:

  • POJ2584_T-Shirt Gumbo(二分图多重最大匹配/最大流)
  • 以精益的眼光重新关注电子商务
  • leetcode-000-序
  • cropper使用在线图片的问题
  • 在SAE搭建Python+Django+MySQL(基于Windows)
  • Java 单例模式
  • TP5分页类
  • 新CSS伪类:focus-within
  • 如果一个人
  • xmemcached 0.60 优化过程
  • 生产环境硬件使用总结
  • xmemcached发布1.1.2 (权重、noreply、spring集成)
  • tomcat8.5报错
  • Clojure世界:利用HouseMD诊断clojure
  • pat解题报告【1082】
  • 【跃迁之路】【585天】程序员高效学习方法论探索系列(实验阶段342-2018.09.13)...
  • 【知识碎片】第三方登录弹窗效果
  • Centos6.8 使用rpm安装mysql5.7
  • CODING 缺陷管理功能正式开始公测
  • gitlab-ci配置详解(一)
  • HTTP请求重发
  • JavaScript对象详解
  • 大整数乘法-表格法
  • 基于HAProxy的高性能缓存服务器nuster
  • 精益 React 学习指南 (Lean React)- 1.5 React 与 DOM
  • 聊聊redis的数据结构的应用
  • 爬虫进阶 -- 神级程序员:让你的爬虫就像人类的用户行为!
  • 前端每日实战:70# 视频演示如何用纯 CSS 创作一只徘徊的果冻怪兽
  • 我建了一个叫Hello World的项目
  • 终端用户监控:真实用户监控还是模拟监控?
  • C# - 为值类型重定义相等性
  • 宾利慕尚创始人典藏版国内首秀,2025年前实现全系车型电动化 | 2019上海车展 ...
  • 曾刷新两项世界纪录,腾讯优图人脸检测算法 DSFD 正式开源 ...
  • 整理一些计算机基础知识!
  • # 计算机视觉入门
  • (04)Hive的相关概念——order by 、sort by、distribute by 、cluster by
  • (附源码)ssm经济信息门户网站 毕业设计 141634
  • (一)认识微服务
  • (原+转)Ubuntu16.04软件中心闪退及wifi消失
  • .NET Core跨平台微服务学习资源
  • .NET 应用架构指导 V2 学习笔记(一) 软件架构的关键原则
  • .NET/C# 使用 #if 和 Conditional 特性来按条件编译代码的不同原理和适用场景
  • .NET6 命令行启动及发布单个Exe文件
  • :not(:first-child)和:not(:last-child)的用法
  • @Autowired标签与 @Resource标签 的区别
  • @Bean有哪些属性
  • @RequestBody的使用
  • [ 攻防演练演示篇 ] 利用通达OA 文件上传漏洞上传webshell获取主机权限
  • [202209]mysql8.0 双主集群搭建 亲测可用
  • [Android] 修改设备访问权限
  • [bzoj2957]楼房重建
  • [Flutter]设置应用包名、名称、版本号、最低支持版本、Icon、启动页以及环境判断、平台判断和打包
  • [IE技巧] 使IE8以单进程的模式运行
  • [Java]快速入门优先队列(堆)手撕相关面试题
  • [JS7] 显示从0到99的100个数字