diff --git a/src/main/java/org/nexial/core/plugins/db/SimpleExtractionDao.java b/src/main/java/org/nexial/core/plugins/db/SimpleExtractionDao.java index bf049ce3a..8148a7c5a 100755 --- a/src/main/java/org/nexial/core/plugins/db/SimpleExtractionDao.java +++ b/src/main/java/org/nexial/core/plugins/db/SimpleExtractionDao.java @@ -378,7 +378,6 @@ protected T resultToCSV(ResultSet rs, T result, File file result.setRowCount(rowCount); outputStream.flush(); - outputStream.close(); } catch (Throwable e) { result.setError(e.getMessage()); } diff --git a/src/main/java/org/nexial/core/plugins/io/ExcelHelper.java b/src/main/java/org/nexial/core/plugins/io/ExcelHelper.java index d389aab88..38cf056f5 100755 --- a/src/main/java/org/nexial/core/plugins/io/ExcelHelper.java +++ b/src/main/java/org/nexial/core/plugins/io/ExcelHelper.java @@ -45,6 +45,7 @@ import org.nexial.core.excel.ExcelAddress; import org.nexial.core.model.ExecutionContext; import org.nexial.core.model.StepResult; +import org.nexial.core.plugins.db.DaoUtils; import org.nexial.core.utils.ConsoleUtils; import static java.lang.System.lineSeparator; @@ -87,6 +88,7 @@ protected StringBuilder xlsx2csv(File excelFile, String worksheet) throws IOExce XSSFSheet excelSheet = workBook.getSheet(worksheet); Iterator rowIterator = excelSheet.rowIterator(); StringBuilder csv = new StringBuilder(); + String delim = context.getTextDelim(); while (rowIterator.hasNext()) { XSSFRow row = (XSSFRow) rowIterator.next(); @@ -96,10 +98,10 @@ protected StringBuilder xlsx2csv(File excelFile, String worksheet) throws IOExce for (int i = 0; i < row.getLastCellNum(); i++) { value = returnCellValue(row.getCell(i)); if (StringUtils.isEmpty(value)) { value = ""; } - oneRow += value + ","; + oneRow += value + delim; } - oneRow = StringUtils.trim(StringUtils.removeEnd(oneRow, ",")); + oneRow = StringUtils.trim(StringUtils.removeEnd(oneRow, delim)); if (!oneRow.isEmpty()) { csv.append(oneRow).append(lineSeparator()); } } @@ -111,6 +113,7 @@ protected StringBuilder xls2csv(File excelFile, String worksheet) throws IOExcep HSSFSheet excelSheet = workBook.getSheet(worksheet); Iterator rowIterator = excelSheet.rowIterator(); StringBuilder csv = new StringBuilder(); + String delim = context.getTextDelim(); while (rowIterator.hasNext()) { HSSFRow row = (HSSFRow) rowIterator.next(); @@ -121,10 +124,10 @@ protected StringBuilder xls2csv(File excelFile, String worksheet) throws IOExcep HSSFCell cell = row.getCell(i); value = returnCellValue(cell); if (StringUtils.isEmpty(value)) { value = ""; } - oneRow += value + ","; + oneRow += value + delim; } - oneRow = StringUtils.trim(StringUtils.removeEnd(oneRow, ",")); + oneRow = StringUtils.trim(StringUtils.removeEnd(oneRow, delim)); if (!oneRow.isEmpty()) { csv.append(oneRow).append(lineSeparator()); } } @@ -157,24 +160,32 @@ protected StepResult saveCSVContentToFile(String file, StringBuilder csv) { protected String returnCellValue(Cell cell) { try { + String value; + switch (cell.getCellTypeEnum()) { case STRING: case BOOLEAN: - return cell.getRichStringCellValue().toString(); + value = cell.getRichStringCellValue().toString(); + break; case NUMERIC: - return formattedCellToString(cell); + value = formattedCellToString(cell); + break; case FORMULA: CellType resultType = cell.getCachedFormulaResultTypeEnum(); if (resultType == STRING) { - return cell.getRichStringCellValue().toString(); + value = cell.getRichStringCellValue().toString(); } else if (resultType == NUMERIC) { - return formattedCellToString(cell); + value = formattedCellToString(cell); } else { - return cell.getStringCellValue(); + value = cell.getStringCellValue(); } + + break; default: - return cell.getStringCellValue(); + value = cell.getStringCellValue(); } + + return DaoUtils.csvFriendly(value, context.getTextDelim(), true); } catch (Exception e) { return null; } diff --git a/src/main/kotlin/org/nexial/core/plugins/db/RdbmsCommand.kt b/src/main/kotlin/org/nexial/core/plugins/db/RdbmsCommand.kt index 2a6eca16e..3a053b69d 100755 --- a/src/main/kotlin/org/nexial/core/plugins/db/RdbmsCommand.kt +++ b/src/main/kotlin/org/nexial/core/plugins/db/RdbmsCommand.kt @@ -59,7 +59,7 @@ class RdbmsCommand : BaseCommand() { // requires(dataAccess.validSQL(query), "invalid sql", sql); val result = dataAccess.execute(query, resolveDao(db)) ?: return StepResult.fail( - "FAILED TO EXECUTE SQL '$sql': no result found") + "FAILED TO EXECUTE SQL '$sql': no result found") context.setData(`var`, result) log("executed query in ${result.elapsedTime} ms with " + @@ -254,7 +254,7 @@ class RdbmsCommand : BaseCommand() { // requires(dataAccess.validSQL(query), "invalid sql", sql); val result = dataAccess.execute(query, dao, File(outputFile)) ?: return StepResult.fail( - "FAILED TO EXECUTE SQL '$sql': no result found") + "FAILED TO EXECUTE SQL '$sql': no result found") log("executed query in ${result.elapsedTime} ms with " + if (result.hasError()) "ERROR ${result.error}" else "${result.rowCount} row(s)")