easypoi設置單元格樣式


左邊是 easypoi 導出的默認格式,右邊是自定義格式

   

   

上述的轉換其實都是在 "導出前一步" 進行完成的,所以開發基本流程為 先做好導出,在增加樣式

主要代碼為:

//-------------------------------設置單元格樣式開始-------------------------------------

                //獲取sheetAt對象,這里一個sheetAt所以角標是0

                Sheet sheetAt = workbook.getSheetAt(0);

                // 表格行數

                int lastRowNum = sheetAt.getLastRowNum();

                // 獲取列數

                int physicalNumberOfCells = sheetAt.getRow(0).getPhysicalNumberOfCells();

                //開始遍歷單元格並進行判斷是否渲染

                for (int i = 2 ; i <= lastRowNum ; i++) {

                    //獲取每行對象

                    Row row = sheetAt.getRow(i);

                    for (int j = 1; j < physicalNumberOfCells; j++) {

                        //獲取單元格對象

                        Cell cell = row.getCell(j) ;

   

                        //獲取單元格樣式對象

                        CellStyle cellStyle = workbook.createCellStyle();

   

                        //獲取單元格內容對象

                        Font font = workbook.createFont();

                        //一定要裝入 樣式中才會生效

                        cellStyle.setFont(font);

   

                        //獲取當前單元格 中的value

                        int score  = Integer.valueOf(map.get(String.valueOf(i - 2)).get(j));

                        if (score > 90 && score < 95){

                            //設置單元格背景顏色

                            cellStyle.setFillForegroundColor(IndexedColors.RED.getIndex());

                            cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);

                            //設置單元格字體顏色

                            font.setColor(IndexedColors.YELLOW.getIndex());

                        }

                        cell.setCellStyle(cellStyle);

                    }

   

                }

//-------------------------------設置單元格樣式完成--------------------------------------

   

實例

/**

     * 場景,假設現在要導出 學生成績表的Excel文件

     * 現在需要對其中 分數大於 90分 同時 小於95分 的同學 標記為 紅色。

     */

    @GetMapping(value = "/api/jncsCarFlow/ExportStudentScore")

    @ApiOperation(value = "導出檢測器監測記錄", notes = "導出檢測器監測記錄")

    public void exportStudentScore(HttpServletResponse response) {

        try {

            Map<String, List<String>> map = getData();

            //表頭

            List<ExcelExportEntity> headList = new ArrayList<>();

            ExcelExportEntity excelExportEntity;

            for (int i = 0; i < map.get("headList").size(); i++) {

                excelExportEntity = new ExcelExportEntity(map.get("headList").get(i), map.get("keyHeadList").get(i));

                headList.add(excelExportEntity);

            }

            //數據

            List<Map<String, Object>> list = new ArrayList<>();

            Map<String, Object> mapData;

            for (int i = 0; i < 9; i++) {

                mapData = new HashMap<>();

                List<String> data = map.get(i + "");

                mapData.put("name", data.get(0));

                mapData.put("languageScore", Integer.valueOf(data.get(1)));

                mapData.put("mathScore", Integer.valueOf(data.get(2)));

                mapData.put("englishScore", Integer.valueOf(data.get(3)));

                list.add(mapData);

            }

   

            //導出

            String excelName = "StudentScoreName" + System.currentTimeMillis() + ".xls";

            if (CollectionUtils.isNotEmpty(list)) {

                response.setCharacterEncoding("UTF-8");

                response.setHeader("content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

                response.setHeader("Content-Disposition",

                        "attachment;filename=" + URLEncoder.encode(excelName, "UTF-8"));

                ExportParams params = new ExportParams("Excel的title", "sheet的Name");

                Workbook workbook = ExcelExportUtil.exportExcel(params, headList, list);

                //-------------------------------設置單元格樣式開始-------------------------------------

                //獲取sheetAt對象,這里一個sheetAt所以角標是0

                Sheet sheetAt = workbook.getSheetAt(0);

                // 表格行數

                int lastRowNum = sheetAt.getLastRowNum();

                // 獲取列數

                int physicalNumberOfCells = sheetAt.getRow(0).getPhysicalNumberOfCells();

                //開始遍歷單元格並進行判斷是否渲染

                for (int i = 2; i <= lastRowNum; i++) {

                    //獲取每行對象

                    Row row = sheetAt.getRow(i);

                    for (int j = 1; j < physicalNumberOfCells; j++) {

                        //獲取單元格對象

                        Cell cell = row.getCell(j);

   

                        //獲取單元格樣式對象

                        CellStyle cellStyle = workbook.createCellStyle();

   

                        //獲取單元格內容對象

                        Font font = workbook.createFont();

                        //一定要裝入 樣式中才會生效

                        cellStyle.setFont(font);

   

                        //獲取當前單元格 中的value

                        int score = Integer.valueOf(map.get(String.valueOf(i - 2)).get(j));

                        if (score > 90 && score < 95) {

                            //設置單元格背景顏色

                            cellStyle.setFillForegroundColor(IndexedColors.RED.getIndex());

                            cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);

                            //設置單元格字體顏色

                            font.setColor(IndexedColors.YELLOW.getIndex());

                        }

                        cell.setCellStyle(cellStyle);

                    }

   

                }

                //-------------------------------設置單元格樣式完成-------------------------------------

                workbook.write(response.getOutputStream());

            }

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

   

    /*

     * 生成數據

     * */

    public Map<String, List<String>> getData() {

        Map<String, List<String>> map = new HashMap<>();

   

        List<String> headList = Arrays.asList("姓名", "語文", "數學", "英語");

        map.put("headList", headList);

        List<String> keyHeadList = Arrays.asList("name", "languageScore", "mathScore", "englishScore");

        map.put("keyHeadList", keyHeadList);

   

        Integer languageScore = 88;

        Integer mathScore = 87;

        Integer englishScore = 86;

   

        for (int i = 0; i < 9; i++) {

            languageScore += i;

            mathScore += i;

            englishScore += i;

            List<String> data = Arrays.asList("小明" + i, languageScore + "", mathScore + "", englishScore + "");

            map.put(i + "", data);

        }

        return map;

    }

   

   

   

   

   

   


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM