ExcelUtility Jira issue BAEL-5198 (#11503)

* CODE REFACTOR AND ADDED UNIT TEST

* SMALL CHANGE

* FIXED TESTS TIMEZONE ISSUES

Co-authored-by: Olsi Seferi <olsi.seferi@sisal.al>
This commit is contained in:
Olsi Seferi 2021-12-01 19:19:19 +01:00 committed by GitHub
parent 01ff175f5d
commit 36866c09f7
3 changed files with 120 additions and 0 deletions

View File

@ -0,0 +1,66 @@
package com.baeldung.poi.excel;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelUtility {
private static final String ENDLINE = System.getProperty("line.separator");
public static String readExcel(String filePath) throws IOException {
File file = new File(filePath);
FileInputStream inputStream = null;
StringBuilder toReturn = new StringBuilder();
try {
inputStream = new FileInputStream(file);
Workbook baeuldungWorkBook = new XSSFWorkbook(inputStream);
for (Sheet sheet : baeuldungWorkBook) {
toReturn.append("--------------------------------------------------------------------").append(ENDLINE);
toReturn.append("Worksheet :").append(sheet.getSheetName()).append(ENDLINE);
toReturn.append("--------------------------------------------------------------------").append(ENDLINE);
int firstRow = sheet.getFirstRowNum();
int lastRow = sheet.getLastRowNum();
for (int index = firstRow + 1; index <= lastRow; index++) {
Row row = sheet.getRow(index);
toReturn.append("|| ");
for (int cellIndex = row.getFirstCellNum(); cellIndex < row.getLastCellNum(); cellIndex++) {
Cell cell = row.getCell(cellIndex, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
printCellValue(cell, toReturn);
}
toReturn.append(" ||").append(ENDLINE);
}
}
inputStream.close();
} catch (IOException e) {
throw e;
}
return toReturn.toString();
}
public static void printCellValue(Cell cell, StringBuilder toReturn) {
CellType cellType = cell.getCellType().equals(CellType.FORMULA) ? cell.getCachedFormulaResultType()
: cell.getCellType();
if (cellType.equals(CellType.STRING)) {
toReturn.append(cell.getStringCellValue()).append(" | ");
}
if (cellType.equals(CellType.NUMERIC)) {
if (DateUtil.isCellDateFormatted(cell)) {
toReturn.append(cell.getDateCellValue()).append(" | ");
} else {
toReturn.append(cell.getNumericCellValue()).append(" | ");
}
}
if (cellType.equals(CellType.BOOLEAN)) {
toReturn.append(cell.getBooleanCellValue()).append(" | ");
}
}
}

View File

@ -0,0 +1,54 @@
package com.baeldung.poi.excel;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Paths;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import org.junit.Before;
import org.junit.Test;
public class ExcelUtilityUnitTest {
private static final String FILE_NAME = "baeldung.xlsx";
private String fileLocation;
private static final String ENDLINE = System.getProperty("line.separator");
private StringBuilder output;
@Before
public void setupUnitTest() throws IOException, URISyntaxException, ParseException {
output = new StringBuilder();
output.append("--------------------------------------------------------------------").append(ENDLINE);
output.append("Worksheet :Sheet1").append(ENDLINE);
output.append("--------------------------------------------------------------------").append(ENDLINE);
output.append("|| Name1 | Surname1 | 3.55696564113E11 | ").append(new SimpleDateFormat("dd/MM/yyyy").parse("4/11/2021").toString()).append(" | ‡ | ||")
.append(ENDLINE);
output.append("|| Name2 | Surname2 | 5.646513512E9 | ").append(new SimpleDateFormat("dd/MM/yyyy").parse("4/12/2021").toString()).append(" | false | ||")
.append(ENDLINE);
output.append("|| Name3 | Surname3 | 3.55696564113E11 | ").append(new SimpleDateFormat("dd/MM/yyyy").parse("4/11/2021").toString()).append(" | 7.17039641738E11 | ||")
.append(ENDLINE);
output.append("--------------------------------------------------------------------").append(ENDLINE);
output.append("Worksheet :Sheet2").append(ENDLINE);
output.append("--------------------------------------------------------------------").append(ENDLINE);
output.append("|| Name4 | Surname4 | 3.55675623232E11 | 13/04/2021 | ||").append(ENDLINE);
fileLocation = Paths.get(ClassLoader.getSystemResource(FILE_NAME).toURI()).toString();
}
@Test
public void givenStringPath_whenReadExcel_thenReturnStringValue() throws IOException {
assertEquals(output.toString(), ExcelUtility.readExcel(fileLocation));
}
@Test
public void givenStringPath_whenReadExcel_thenThrowException() {
assertThrows(IOException.class, () -> {
ExcelUtility.readExcel("baeldung");
});
}
}

Binary file not shown.