-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test library to handle Excel file
Signed-off-by: Viet Nguyen Duc <[email protected]>
- Loading branch information
Showing
3 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
72 changes: 72 additions & 0 deletions
72
...aries/test-libraries-office-docs/src/main/java/org/ndviet/library/excel/ExcelHelpers.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ""; | ||
} | ||
} | ||
} |