POI根据表头模板导出excel数据,并指定单个单元格样式,多sheet。

最近的公司需求,因为Excel表头样式较为复杂,不易直接用poi写出。
需要的Excel为这种:在这里插入图片描述
直接模板导出不能成为这样。

   
    public void exportCheckCsdn(HttpServletResponse response) {
        //获取到MNR 和 MNR-DT 的List
        
//        此处写 获取到指定list 的语句
        List<MnrExcelDTO> mnrExcelDTOS = new ArrayList<>();
        
        //sheet2,如果是单个sheet,不需要这个
        List<MnrDtExcelDTO> mnrDtExcelDTOS = new ArrayList<>();
//        list赋值

//excel模板路径
        File fi = null;
        try {
//            这边写的模板在resources下的 templates 下
            fi = ResourceUtils.getFile("classpath:templates/MNR_Check_Result.xlsx");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        XSSFWorkbook wb = null;
        try {
            //读取excel模板
            wb = new XSSFWorkbook(new FileInputStream(fi));
        } catch (IOException e) {
            e.printStackTrace();
        }
        XSSFSheet mnrSheet = wb.getSheetAt(0);
        XSSFSheet mnrSheetDt = wb.getSheetAt(1);
        
        
        //从第二行开始 sheet1的  ,因为第一行为模板
        for (int i = 2; i < mnrExcelDTOS.size() + 2; i++) {
            //获取行数据
            //根据字段名获取对应行数据
            MnrExcelDTO rowData = mnrExcelDTOS.get(i - 2);
            
            Row row = mnrSheet.getRow(i);
            if (row == null) {
                // 创建新行对象
                row = mnrSheet.createRow(i);
            }
            
            for (int j = 0; j < MnrExcelDTO.class.getDeclaredFields().length; j++) {
                // 获取当前单元格
                Cell cell = row.getCell(j);
                if (cell == null) {
                    // 如果单元格不存在,创建一个新的单元格对象
                    cell = row.createCell(j);
                }
                
                
                // 获取对应单元格的字段名称
                Field field = MnrExcelDTO.class.getDeclaredFields()[j];
                String fieldName = field.getName();
                // 获取字段对应的 getter 方法
                Method method = null;
                String cellValue = "";
                try {
                    //获取get方法
                    method = rowData.getClass().getMethod("get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1));
                    // 调用 getter 方法获取字段的属性值
                    Object value = method.invoke(rowData);
                    if (null == value) {
                        cellValue = "";
                    } else {
                        // 将值转换为字符串类型,并设置到单元格中
                        cellValue = String.valueOf(value);
                    }
                    cell.setCellValue(cellValue);
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
                
                
                CellStyle redCellStyle = null;
                
                    Object value = "";
                    try {
//                        如果字段名称不等于ckStatus,则设置单元格的样式
                        if (!"ckStatus".equals(fieldName)) {
                            
                            value = method.invoke(rowData);
                            
                            // 将值转换为字符串类型,并设置到单元格中
                            cellValue = String.valueOf(value);
                            cell.setCellValue(cellValue);
                        }
                    } catch (InvocationTargetException e) {
                        e.printStackTrace();
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                    
                    redCellStyle = wb.createCellStyle();
                    // 重点:从现有样式克隆style,只修改Font,其它style不变
                    redCellStyle.cloneStyleFrom(cell.getCellStyle());
                    // 获取原有字体
                    Font oldFont = wb.getFontAt(redCellStyle.getFontIndexAsInt());
                    // 创建新字体
                    Font redFont = wb.createFont();
                    // 重点:保留原字体样式
                    redFont.setFontName(oldFont.getFontName()); // 保留原字体
                    redFont.setFontHeightInPoints(oldFont.getFontHeightInPoints()); // 保留原字体高度
                    redFont.setBold(true); // 加粗
                    redFont.setColor(IndexedColors.RED.getIndex());  // 字体颜色:红色
                    // 设置红色字体
                    redCellStyle.setFont(redFont);
                    
            
                // 设置样式
                cell.setCellStyle(redCellStyle);
                
            }
        }
    
    
        //从第二行开始 sheet2的  ,因为第一行为模板   同上
        for (int i = 2; i < mnrDtExcelDTOS.size() + 2; i++) {
            //获取行数据
            //根据字段名获取对应行数据
            MnrDtExcelDTO rowData = mnrDtExcelDTOS.get(i - 2);
        
            Row row = mnrSheet.getRow(i);
            if (row == null) {
                // 创建新行对象
                row = mnrSheet.createRow(i);
            }
        
            for (int j = 0; j < MnrExcelDTO.class.getDeclaredFields().length; j++) {
                // 获取当前单元格
                Cell cell = row.getCell(j);
                if (cell == null) {
                    // 如果单元格不存在,创建一个新的单元格对象
                    cell = row.createCell(j);
                }
            
            
                // 获取对应单元格的字段名称
                Field field = MnrExcelDTO.class.getDeclaredFields()[j];
                String fieldName = field.getName();
                // 获取字段对应的 getter 方法
                Method method = null;
                String cellValue = "";
                try {
                    //获取get方法
                    method = rowData.getClass().getMethod("get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1));
                    // 调用 getter 方法获取字段的属性值
                    Object value = method.invoke(rowData);
                    if (null == value) {
                        cellValue = "";
                    } else {
                        // 将值转换为字符串类型,并设置到单元格中
                        cellValue = String.valueOf(value);
                    }
                    cell.setCellValue(cellValue);
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            
            
                CellStyle redCellStyle = null;
            
                Object value = "";
                try {
//                        如果字段名称不等于ckStatus,则设置单元格的样式
                    //也可以加上其他的判断,比如字符串末尾有 er 字符后缀,表示处理失败,给他变色即可
                    if (!"ckStatus".equals(fieldName)) {
                    
                        value = method.invoke(rowData);
                    
                        // 将值转换为字符串类型,并设置到单元格中
                        cellValue = String.valueOf(value);
                        cell.setCellValue(cellValue);
                    }
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            
                redCellStyle = wb.createCellStyle();
                // 重点:从现有样式克隆style,只修改Font,其它style不变
                redCellStyle.cloneStyleFrom(cell.getCellStyle());
                // 获取原有字体
                Font oldFont = wb.getFontAt(redCellStyle.getFontIndexAsInt());
                // 创建新字体
                Font redFont = wb.createFont();
                // 重点:保留原字体样式
                redFont.setFontName(oldFont.getFontName()); // 保留原字体
                redFont.setFontHeightInPoints(oldFont.getFontHeightInPoints()); // 保留原字体高度
                redFont.setBold(true); // 加粗
                redFont.setColor(IndexedColors.RED.getIndex());  // 字体颜色:红色
                // 设置红色字体
                redCellStyle.setFont(redFont);
            
            
                // 设置样式
                cell.setCellStyle(redCellStyle);
            
            }
        }
    
    
        ServletOutputStream outputStream = null;
        
        try {
            ExcelUtil.setResponse(response, "userName-today-check");
            outputStream = response.getOutputStream();
            wb.write(outputStream);
            outputStream.flush();
            outputStream.close();
            wb.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
  • 7
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值