java 导出excel并且针对海量数据进行处理

阅读更多
out.clearBuffer();

rs = stmt.executeQuery("select * from BATCH_POINTADJUST_DETAIL );
//获取服务器绝对路径
String path = request.getSession().getServletContext().getRealPath("")+ java.io.File.separator + "WEB-INF/download/batch_pointadjust_detail.xls";
File file = new File(path); //创建一个.xls文件
System.out.println("path is =========" + path);
File folder = new File(path.substring(0,path.lastIndexOf("/")));
//判断文件夹和文件是否存在,如果不存在就创建
if(!folder.exists()){
file.mkdirs();
}
if(file.exists()){
file.delete();
file.createNewFile();
}else{
file.createNewFile();
}

BufferedInputStream fis = null;
try{
WritableWorkbook wwb = Workbook.createWorkbook(new FileOutputStream(file));
WritableSheet ws = wwb.createSheet("积分批量调整成功记录明细", 0); //创建一个sheet
//创建表头
Label labelC = new jxl.write.Label(0, 0, "流水ID");
ws.addCell(labelC);
labelC = new jxl.write.Label(1, 0, "流水批次号");
ws.addCell(labelC);
labelC = new jxl.write.Label(2, 0, "客户号");
ws.addCell(labelC);

int i = 1;//行
boolean flg = false;
int t = 0;//工作表个数
if(rs != null){
while(rs.next()){
//excel工作表最大记录数为65536,当超出此范围时需要重新创建新的工作表(sheet)
if(i%60000 == 0){
t ++ ;
flg = true;
ws = wwb.createSheet("积分批量调整成功记录明细" + t, 0); //创建新的sheet
//创建表头
labelC = new jxl.write.Label(0, 0, "流水ID");
ws.addCell(labelC);
labelC = new jxl.write.Label(1, 0, "流水批次号");
ws.addCell(labelC);
labelC = new jxl.write.Label(2, 0, "客户号");
ws.addCell(labelC);
}
//将记录集中的数据填充至excel文件中
for(int j = 0;j < rs.getMetaData().getColumnCount();j ++ ){
if(flg){ //将excel中的行数归零
labelC = new Label(j,i + 1 - t * 60000,rs.getString(j + 1));
}else{
labelC = new Label(j,i,rs.getString(j + 1));
}
ws.addCell(labelC);
}
i ++ ;
}
}
data.close();
//写入Excel工作表
wwb.write();
//关闭Excel工作薄对象
wwb.close();

//提供下载文件
int byteRead;
fis = new BufferedInputStream(new FileInputStream(file));
response.setContentType("application/octet-stream; charset=iso-8859-1");
response.setHeader("Content-disposition","attachment; filename=\"" + file.getName() + "\"");
while ((byteRead = fis.read()) != -1) {
out.write(byteRead);
}
out.flush();


你可能感兴趣的:(Excel,Java,J#,Web,工作)