从大型数据集创建多个Excel文件并压缩excel文件

时间:2014-04-14 14:36:48

标签: java rest zip apache-poi

我有一个庞大的数据集。我使用Apache POI从数据集创建一个excel文件。问题是excel中的数据集大小超过40万行。我想分割这个数据集,制作多个excel文件然后压缩它并为用户提供可下载的zip文件。我顺便使用Restful服务。

RetrieveData类:

public Map<Integer, Object[]> exportXLSXData(String dim, String client, String type) {

      int count = 1;

      Map<Integer, Object[]> data = new TreeMap<Integer, Object[]>();


    StringBuilder queryBuilder = new StringBuilder();

    try (Connection con = datasource.getConnection()) {

        String dimName = getName(dim, client);

        data.put(count, new Object[] {dimName, "Job", "A", "B", "C", "D", "E", "F"});

        Statement stmt = con.createStatement();

        if(type.equalsIgnoreCase("u")){
            System.out.println("Exporting UnMapped!!");

            queryBuilder.append("select * from Table where client = "+client+" and "+dimName+" is null");

            stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery(queryBuilder.toString());

            while(rs.next()){
                count += 1;
                data.put(count, new Object[] {"", rs.getInt("job"), rs.getInt("a"), 
                        rs.getInt("b"), rs.getInt("c"), rs.getInt("d"), rs.getInt("e"), rs.getString("f")});


            }

        }else if(type.equalsIgnoreCase("m")){
            System.out.println("Exporting Mapped !!");

            queryBuilder.append("select * from Table where client = "+client+" and "+dimName+" is not null");

            stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery(queryBuilder.toString());

            while(rs.next()){
                count += 1;
                data.put(count, new Object[] {rs.getString(dimName), rs.getInt("job"), rs.getInt("a"), 
                        rs.getInt("b"), rs.getInt("c"), rs.getInt("d"), rs.getInt("e"), rs.getString("f")});

            }
        }



    }catch (SQLException e) {

        System.err.println(e.getErrorCode() + e.getMessage());
    } 

    return data;

}

在我的REST Resource类中,我需要使用这个数据集来分割数据并创建excel文件并压缩它们。

休息资源:

@GET
@Path("/export")
@Produces("application/zip")
public Response exportXLSX(@QueryParam("dim") final String dim,
        @QueryParam("client") final String client,
        @QueryParam("type") final String type,
        @Context UriInfo ui) {

     XSSFWorkbook workbook = new XSSFWorkbook(); 

      //Create a blank sheet
      XSSFSheet sheet = null;

    Map<Integer, Object[]> data = new TreeMap<Integer, Object[]>();
    data = engineService.exportXLSXData(dim, client, type);

    String filetype = null;
    if(type.equalsIgnoreCase("u")){
        filetype = "UnMapped ";
        sheet = workbook.createSheet("UnMapped");
    }else if(type.equalsIgnoreCase("m")){
        filetype = "Mapped ";
        sheet = workbook.createSheet("Mapped");
    }

    Set<Integer> keyset = data.keySet();
      int rownum = 0;
      for (Integer key : keyset)
      {
          Row row = sheet.createRow(rownum++);
          Object [] objArr = data.get(key);
          int cellnum = 0;
          for (Object obj : objArr)
          {
             Cell cell = row.createCell(cellnum++);
             if(obj instanceof String)
                  cell.setCellValue((String)obj);
              else if(obj instanceof Integer)
                  cell.setCellValue((Integer)obj);
          }
      }

    ResponseBuilder response = Response.ok((Object) workbook);
    response.header("Content-Disposition",
            "attachment; filename=\"" + filetype + " - " + new Date().toString() + ".zip\"");
    return response.build();

}

这里我创建了一个excel文件。但我想根据阈值拆分文件,创建多个文件并将它们拼接在一起。

0 个答案:

没有答案