关于jxls/poi导出excel数据源为横向的问题

既然被归类到技术贴,我尽量控制少说废话,直接上问题:
数据源如下图:

关于jxls/poi导出excel数据源为横向的问题_第1张图片
webwxgetmsgimg (1).jpg

客户要求效果图:

webwxgetmsgimg.jpg

由以上两图可以看出,以我对jxls的简单认知是基本没可能做出来的,于是乎,我采取了最笨的方法,使用poi将数据生生写入excel,哈哈哈哈哈哈,祭出主要代码:

Here is the code:


private int exportAccountBankTotalExcel(List listAccountBankTotal, Integer year, Integer month,
            OutputStream outputStream, String templateFilePath) {
        InputStream templateXls = null;
        try {
                          //读取excel模板,模板中只有一行标题,templateFilePath是传过来的路径
            templateXls = new BufferedInputStream(ExportMoneyExcelImpl.class.getResourceAsStream(templateFilePath));
            XLSTransformer transformer = new XLSTransformer();
            Map beans = new HashMap();
            //创建Workbook对象
            Workbook workBook = transformer.transformXLS(templateXls, beans);
            // sheet对应一个工作页
            Sheet sheet = workBook.getSheetAt(0);

            // 设置样式1
            CellStyle cellStyle = workBook.createCellStyle();
            cellStyle.setBorderBottom(CellStyle.BORDER_THIN);
            cellStyle.setBorderLeft(CellStyle.BORDER_THIN);
            cellStyle.setBorderRight(CellStyle.BORDER_THIN);
            cellStyle.setBorderTop(CellStyle.BORDER_THIN);
            cellStyle.setAlignment((HSSFCellStyle.ALIGN_CENTER));
            // 设置样式2
            CellStyle cellStyleRight = workBook.createCellStyle();
            cellStyleRight.cloneStyleFrom(cellStyle);
            cellStyleRight.setAlignment((HSSFCellStyle.ALIGN_RIGHT));
            // 往excel中写入新数据
            Row firstRow = sheet.createRow(1); // 第一行让给表头了,这个假装是第一行
            Cell firstCell = firstRow.createCell(0); // 第一列
            firstCell.setCellValue("日期");
            firstCell.setCellStyle(cellStyle);

            // 以acountDate为key
            Map> map = new LinkedHashMap>();
            for (AccountBankTotal accountBankTotal : listAccountBankTotal) {
                if (map.containsKey(accountBankTotal.getAccountDate())) {
                    map.get(accountBankTotal.getAccountDate()).add(accountBankTotal);
                } else {
                    List newList = new ArrayList();
                    newList.add(accountBankTotal);
                    map.put(accountBankTotal.getAccountDate(), newList);
                }
            }
            // 声明Set集合
            Set bankSet = new LinkedHashSet();
            // 遍历Map将银行名存到Set集合中
            for (String key : map.keySet()) {
                for (AccountBankTotal abt : map.get(key)) {
                    bankSet.add(abt.getBankName());
                }
            }
            // 输出银行名到Excel
            int i = 1;
            for (Iterator it = bankSet.iterator(); it.hasNext();) {
                firstRow.createCell(i).setCellValue(it.next().toString());
                // logger.info("bankname:" + it.next().toString());
                firstRow.getCell(i).setCellStyle(cellStyle);
                i++;
            }

            // 遍历Map,将Map中的键值写入Excel
            int count = 2;
            for (String key : map.keySet()) {
                sheet.createRow(count); // 从第二行开始创建行
                int index = 0;
                int countRow = count;
                // 写入日期&日期格式
                sheet.getRow(count).createCell(index).setCellValue(key);
                sheet.getRow(count).getCell(index).setCellStyle(cellStyle);
                // 遍历Set,通过bankName&date确定对应的money
                for (String bankName : bankSet) {
                    if (map.get(key).get(index).getBankName().equals(bankName)) {
                        sheet.getRow(countRow).createCell(index + 1)
                                .setCellValue(String.format("%.2f", map.get(key).get(index).getPayMoney()));
                        logger.info("map.Money: " + map.get(key).get(index).getPayMoney());
                        sheet.getRow(countRow).getCell(index + 1).setCellStyle(cellStyleRight);
                    }
                    index++;
                }
                count++;
            }
            // 将“合计”写入Excel最后一行第一列
            Row rowLast = sheet.createRow(sheet.getLastRowNum() + 1);
            rowLast.createCell(0).setCellValue("合计");
            rowLast.getCell(0).setCellStyle(cellStyle);
            int countSet = 1;
            // 以银行名为key存储数据
            Map> tempMap = new LinkedHashMap>(); // 以银行名为键
            for (AccountBankTotal accountBankTotal : listAccountBankTotal) {
                if (tempMap.containsKey(accountBankTotal.getBankName())) {
                    tempMap.get(accountBankTotal.getBankName()).add(accountBankTotal);
                } else {
                    List newList = new ArrayList();
                    newList.add(accountBankTotal);
                    tempMap.put(accountBankTotal.getBankName(), newList);
                }
            }
            List sumList = new LinkedList();

            for (String key : tempMap.keySet()) {
                BigDecimal sum = new BigDecimal("0.00");
                for (int m = 0; m < tempMap.get(key).size(); m++) {
                    logger.info("Money: " + tempMap.get(key).get(m).getPayMoney());
                    sum = sum.add(tempMap.get(key).get(m).getPayMoney());
                }
                sumList.add(String.format("%.2f", sum));
            }
            // 遍历 合计 值&将其写入表格
            for (String sum : sumList) {
                rowLast.createCell(countSet);
                rowLast.getCell(countSet).setCellValue(sum + "");
                rowLast.getCell(countSet).setCellStyle(cellStyleRight);
                countSet++;
            }

            // 合并单元格
            sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, bankSet.size()));
            // 设置行高、列宽
            for (int colNum = 0; colNum < 4; colNum++) {
                sheet.setColumnWidth(colNum, 256 * 21);
                for (int rowNum = 1; rowNum < sheet.getLastRowNum(); rowNum++) {
                    sheet.getRow(rowNum).setHeightInPoints(13.5f);
                }
            }
            //写入excel模板
            workBook.write(outputStream);
            outputStream.flush();
            return 0;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            logger.error("", e);
            return -2;
        } finally {
            if (templateXls != null) {
                try {
                    templateXls.close();
                } catch (IOException e) {
                }
            }
        }
    }

以上代码,注释比较详尽,如有类似问题,可在参考该代码后自行动脑解决,写完了,不说了,憋不住了,我要出宫去了。。。。

你可能感兴趣的:(关于jxls/poi导出excel数据源为横向的问题)