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();
        }
    }
    

最近更新

  1. TCP协议是安全的吗?

    2023-12-28 15:38:04       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-28 15:38:04       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-28 15:38:04       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-28 15:38:04       20 阅读

热门阅读

  1. C#(Unity)循环遍历Dictionary,并修改内容或删除内容

    2023-12-28 15:38:04       27 阅读
  2. 力扣热题100道-哈希篇

    2023-12-28 15:38:04       39 阅读
  3. 你会画菱形吗3044:练9.1 字符菱形

    2023-12-28 15:38:04       40 阅读
  4. 兔子的序列

    2023-12-28 15:38:04       35 阅读
  5. 低时延,可扩展的 l4s 拥塞控制算法

    2023-12-28 15:38:04       39 阅读
  6. LeetCode 75| 位运算

    2023-12-28 15:38:04       40 阅读
  7. C#高级 03委托

    2023-12-28 15:38:04       30 阅读
  8. C语言中的Do While循环:深度解析与实践应用

    2023-12-28 15:38:04       35 阅读