我是靠谱客的博主 聪明雨,最近开发中收集的这篇文章主要介绍poi导出excel合并单元格,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述


    /*导出服务*/
    @RequestMapping(value = "/exportMaintenance.html")
    public void exportMaintenance(HttpServletRequest request, HttpServletResponse response) {
        String ids = request.getParameter("ids");
        List<Map> sourceData = null;
        //判断是否导出所有
        if (StringFacs.isEmpty(ids)) {
            //ids为空则导出所有
            sourceData = greenBasiceService.exportMaintenance(request);
        } else {
            sourceData = greenBasiceService.exportMaintenance2(ids);
        }
        CellRangeAddress cellRangeAddress =null;
        //创建Excel管理对象,可读写.xls或.xlsx文件,与MS-Office版本2007或更高版本兼容
        XSSFWorkbook workbook = new XSSFWorkbook();
        //创建新的工作簿
        XSSFSheet sheet = workbook.createSheet();
        String[] baseKeyArray = {"序号", "xzqh", "yhpq", "people", "zyrc", "jxtb", "jxczrs", "yhcl", "time", "username", "number", "work", "work_num", "unit"};
        String[] titleData = null;
        titleData = new String[]{"序号", "行政区划", "养护片区", "片区负责人", "作业人次", "机械台班", "机械承载人数", "养护材料", "上报时间", "上报人员", "编号", "工作内容", "工程量", "单位"};
        CTWorksheet sheetX = sheet.getWorkbook().getSheetAt(0).getCTWorksheet();
        //设置首行样式
        XSSFFont fontRow1 = (XSSFFont) workbook.createFont();
        PoiUtils.setXSSFFont(fontRow1, (short) 11, "微软雅黑", true, false, null);
        XSSFCellStyle styleRow1 = (XSSFCellStyle) workbook.createCellStyle();
        PoiUtils.setXSSFCellStyle(styleRow1, fontRow1, true);
        //首行
        XSSFRow row0 = sheet.createRow(0);
        for (int i = 0; i < titleData.length; i++) {
            XSSFCell cell = row0.createCell(i);
            cell.setCellStyle(styleRow1);
            cell.setCellValue(titleData[i]);
        }
        //设置第正文样式
        XSSFFont fontRowMain = (XSSFFont) workbook.createFont();
        PoiUtils.setXSSFFont(fontRowMain, (short) 11, "微软雅黑", false, false, null);
        XSSFCellStyle styleRowMain = (XSSFCellStyle) workbook.createCellStyle();
        PoiUtils.setXSSFCellStyle(styleRowMain, fontRowMain, true);
        if (sourceData.size()>0) {
            for (int i = 0; i < sourceData.size(); i++) {
                Map data = sourceData.get(i);
                XSSFRow mainRow = sheet.createRow(i + 1);
                for (int j = 0; j < titleData.length; j++) {
                    XSSFCell mainCell = mainRow.createCell(j);
                    if (j == 0) {
                        mainCell.setCellValue(i + 1);
                    } else {
                        String value = data.get(baseKeyArray[j]) == null ? "" : data.get(baseKeyArray[j]).toString();
                        mainCell.setCellValue(value);
                    }
                    mainCell.setCellStyle(styleRowMain);
                }
                if (i<sheet.getLastRowNum()) {
                    String stringCellValue = sheet.getRow(i).getCell(10).getStringCellValue();//获取第i行的编号值
                    String stringCellValue2 = sheet.getRow(i+1).getCell(10).getStringCellValue();//获取第i+1行的编号值
                    if(stringCellValue.equals(stringCellValue2)){
                        //编号相同,进行合并
                        for(int l=0;l<11;l++){
                             cellRangeAddress = new CellRangeAddress(i, i+1, l, l);
                            // 重写合并的方法
                            addMergedReigon(sheetX, cellRangeAddress);
                        }

                    }
                }

            }
        }
        OutputStream ouputStream = null;
        String fileName = "日养台账管理" + DateFacs.getMsTime();
        try {
            ouputStream = response.getOutputStream();
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setHeader("Content-disposition", "attachment;filename=" + new String(fileName.getBytes(), "iso-8859-1") + ".xlsx");
            response.setHeader("Set-Cookie", "fileDownload=true;path=/");
            workbook.write(ouputStream);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                ouputStream.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                ouputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    /*
     * 重写合并单元格方法*/
    private static void addMergedReigon(CTWorksheet sheetX, CellRangeAddress cellRangeAddress) {
        CTMergeCells ctMergeCells;
        if (sheetX.isSetMergeCells()) {
            ctMergeCells = sheetX.getMergeCells();
        } else {
            ctMergeCells = sheetX.addNewMergeCells();
        }

        CTMergeCell ctMergeCell = ctMergeCells.addNewMergeCell();
        ctMergeCell.setRef(cellRangeAddress.formatAsString());
    }

最后

以上就是聪明雨为你收集整理的poi导出excel合并单元格的全部内容,希望文章能够帮你解决poi导出excel合并单元格所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(41)

评论列表共有 0 条评论

立即
投稿
返回
顶部