2016-03-09 70 views
0

我想用groovy做一些自動操作。我正在接收另一個請求的響應數據,並使用jxl jars將這兩個請求和兩個輸出存儲在excel中。但如果我這樣做,我的Excel文件被損壞。但是當我做了單一的請求響應,它工作正常。所以我想知道是否有任何jxl內存問題??如果是的話如何克服?使用jxl編寫大型數據時,excel文件損壞gent

這裏是我的腳本:

import jxl.* 
    import jxl.write.* 
    import groovy.sql.Sql 


    //Datasheet read define start 
    def projectLocation="E:/" 
    def dataFileLocation=projectLocation+"zip.xls" 
    def workbook = Workbook.getWorkbook(new File(dataFileLocation)) 
    def readSheet = workbook.getSheet(0) 
    def rowCount = readSheet.getRows() 
    def colCount = readSheet.getColumns() 

    def myTestCase = context.testCase 
    //db config 
    def url = 'jdbc:hsqldb:hsql://localhost/' 
    def user = 'sa' 
    def password = '' 
    def driver = 'org.hsqldb.jdbcDriver' 
    def sql = Sql.newInstance(url, user, password, driver) 

    propTestStep = myTestCase.getTestStepByName("Properties"); 
    //Datasheet read end 

    //Content Write define start 
    WorkbookSettings s = new WorkbookSettings(); 
    s.setUseTemporaryFileDuringWrite(true); 
    // s.setInitialFileSize(100000000) 

    WritableWorkbook workbook1 = Workbook.createWorkbook(new File(projectLocation+"output1.xls"),s) 
    WritableSheet writeSheet = workbook1.createSheet("new", 0) 
    // 
    //WritableWorkbook workbook2 = Workbook.createWorkbook(new File(projectLocation+"output2.xls")) 
    //WritableSheet writeSheet1 = workbook2.createSheet("newOutput", 0) 
    def project = testRunner.testCase.testSuite.project 
    def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context) 
    //Content Write define end 


    for(int i = 1;i < rowCount; i++) 
        { 

            for(int j = 0;j < colCount; j++) 
                { 

                    val= readSheet.getCell(j,i).getContents() 
                    // log.info "before storing in zip val="+ val 
                    // propTestStep.setPropertyValue("zip",val) 
                    def req=groovyUtils.getXmlHolder("GetInfoByZIP#Request") 
                    req["//web:USZip"] = val 

                    req.updateProperty() 
                    testRunner.runTestStep(project.testSuites['USZipSoap12 TestSuite'].testCases['GetInfoByZIP TestCase'].testSteps['GetInfoByZIP']) 

                    // log.info "before sending to db val="+val 
         sql.eachRow('select state,city from zip where zip=?',[val]){ row -> 
         st= row.state 
         ct= row.city 
         } 

                    //log.info st+" "+ct 
                    def res=groovyUtils.getXmlHolder("GetInfoByZIP#Response") 
         def state = res.getNodeValues("//STATE") 
         def city=res.getNodeValues("//CITY") 

         def ct1=city[0] 
         log.info ct +" " + ct1 
         if(ct1.equalsIgnoreCase(ct)){ 
            // log.info "city equals" 
         } 
         else{ 
            // log.info "not eq" 
         } 


                     def req1=groovyUtils.getXmlHolder("GetInfoByState#Request") 
                     req1["//web:USState"]=state[0] 
                     req1.updateProperty() 

                     testRunner.runTestStep(project.testSuites['USZipSoap12 TestSuite'].testCases['GetInfoByZIP TestCase'].testSteps['GetInfoByState']) 

                     def respo1= testRunner.testCase.testSteps["GetInfoByState"].testRequest.response.contentAsString 
                    Label labelReq = new Label(j,i,context.testCase.getTestStepByName("GetInfoByZIP").getProperty("request").value) 
                    Label labelReq1 = new Label(j+2,i,context.testCase.getTestStepByName("GetInfoByState").getProperty("request").value) 

                    def response = testRunner.testCase.testSteps["GetInfoByZIP"].testRequest.response.contentAsString 
                    Label labelResp = new Label(j+1,i,response); 
                                 Label labelResp1 = new Label(j+3,i,respo1); 
                    writeSheet.addCell(labelReq) 
                    writeSheet.addCell(labelResp); 
                 log.info respo1 
                     writeSheet.addCell(labelReq1) 
                    writeSheet.addCell(labelResp1); 

                } 

        } 
    workbook1.write() 
    workbook1.close() 
