Java代码需要很长时间才能生成Excel文件

时间:2018-12-03 11:50:43

标签: java apache-poi

我正在尝试生成具有20万条记录的excel图书。生成记录需要1-2小时。

下面是我的代码。

Workbook workbook = workbook = new HSSFWorkbook();

Sheet sheet = workbook.createSheet(WorkbookUtil.createSafeSheetName(studAppForm.get(0).getScheme_Id()+"_"+studAppForm.get(0).getEFP_Scholarship_Name(),'_'));

createXLSHeaders(sheet,schemeQuestionData.get(currSheetSchemeId));

writeBook(studAppForm.get(j), row);

      File file=new File(path+csvFileName);
                        file.getParentFile().mkdirs();
                        file.createNewFile();
                        writer = new FileWriter(file);

                       try (FileOutputStream outputStream = new FileOutputStream(path+csvFileName)) {
                               workbook.write(outputStream);
                        }             

                        writer.flush();
                        writer.close();

createXLSHeaders函数

void createXLSHeaders( Sheet sheet, List<SchemeMasterBean> schemeMasterBeanList){

       LOGGER.info("Creating XLS SheetHeaders for sheet "+sheet.getSheetName());

     //   Sheet sheet = workbook.createSheet();
        Row header = sheet.createRow(0);
        header.createCell(0).setCellValue("APPLICATION ID");
        header.createCell(1).setCellValue("APPLICATION STATUS");
        .
        .
        .
        72 field
        .
        .

    }

这是writeBook函数

private void writeBook(StudentAppFormVsp saf, Row row) throws JSONException {

            //LOGGER.info("Start : Write book for appId"+saf.getApp_Id()); 
            Cell cell = row.createCell(0);
            cell.setCellValue(saf.getApp_Id()!=null?saf.getApp_Id().toString():"");
            cell = row.createCell(1);
            cell.setCellValue(saf.getApp_Status()!=null?getApplicationStatusMap().get(saf.getApp_Status()):"");
            cell = row.createCell(2);
            cell.setCellValue(saf.getCrtn_time()!=null?saf.getCrtn_time().toString():"");
            .
            .
            72 field
            .
            .


            JSONObject obj = new JSONObject(saf.getScheme_Que_Dtls().getValFields());
            TreeMap<Integer,String> treeMapJsonObj=new TreeMap<>();
            Integer id=null;

            for(int k = 0 ;k< obj.names().length();k++){
                id=Integer.parseInt(obj.names().get(k).toString().substring(4, obj.names().get(k).toString().length()));
                treeMapJsonObj.put(id, (String)obj.get(obj.names().get(k).toString()));
            }

                        for(Map.Entry<Integer, String> mp:treeMapJsonObj.entrySet()){
                cell = row.createCell(i);
                String extension = "";
                if(mp.getKey()!=null){
                    String value = mp.getValue();
                    if(value.lastIndexOf(".")!=-1){
                 extension=value.substring(value.lastIndexOf("."),value.length());
                 if (extension.toUpperCase().equalsIgnoreCase(".JPEG")||extension.equalsIgnoreCase(".PNG")||extension.equalsIgnoreCase(".PDF")||extension.equalsIgnoreCase(".JPG")) {
                     cell.setCellValue("Y");
                    }
                 else{
                     cell.setCellValue(((value!=null) && !(value.equals("Not Null")) && !(value.equals("undefined")) && !(value.equals("null")))?value:"");
                 }
                 }
                 else{
                     cell.setCellValue(((value!=null) && !(value.equals("Not Null")) && !(value.equals("undefined")) && !(value.equals("null")))?value:"");
                 }
                }
                else{
                     cell.setCellValue("");
                }
                i++;
            }

    }

我尝试按照某些stackoverflow链接的建议,使用java BufferedInputStream类减少excel文件的生成。但这没用。

请建议我所有可能的方法以减少文件生成时间。预先谢谢你。

0 个答案:

没有答案