Groovy - How to periodically flush data into excel files

浪子不回头ぞ 提交于 2019-12-25 02:59:41

问题


I am writing a groovy code (inside SoapUI tool) to create excel file and periodically flush data into it. The idea is if due to some exception/interruption the code did not complete, i should get the data in excel till the time error/issue occurred.

I am using outputStream.flush() as shown below. Below is my sample code.

import jxl.*
import jxl.write.*
import java.io.IOException;
import java.io.OutputStream;

    WritableWorkbook workbook;
    WritableSheet sheet;
    try{
       log.info ("    >>    Start");
       def times;
       def currentTime = times = new Date().format("dd-MM-yyyy HH-mm-ss");  

       OutputStream outputStream = new FileOutputStream("C:/Users/user1/Result-"+currentTime+".xls");

       WorkbookSettings workbookSettings = new WorkbookSettings();
       workbook = Workbook.createWorkbook(outputStream);

       sheet = workbook.createSheet("Sample 1",0);

       for(int i=0;i<10;i++){

         Label noScenarioSetLabel1 = new Label(0, i, "NO SCENARIO SET FOR EXECUTION"); 
         sheet.addCell(noScenarioSetLabel1);

         outputStream.flush();

         Thread.sleep(1000);

        }
    workbook.write();
    workbook.close();
    log.info ("    >>    End");

}catch(Exception exp){
    log.error ("Some Exp occurred : "+exp.toString());
    workbook.write();
    workbook.close();
}

This is not writing data on every flush. Am i missing anything here?


回答1:


Yes. You're not writing any data for the stream to flush.

The Excel data is written to the output stream by the workbook.write() method. You're not calling that method within the for loop. So you can simply call workbook.write() before flushing.

A Groovier solution

Here's a Groovier way to write your program:

import jxl.*
import jxl.write.*

log.info ("    >>    Start")
def times;
def currentTime = times = new Date().format("dd-MM-yyyy HH-mm-ss") 

new FileOutputStream("C:/Users/user1/Result-${currentTime}.xls").withStream {outputStream ->
   def workbook = Workbook.createWorkbook(outputStream)
   def sheet = workbook.createSheet('Sample 1',0)

    0.upto(9) { 
         sheet.addCell new Label(0, it, 'NO SCENARIO SET FOR EXECUTION')  
         workbook.write()    
         outputStream.flush()        
         Thread.sleep(1000)
    }      

    workbook.write() 
    workbook.close()
    log.info ("    >>    End")
}



回答2:


Libraries like such (say "document builders") has in general problem with "flushing before finish". They build multilevel structures etc, I guess, i'm almost sure, big part of building is done at closing of levels cell, row, page, sheet ...

Look at 4 level of constructor calls (opening) , theoretically flush should propagate the same level (in same or reverse order)

Intelectual exercise: How to flush half of row, which is formally illegal in output document, has no End Of Row? Or: how to flush row with bad cell count

DISCLAIMER: I don't use jxl, but few other document interfaces like. During very deep structural doc in iText is almost impossible to successfully flush on exception, is has no sense.

If You have risk of exception maybe get data to simpler structure in 1st stage (classic: Map, List etc), then 2nd pass (safe) with document building?



来源:https://stackoverflow.com/questions/32610570/groovy-how-to-periodically-flush-data-into-excel-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!