+0

編輯問題並顯示您擁有的腳本。 – Rao

+0

編輯請看一次 – LowCool

回答

0

我想你是超過Excel的單元格極限的響應數據。你可以通過複製粘貼檢查這個事件來檢查有多少個字符。 Excel可以在一行中包含32,767個字符。如果它不止是可能會腐敗。

+0

感謝雅與你提到的同樣的問題,我用不同的請求其工作正常.. – LowCool

0

最有可能的,你是壓倒一切的東西無意的。我不知道它是什麼。但是,一個好的起點是更簡潔的代碼。所以我建議使用Frosted Sheets(由Apache POI支持)而不是JXL。它使與工作簿許多更容易。

我寫了這個庫,因爲我厭倦了看到一些最愚蠢的代碼,而且我知道它可以做得更多...... Groovy-ly。

import com.emmanuelrosa.frostedsheets.* 
import org.apache.poi.hssf.usermodel.HSSFWorkbook 
import groovy.sql.Sql 

def projectLocation="E:/" 
def dataFileLocation="${projectLocation}zip.xls" 
def workbook = new FrostedWorkbook(new HSSFWorkbook(new FileInputStream(dataFileLocation))) 
def rowCount = readSheet.getRows() 
def colCount = readSheet.getColumns() 

def myTestCase = context.testCase 
def url = 'jdbc:hsqldb:hsql://localhost/' 
def user = 'sa' 
def password = '' 
def driver = 'org.hsqldb.jdbcDriver' 
def sql = Sql.newInstance(url, user, password, driver) 

propTestStep = myTestCase.getTestStepByName("Properties"); 

def workbook1 = FrostedWorkbook.createXLS() 
def writeSheet = workbook1['new'] 
def project = testRunner.testCase.testSuite.project 
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context) 

/* 
* cellIterator() returns a special Iterator which traverses 
* through the cells by row, then column; top-down, left-right. 
* It eliminates the need for nested loops. 
*/ 
workbook[0].cellIterator().each { cell -> 
    req["//web:USZip"] = cell.cellValue 
    def req=groovyUtils.getXmlHolder("GetInfoByZIP#Request") 

    req.updateProperty() 
    testRunner.runTestStep(project.testSuites['USZipSoap12 TestSuite'].testCases['GetInfoByZIP TestCase'].testSteps['GetInfoByZIP']) 

    sql.eachRow('select state,city from zip where zip=?',[val]) { row -> 
     st= row.state 
     ct= row.city 
    } 

    def res=groovyUtils.getXmlHolder("GetInfoByZIP#Response") 
    def state = res.getNodeValues("//STATE") 
    def city=res.getNodeValues("//CITY") 

    def ct1=city[0] 
    log.info ct +" " + ct1 

    if(ct1.equalsIgnoreCase(ct)) { 
      // log.info "city equals" 
    } 
    else{ 
      // log.info "not eq" 
    } 

    def req1=groovyUtils.getXmlHolder("GetInfoByState#Request") 
    req1["//web:USState"]=state[0] 
    req1.updateProperty() 

    testRunner.runTestStep(project.testSuites['USZipSoap12 TestSuite'].testCases['GetInfoByZIP TestCase'].testSteps['GetInfoByState']) 

    writeSheet[cell.rowIndex + 1].with { 
     def stateInfo = testRunner.testCase.testSteps["GetInfoByState"].testRequest.response.contentAsString 
     def zipValue = context.testCase.getTestStepByName("GetInfoByZIP").getProperty("request").value 
     def stateValue = context.testCase.getTestStepByName("GetInfoByState").getProperty("request").value 
     def zipInfo = testRunner.testCase.testSteps["GetInfoByZIP"].testRequest.response.contentAsString 

     // Add the columns to the row 
     append [zipValue, zipInfo, stateValue, stateInfo] 
    } 

} 

new FileOutputStream("${projectLocation}output1.xls").withStream { stream -> 
    workbook1.write(stream) 
} 
+0

它向我展示了FrostedWorkBook無法使用POI解析.m 3.14 – LowCool

+0

首先,確保已經設置了Maven repo(解析器)*和*依賴關係,如自述文件中所述。 .jar不在Maven Central中。如果它抓住POI 3.14-beta1,那麼你就設置好了。接下來,確保你拼寫正確的類名。這是'FrostedWorkbook'與lowecase * b *。 –