我正在从soapUI中读取属性及其值,并将它们写入excel。
我能够将唯一的属性名称写入excel中。
def oExcel = new ActiveXObject('Excel.Application')
Thread.sleep(1000)
assert oExcel != null, "Excel object not initalized"
def openWb = oExcel.Workbooks.Open(excelPath) //excelPath complete path to the excel
def dtUsedRange = openWb.Sheets(dataSheetName).UsedRange //dataSheetName is the name of teh sheet which will ultimately hold the data
//add property names to xlMapSheet under col d or col# 4
for(int r = 1;r<=uniqPropName.size().toInteger();r++){ //uniqPropName is a list that holds all the unique property names in a test suite
openWb.Sheets(xlMapSheet).Cells(r,4).Value = uniqPropName[r-1]
}
oExcel.DisplayAlerts = false
openWb.Save
oExcel.DisplayAlerts = true
openWb.Close(false,null,false)
oExcel.Quit()
Scriptom.releaseApartment()然而,现在我必须把所有的属性都写到同一个excel中。我已经创建了一个excel列名和soapUI属性的映射,所以现在我只需要从映射中找到匹配的excel名称,并将属性值写入该excel下。
我用一个函数来做这件事。此函数从for循环中调用,该循环循环遍历测试用例中的所有属性。对于这个函数,我传递
sheetName //sheet where data has to be written
sheet //path of the excel file
pName //property name
pValue //property value
xMap //excel col name/heading map
tName //test case name
tsNum //step number此函数的相关代码如下。
def write2Excel(sheetName,sheet,pName,pValue,xMap,tName,tsNum){
//find the xl Col Name from the map
def xl = new ActiveXObject('Excel.Application')
assert xl != null, "Excel object not initalized"
//open excel
def wb = xl.Workbooks.Open(sheet)
def rng = wb.Sheets(sheetName).UsedRange
//get row count
int iColumn = rng.Columns.Count.toInteger()
int iRow = rng.Rows.Count.toInteger()
//find column number using the col name
//find the row with matching testcase name and step#
//write data to excel
if(rFound){ //if a row matching test case name and step number is found
rng.Cells(r,colId).Value = pValue
}else{
rng = rng.Resize(r+1,iColumn) //if the testcase and step# row doesn't exist then the current range has to be extended to add one more row of data.
rng.Cells(r+1,colId).Value = pValue
}
//save and close
xl.DisplayAlerts = false
wb.Save
xl.DisplayAlerts = true
wb.Close(false,null,false)
xl.Quit()
Scriptom.releaseApartment()
}该代码目前正在运行。它从昨天晚上(东部时间下午2点)开始运行,所以即使代码工作,它也不是最优的。我等不及要写数据了。
奇怪的是,excel的大小不断增加,这意味着数据正在写入excel,但我检查了excel,它没有新的data..nothing..zilch!
增加文件大小的证据.
20/02/2014 04:23 PM 466,432 my_excel_file.xls
20/02/2014 04:23 PM 466,944 my_excel_file.xls
20/02/2014 04:38 PM 470,016 my_excel_file.xls
20/02/2014 04:45 PM 471,552 my_excel_file.xls
20/02/2014 04:47 PM 472,064 my_excel_file.xls
20/02/2014 05:01 PM 474,112 my_excel_file.xls
20/02/2014 05:01 PM 474,112 my_excel_file.xls
21/02/2014 07:23 AM 607,232 my_excel_file.xls
21/02/2014 07:32 AM 608,768 my_excel_file.xls
21/02/2014 07:50 AM 611,328 my_excel_file.xls我的问题是:
我将终止excel进程,而不是循环,我将尝试使用该函数只编写一两组数据,并相应地更新这个问题。
发布于 2014-02-25 15:39:44
打开excel、写入单元格、保存excel、关闭excel是一项耗时的任务,当您将其与300个测试用例和每个测试的15个属性相乘时,可能会花费很长的时间。这就是在我的情况下所发生的事情,因此,这一过程需要花费很长时间才能完成。
我并不是百分之百地知道为什么excel的大小在增加,什么也没有写出来,但我猜想数据被保存在内存中,并且一旦最后一个单元格被写入,工作簿被保存并且excel关闭,我就会写数据。这从来没有发生,因为我没有让它完成,当我意识到它已经运行了非常长的一段时间后,我会杀死它。
为了使这一工作,我改变了我的方法如下。
对于测试套件中的所有测试用例,都会重复这些步骤。使用这个过程,我能够在几分钟内完成我的任务。
我知道我正在使用相当多的地图,但这是我能想到的方法。如果有人有更好的方法,我也很想尝试一下。
https://stackoverflow.com/questions/21935285
复制相似问题