java生成excel花费的时间,Apache POI需要很长时间才能生成excel表

package efms.bizobj;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.List;

import java.util.Random;

import org.apache.poi.hssf.usermodel.HSSFCell;

import org.apache.poi.hssf.usermodel.HSSFRow;

import org.apache.poi.hssf.usermodel.HSSFSheet;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import efms.db.bean.PsatMetricsDrillDown2ActionList;

import efms.db.bean.PsatMetricsDrillDown2Bean;

import efms.log.EfmsLogger;

public class DrillDown2ExcelReport {

public void generateHeader1(HSSFWorkbook wb, HSSFSheet sheet, String name,List records) {

EfmsLogger.debug("Inside generateHeader1");

HSSFRow row = sheet.createRow(0);

HSSFCell cell;

int count = 14;

row = sheet.createRow(0);

cell = row.createCell(0);

cell.setCellValue("USO Number");

cell = row.createCell(1);

cell.setCellValue("CLCI");

cell = row.createCell(2);

cell.setCellValue("CLO");

cell = row.createCell(3);

cell.setCellValue("OCO CLLI");

cell = row.createCell(4);

cell.setCellValue("Service Type");

cell = row.createCell(5);

cell.setCellValue("Order Action");

cell = row.createCell(6);

cell.setCellValue("Order Status");

cell = row.createCell(7);

cell.setCellValue("Test Section");

cell = row.createCell(8);

cell.setCellValue("Test Status");

cell = row.createCell(9);

cell.setCellValue("Condition Code and Desc");

cell = row.createCell(10);

cell.setCellValue("Details on Failure Reason");

cell = row.createCell(11);

cell.setCellValue("CKL Number");

cell = row.createCell(12);

cell.setCellValue("Test Triggered Date/Time");

cell = row.createCell(13);

cell.setCellValue("Trigger");

cell = row.createCell(14);

cell.setCellValue("Trigger Name");

for(int i =0 ;i <= count ;i ++)

sheet.autoSizeColumn(i);

}

public void insertReportData(HSSFWorkbook wb, HSSFSheet sheet, String name,List records,String failType) {

HSSFRow row ;

HSSFCell cell;

String clci ;

String clo;

int orderType;

String usoNumber ;

int svcOrderId ;

String serviceType;

String ocoClli ;

String orderAction;

String orderStatus;

String testStatus ;

String conditionCode ;

String conditionCodeDesc ;

String cklNum ;

String triggerDateTime ;

String trigger ;

String triggerName ;

for(int i=0 ; i < records.size() ;i ++){

clci =" ";

clo =" ";

orderType = -1;

usoNumber = " ";

svcOrderId = -1;

serviceType = " ";

ocoClli = " ";

orderAction = " ";

orderStatus = " ";

testStatus = " ";

conditionCode = " ";

conditionCodeDesc = " ";

cklNum = " " ;

triggerDateTime = " ";

trigger = " ";

triggerName = " ";

if(records.get(i).getClci() != null)

clci = records.get(i).getClci();

if(records.get(i).getClo() != null)

clo = records.get(i).getClo();

if(records.get(i).getOrderType() != -1)

orderType = records.get(i).getOrderType();

if(records.get(i).getUsoNumber() != null)

usoNumber = records.get(i).getUsoNumber();

if(records.get(i).getSvcOrderId() != -1)

svcOrderId = records.get(i).getSvcOrderId();

if(records.get(i).getServiceType() != null)

serviceType = records.get(i).getServiceType();

if(records.get(i).getOcoClli() != null)

ocoClli = records.get(i).getOcoClli();

if(records.get(i).getOrderAction() != null)

orderAction = records.get(i).getOrderAction();

if(records.get(i).getOrderStatus() != null)

orderStatus = records.get(i).getOrderStatus();

if(records.get(i).getTestStatus() != null)

testStatus = records.get(i).getTestStatus();

if(records.get(i).getConditionCode() != null)

conditionCode = "("+ records.get(i).getConditionCode()+")";

if(records.get(i).getConditionCodeDescription() != null)

conditionCodeDesc = records.get(i).getConditionCodeDescription();

if(records.get(i).getCklNumber() != null)

cklNum = records.get(i).getCklNumber();

if(records.get(i).getTrigDateTime() != null)

triggerDateTime = records.get(i).getTrigDateTime();

if(records.get(i).getTrigger() != null)

trigger = records.get(i).getTrigger();

if(records.get(i).getTriggerName() != null)

triggerName = records.get(i).getTriggerName();

row = sheet.createRow(i+1);

cell = row.createCell(0);

cell.setCellValue(usoNumber);

sheet.autoSizeColumn(0);

cell = row.createCell(1);

cell.setCellValue(clci);

sheet.autoSizeColumn(1);

cell = row.createCell(2);

cell.setCellValue(clo);

sheet.autoSizeColumn(2);

cell = row.createCell(3);

cell.setCellValue(ocoClli);

sheet.autoSizeColumn(3);

cell = row.createCell(4);

cell.setCellValue(serviceType);

sheet.autoSizeColumn(4);

cell = row.createCell(5);

cell.setCellValue(orderAction);

sheet.autoSizeColumn(5);

cell = row.createCell(6);

cell.setCellValue(orderStatus);

sheet.autoSizeColumn(6);

cell = row.createCell(7);

cell.setCellValue(failType);

sheet.autoSizeColumn(7);

cell = row.createCell(8);

cell.setCellValue(testStatus);

sheet.autoSizeColumn(8);

cell = row.createCell(9);

cell.setCellValue(conditionCode + conditionCodeDesc);

sheet.autoSizeColumn(9);

if(records.get(i).getActionList()!=null){

for(PsatMetricsDrillDown2ActionList p: records.get(i).getActionList()) {

cell = row.createCell(10);

cell.setCellValue(p.getActionName() + " - " + p.getActionReason());

sheet.autoSizeColumn(10);

}

}else{

cell = row.createCell(10);

cell.setCellValue(" ");

sheet.autoSizeColumn(10);

}

cell = row.createCell(11);

cell.setCellValue(cklNum);

sheet.autoSizeColumn(11);

cell = row.createCell(12);

cell.setCellValue(triggerDateTime);

sheet.autoSizeColumn(12);

cell = row.createCell(13);

cell.setCellValue(trigger);

sheet.autoSizeColumn(13);

cell = row.createCell(14);

cell.setCellValue(triggerName);

sheet.autoSizeColumn(14);

}

}

public String generateDD2ExcelReport(List records,String failType){

HSSFWorkbook wb = new HSSFWorkbook();

HSSFSheet dd2ExcelSheet = wb.createSheet("DrillDown2 Report");

generateHeader1(wb,dd2ExcelSheet,"DrillDown2 Report",records);

insertReportData(wb,dd2ExcelSheet,"DrillDown2 Report",records,failType);

FileOutputStream fileOut;

Random rand = new Random();

String fileName = "";

String fileLocation = "" ;

boolean fileExists = true;

try {

do{

fileName = "DrillDown2" + rand.nextInt(10000000)+"Report.xls";

fileLocation = "/opt/www/data/psat_report" + File.separator + fileName;

fileExists = new File(fileLocation).exists();

}while(fileExists);

fileOut = new FileOutputStream(fileLocation);

String path = new File(fileLocation).getAbsolutePath();

wb.write(fileOut);

fileOut.close();

} catch (IOException e) {

e.printStackTrace();

}

return fileName;

}

}

此代码用于生成Excel报告 . 共有15列,如果记录数约为1000,则生成excel表需要近10-15分钟 . 上面的代码有什么问题吗?如何提高性能(excel报告生成的时间减少)?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值