-2
我有下面的代碼從git在eclipse中工作100%。它在控制檯上將excel文件轉換爲Xml格式。當我將其複製/粘貼到soapUI Groovy腳本中並播放時,我沒有看到任何事情發生。我試圖用log.info
替換println
無濟於事,所以我試圖將xml返回到一個變量,但我不知道如何做到這一點,因爲它看起來像它需要發生在類之外 - >我很困惑如何從main
獲取testCase屬性的信息。我也嘗試使用main
內部的testRunner
變量和class
內部的變量作爲獲取者和設置者,但是......錯誤,錯誤和更多錯誤大聲笑。替換System.out.println與類groovy之外的變量soapui
感謝您的任何反饋
import org.apache.poi.ss.usermodel.*
import org.apache.poi.hssf.usermodel.*
import org.apache.poi.xssf.usermodel.*
import org.apache.poi.ss.util.*
import org.apache.poi.ss.usermodel.*
import java.io.*
class GroovyExcelParser {
//http://poi.apache.org/spreadsheet/quick-guide.html#Iterator
def parse(path) {
InputStream inp = new FileInputStream(path)
Workbook wb = WorkbookFactory.create(inp);
Sheet sheet = wb.getSheetAt(0);
Iterator<Row> rowIt = sheet.rowIterator()
Row row = rowIt.next()
def headers = getRowData(row)
def rows = []
while(rowIt.hasNext()) {
row = rowIt.next()
rows << getRowData(row)
}
[headers, rows]
}
def getRowData(Row row) {
def data = []
for (Cell cell : row) {
getValue(row, cell, data)
}
data
}
def getRowReference(Row row, Cell cell) {
def rowIndex = row.getRowNum()
def colIndex = cell.getColumnIndex()
CellReference ref = new CellReference(rowIndex, colIndex)
ref.getRichStringCellValue().getString()
}
def getValue(Row row, Cell cell, List data) {
def rowIndex = row.getRowNum()
def colIndex = cell.getColumnIndex()
def value = ""
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
value = cell.getRichStringCellValue().getString();
break;
case Cell.CELL_TYPE_NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
value = cell.getDateCellValue();
} else {
value = cell.getNumericCellValue().toInteger();
}
break;
case Cell.CELL_TYPE_BOOLEAN:
value = cell.getBooleanCellValue();
break;
case Cell.CELL_TYPE_FORMULA:
value = cell.getCellFormula();
break;
default:
value = ""
}
data[colIndex] = value
data
}
def toXml(header, row) {
def obj = "<object>\n"
row.eachWithIndex { datum, i ->
def headerName = header[i]
obj += "\t<$headerName>$datum</$headerName>\n"
}
obj += "</object>"
}
public static void main(String[]args) {
def filename = 'RaNdOmxlsxFile.xslx'
GroovyExcelParser parser = new GroovyExcelParser()
def (headers, rows) = parser.parse(filename)
System.out.println 'Headers'
System.out.println '------------------'
headers.each { header ->
System.out.println header
}
System.out.println "\n"
System.out.println 'Rows'
System.out.println '------------------'
rows.each { row ->
System.out.println parser.toXml(headers, row)
}
}
}
饒,謝謝:)我試過#3,它的工作方式我現在需要它:) – MistaWizard