POI 导出例子

    @Override
    public InputStream exportTrialAccount(Map<String, Object> params) {
        List<Object> list = this.searchTrialAccountList(params);
        try {

            if (list != null && list.size() > 0) {
                XSSFWorkbook xssf = new XSSFWorkbook();
                Workbook wb = new SXSSFWorkbook(xssf);
                Sheet sheet = wb.createSheet("导出");
                sheet.setAutoFilter(CellRangeAddress.valueOf("B1:C1"));

                sheet.setColumnWidth(1,256*8*2);
                sheet.setColumnWidth(2,256*8*2);
                sheet.setColumnWidth(3,256*8*2);
                sheet.setColumnWidth(4,256*8*2);
                sheet.setColumnWidth(5,256*8*1);
                sheet.setColumnWidth(6,256*6*2);
                sheet.setColumnWidth(7,256*6*2);
                sheet.setColumnWidth(8,256*8*1);
                Row row1 = sheet.createRow(0);

                // excel 第一行为列名
                String[] cols = new String[]{"序号","开号人员", "分配员工", "产品名称", "软件帐号", "软件密码", "开始时间", "结束时间", "帐号状态", "分配状态"};
                String[] columnNames = new String[]{"排名","staffName", "assignName", "name", "account", "password", "start_time", "end_time", "status", "empAssignStatus"};

                CellStyle style = setRowStyle(wb);

                for (int i = 0; i < cols.length; i++) {
                    Cell colCell = row1.createCell(i);
                    colCell.setCellValue(cols[i]);
                    colCell.setCellStyle(style);
                }
                int rowNum = 1;// 数据从第二行开始显示
                int colNum = 0;// 每一行的cell数

                for (int k = 0; k < list.size(); k++) {
                    Row dataRow = sheet.createRow(rowNum);
                    Map entity = (Map) list.get(k);

                    for (colNum = 0; colNum < columnNames.length; colNum++){
                        String key = columnNames[colNum];
                        Cell cell = null;
                        if (colNum == 0) {
                            cell = dataRow.createCell(colNum);
                            cell.setCellValue(k+1);
                        } else {
                            cell = dataRow.createCell(colNum);
                            cell.setCellValue(entity.get(key) != null ? entity.get(key).toString() : "");
                        }
                    }
                    rowNum++;
                    colNum = 0;
                }

                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                wb.write(baos);
                ByteArrayInputStream swapStream = new ByteArrayInputStream(baos.toByteArray());
                baos.flush();
                baos.close();
                return swapStream;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }



    private CellStyle setRowStyle(Workbook wb) {
        CellStyle style = wb.createCellStyle();
        //边框填充
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 居中对齐
        style.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
        style.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
        style.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
        style.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框

        Font font = wb.createFont();
        font.setFontName("黑体");
        style.setFont(font);

//        style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
//        style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);

        style.setWrapText(true);
        return  style;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值