poi导出全部数据与指定数据,个人源代码可直接使用

Controller

@ApiOperation("导出全部数据与指定导出")
@PostMapping("/excel")
public void export(long[] ids, HttpServletResponse response, ArcDocuments arcDocuments) throws IOException {
    if (ids == null) {
        arcExcelService.export(response, arcDocuments);
    } else {
        arcExcelService.exportEx(response, ids);
    }
}

Service 

/**
 * 导出全部
 * @param response
 * @param arcDocuments
 * @throws IOException
 */
void export(HttpServletResponse response, ArcDocuments arcDocuments) throws IOException;
/**
 * 指定
 */
void exportEx(HttpServletResponse response, long[] ids) throws IOException;
 

 Impl

  //全部
@Override
    public void export(HttpServletResponse response, ArcDocuments pojo) throws IOException {
        List<ArcDocuments> arcDocuments = arcExcelMapper.ExcelTemplate();
        excelHead(arcDocuments,pojo,response);
    }

//指定
@Override
    public void exportEx(HttpServletResponse response, long[] ids) throws IOException {
        List<ArcDocuments> arcDocuments = arcExcelMapper.ExcelTemplate();
        idsExcel(arcDocuments,response,ids);
    }

  /**
     * 导出全部
     *
     * @param arcDocuments
     * @param pojo
     * @param response
     * @throws IOException
     */
    private void excelHead(List<ArcDocuments> arcDocuments, ArcDocuments pojo, HttpServletResponse response) throws IOException {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("档案信息");
        CellStyle style = workbook.createCellStyle();
        Font font = workbook.createFont();
        //设置上边框线条类型
        style.setBorderTop(THIN);
        //设置右边框线条类型
        style.setBorderRight(THIN);
        //设置下边框线条类型
        style.setBorderBottom(THIN);
        //设置左边框线条类型
        style.setBorderLeft(THIN);
        //设置上边框线条颜色
        style.setTopBorderColor(IndexedColors.WHITE.getIndex());
        //设置右边框线条颜色
        style.setRightBorderColor(IndexedColors.WHITE.getIndex());
        //设置下边框线条颜色
        style.setBottomBorderColor(IndexedColors.WHITE.getIndex());
        //设置左边框线条颜色
        style.setLeftBorderColor(IndexedColors.WHITE.getIndex());
        style.setAlignment(HorizontalAlignment.CENTER);

        style.setFillForegroundColor(IndexedColors.GREY_40_PERCENT.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        font.setColor(IndexedColors.WHITE.getIndex());
        style.setFont(font);
//        sheet.getPrintSetup().setPaperSize(PrintSetup.A4_PAPERSIZE);
        Row row1 = sheet.createRow(0);

        int i = 0;
            Cell cell1 = row1.createCell(i);
            cell1.setCellValue("序号");
            cell1.setCellStyle(style);
            i++;
        if(arcDocuments.get(45).getStart() == '1'){
            Cell cell = row1.createCell(i);
            cell.setCellValue("哈哈1");
            cell.setCellStyle(style);
            i++;
        }
        if(arcDocuments.get(0).getStart() == '1'){
            Cell cell = row1.createCell(i);
            cell.setCellValue("哈哈2");
            cell.setCellStyle(style);
            i++;
        }
        if(arcDocuments.get(1).getStart() == '1'){
            Cell cell = row1.createCell(i);
            cell.setCellValue("哈哈3");
            cell.setCellStyle(style);
            i++;
 }

        ArcDocuments arcDocuments1 = new ArcDocuments();
        List<ArcDocuments> list = iCfDocumentsService.findAll(arcDocuments1);
        int x = 1;
        for (ArcDocuments arcex: list){
            int j = 0;
            Row row = sheet.createRow(x);
            Cell cell2 = row.createCell(0);
            cell2.setCellValue(x);
            j++;
          
            if(arcDocuments.get(4).getStart() == '1'){
                Cell cell = row.createCell(j);
                if(arcex.getDeputyTitle()!=null){
                    cell.setCellValue(arcex.getDeputyTitle());
                }
                j++;
            }
            if(arcDocuments.get(5).getStart() == '1'){
                Cell cell = row.createCell(j);
                if(arcex.getParallelTitle()!=null){
                    cell.setCellValue(arcex.getParallelTitle());
                }
                j++;
            }
            if(arcDocuments.get(6).getStart() == '1'){
                Cell cell = row.createCell(j);
                if(arcex.getExplainTheTitlText()!=null){
                    cell.setCellValue(arcex.getExplainTheTitlText());
                }
                j++;
            }
          
            x++;
        }

        // 4.建立输出流,输出浏览器文件
        OutputStream os = null;
        // 4.1 设置一下excel名字,输出类型编码
        response.setContentType("application/octet-stream;chartset=utf8");
        response.setHeader("Content-Disposition","attachment;filename="+ new String("信息".getBytes(),"iso-8859-1")+".xlsx");
        // 4.2 输出文件
        os = response.getOutputStream();
        workbook.write(os);
        os.flush();
        // 5. 关闭输出流
        os.close();

    }
  /**
     * 导出选中
     */
    private void idsExcel(List<ArcDocuments> arcDocuments, HttpServletResponse response, long[] ids)throws IOException {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("信息");
        Row row1 = sheet.createRow(0);
        int i = 0;

        char start = arcDocuments.get(0).getStart();
        Cell cell1 = row1.createCell(i);
        cell1.setCellValue("序号");
        i++;


        if(arcDocuments.get(0).getStart() == '1'){
            Cell cell = row1.createCell(i);
            cell.setCellValue("文件编号");
            i++;
        }
        if(arcDocuments.get(1).getStart() == '1'){
            Cell cell = row1.createCell(i);
            cell.setCellValue("全宗号");
            i++;
        }
    
        ArrayList<ArcDocuments> list = new ArrayList<>();
        for (long id : ids) {
            list.add(arcExcelMapper.queryFind(id));
        }
        int x = 1;
        for (ArcDocuments arcex:list){
            int j = 0;
            Row row = sheet.createRow(x);
            Cell cell2 = row.createCell(j);
            cell2.setCellValue(x);
            j++;

            if(arcDocuments.get(1).getStart() == '1'){
                Cell cell = row.createCell(j);
                if(arcex.getFileNumber()!=null){
                    cell.setCellValue(arcex.getFileNumber());
                }
                j++;
            }
            if(arcDocuments.get(1).getStart() == '1'){
                Cell cell = row.createCell(j);
                if(arcex.getAllCasesOfNo()!=null){
                    cell.setCellValue(arcex.getAllCasesOfNo());
                }
                j++;
            }
            if(arcDocuments.get(1).getStart() == '1'){
                Cell cell = row.createCell(j);
                if(arcex.getAllCasesOfName()!=null){
                    cell.setCellValue(arcex.getAllCasesOfName());
                }
                j++;
            }

            x++;
        }
        // 4.建立输出流,输出浏览器文件
        OutputStream os = null;
        // 4.1 设置一下excel名字,输出类型编码
        response.setContentType("application/octet-stream;chartset=utf8");
        response.setHeader("Content-Disposition","attachment;filename="+ new String("信息".getBytes(),"iso-8859-1")+".xlsx");
        // 4.2 输出文件
        os = response.getOutputStream();
        workbook.write(os);
        os.flush();
        // 5. 关闭输出流
        os.close();

    }

两者差距不大,主要还是传输了ids,ids为空导出全部再者相反。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值