Java 从大型数据集中创建多个excel文件并压缩excel文件
我有一个庞大的数据集。Im使用ApachePOI从数据集创建excel文件。问题是excel中的数据集大小超过了4000万行。我想分割这个数据集,创建多个excel文件,然后压缩它,并为用户提供一个可下载的压缩文件。顺便说一下,我正在使用Restful服务 检索数据类:Java 从大型数据集中创建多个excel文件并压缩excel文件,java,rest,zip,apache-poi,Java,Rest,Zip,Apache Poi,我有一个庞大的数据集。Im使用ApachePOI从数据集创建excel文件。问题是excel中的数据集大小超过了4000万行。我想分割这个数据集,创建多个excel文件,然后压缩它,并为用户提供一个可下载的压缩文件。顺便说一下,我正在使用Restful服务 检索数据类: public Map<Integer, Object[]> exportXLSXData(String dim, String client, String type) { int count = 1;
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;
}
公共地图导出XLSXDATA(字符串dim、字符串客户端、字符串类型){
整数计数=1;
映射数据=新树映射();
StringBuilder queryBuilder=新StringBuilder();
try(Connection con=datasource.getConnection()){
字符串dimName=getName(dim,客户端);
数据.put(计数,新对象[{dimName,“Job”,“A”,“B”,“C”,“D”,“E”,“F”});
语句stmt=con.createStatement();
if(类型:等信号情况(“u”)){
System.out.println(“导出未映射!!”;
append(“从表中选择*,其中client=“+client+”和“+dimName+”为空”);
stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(queryBuilder.toString());
while(rs.next()){
计数+=1;
data.put(count,新对象[]{“”,rs.getInt(“作业”),rs.getInt(“a”),
rs.getInt(“b”)、rs.getInt(“c”)、rs.getInt(“d”)、rs.getInt(“e”)、rs.getString(“f”)};
}
}else if(类型.equalsIgnoreCase(“m”)){
System.out.println(“导出映射!!”;
append(“从表中选择*,其中client=“+client+”和“+dimName+”不为空”);
stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(queryBuilder.toString());
while(rs.next()){
计数+=1;
data.put(count,新对象[]{rs.getString(dimName),rs.getInt(“作业”),rs.getInt(“a”),
rs.getInt(“b”)、rs.getInt(“c”)、rs.getInt(“d”)、rs.getInt(“e”)、rs.getString(“f”)};
}
}
}捕获(SQLE异常){
System.err.println(e.getErrorCode()+e.getMessage());
}
返回数据;
}
在我的REST资源类中,我需要使用这个数据集来分割数据,创建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();
}
@GET
@路径(“/export”)
@生成(“应用程序/zip”)
公共响应exportXLSX(@QueryParam(“dim”)最终字符串dim,
@QueryParam(“客户端”)最终字符串客户端,
@QueryParam(“类型”)最终字符串类型,
@上下文信息(ui){
XSSFWorkbook工作簿=新XSSFWorkbook();
//创建空白工作表
XSSFSheet=null;
映射数据=新树映射();
数据=engineService.exportXLSXData(dim、客户端、类型);
字符串filetype=null;
if(类型:等信号情况(“u”)){
filetype=“未映射”;
sheet=workbook.createSheet(“未映射”);
}else if(类型.equalsIgnoreCase(“m”)){
filetype=“Mapped”;
工作表=工作簿。创建工作表(“映射”);
}
Set keyset=data.keyset();
int rownum=0;
for(整数键:键集)
{
Row-Row=sheet.createRow(rownum++);
Object[]objArr=data.get(key);
int-cellnum=0;
用于(对象对象对象:对象对象对象)
{
Cell Cell=row.createCell(cellnum++);
if(字符串的obj实例)
cell.setCellValue((字符串)obj);
else if(obj instanceof Integer)
cell.setCellValue((整数)obj);
}
}
ResponseBuilder response=response.ok((对象)工作簿);
header(“内容处置”,
“附件;文件名=\”“+filetype+”-“+new Date().toString()+”.zip\”;
返回response.build();
}
这里我创建一个excel文件。但是我想根据阈值分割文件,创建多个文件并将其压缩到一起。你有什么问题?@Gagravarr我想知道如何分割此数据集,创建多个excel文件并将其压缩。在一定数量的行之后,将工作簿写入压缩包,然后打开一个新工作簿继续写入?