Skip to content

Commit

Permalink
Add test library to handle Excel file
Browse files Browse the repository at this point in the history
Signed-off-by: Viet Nguyen Duc <[email protected]>
  • Loading branch information
VietND96 committed Nov 11, 2023
1 parent 3e6a8eb commit b3d91cb
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 0 deletions.
1 change: 1 addition & 0 deletions test-libraries/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<module>test-libraries-webui</module>
<module>test-libraries-api</module>
<module>test-libraries-kubernetes</module>
<module>test-libraries-office-docs</module>
<module>test-libraries-listeners</module>
</modules>

Expand Down
26 changes: 26 additions & 0 deletions test-libraries/test-libraries-office-docs/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.ndviet</groupId>
<artifactId>test-libraries</artifactId>
<version>${revision}</version>
</parent>

<artifactId>test-libraries-office-docs</artifactId>
<name>Test Libraries - Office Docs</name>

<dependencies>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package org.ndviet.library.excel;

import org.apache.poi.ss.usermodel.Cell;
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;

import java.io.File;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

public class ExcelHelpers {
public static List getNameOfSheets(String filePath) throws Exception {
Workbook workbook = new XSSFWorkbook(new File(filePath));
List<String> listSheets = new ArrayList<>();
int numberOfSheets = workbook.getNumberOfSheets();
for (int i = 0; i < numberOfSheets; i++) {
listSheets.add(workbook.getSheetName(i));
}
return listSheets;
}

public static LinkedHashMap getMapValuesBySheetName(String filePath) throws Exception {
List<String> sheets = getNameOfSheets(filePath);
System.out.println(sheets);
return getMapValuesBySheetName(filePath, sheets.get(0));
}

public static LinkedHashMap getMapValuesBySheetName(String filePath, String sheetName) throws Exception {
Workbook workbook = new XSSFWorkbook(new File(filePath));
Sheet sheet = workbook.getSheet(sheetName);
LinkedHashMap<String, List<String>> sheet_map = new LinkedHashMap<>();
Row headers = sheet.getRow(0);
int numberOfColumns = headers.getPhysicalNumberOfCells();
for (int columnIndex = 0; columnIndex < numberOfColumns; columnIndex++) {
String headerName = null;
List<String> listValues = new ArrayList<>();
for (int rowIndex = 0; rowIndex < sheet.getPhysicalNumberOfRows(); rowIndex++) {
Row row = sheet.getRow(rowIndex);
Cell cell = row.getCell(columnIndex);
if (cell != null) {
if (rowIndex > 0) {
listValues.add(getCellValue(cell));
} else {
headerName = getCellValue(cell);
}
}
}
sheet_map.put(headerName, listValues);
}
return sheet_map;
}

public static String getCellValue(Cell cell) {
switch (cell.getCellType()) {
case STRING:
return cell.getStringCellValue();
case NUMERIC:
return cell.getNumericCellValue() + "";
case BOOLEAN:
return cell.getBooleanCellValue() + "";
case ERROR:
return cell.getErrorCellValue() + "";
case FORMULA:
return cell.getCellFormula();
default:
return "";
}
}
}

0 comments on commit b3d91cb

Please sign in to comment.