Groovy - 如何定期将数据刷新到Excel文件中 [英] Groovy - How to periodically flush data into excel files

查看:956
本文介绍了Groovy - 如何定期将数据刷新到Excel文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个groovy代码(SoapUI内部的工具)来创建excel文件并定期将数据刷新到其中。
想法是如果由于某些异常/中断代码没有完成,我应该获取excel中的数据,直到时间错误/问题发生。



我正在使用outputStream.flush(),如下所示。
以下是我的示例代码。

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

WritableWorkbook工作簿;
可写表单
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); (int i = 0; i< 10; i ++){

标签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();
}

这不是在每次刷新时写入数据。我在这里缺少什么?

解决方案

像这样的图书馆(比如说文件制作者)在完成前冲洗一般有问题。
他们建立多层次的结构等等,我猜,我几乎可以肯定,大部分建筑是在关闭层级单元格,行,页面,工作表...完成的。



查看4级的构造函数调用(打开),理论上flush应该传播相同的级别(以相同或相反的顺序)



非智力练习:
如何刷新输出文档中正式非法的行的一半,没有结尾?或者:如何清除行的细胞计数不正确



免责声明:我不使用jxl,而是其他几个文件界面。在iText中非常深刻的结构化文档几乎不可能成功地刷新异常,是没有意义的。



如果您有异常风险,可能会在第一阶段(经典:地图,列表等)中获取数据更简单的结构,然后使用文档构建的第二通(安全)? / p>

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?

解决方案

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?

这篇关于Groovy - 如何定期将数据刷新到Excel文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