This repository has been archived by the owner on Oct 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
4 changed files
with
224 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<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> | ||
<groupId>es.implementations.frameworks</groupId> | ||
<artifactId>Apache-poi</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<name>Apache POI</name> | ||
<description>Excel parser usage</description> | ||
|
||
<dependencies> | ||
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml --> | ||
<dependency> | ||
<groupId>org.apache.poi</groupId> | ||
<artifactId>poi-ooxml</artifactId> | ||
<version>3.17</version> | ||
</dependency> | ||
|
||
<!-- https://mvnrepository.com/artifact/junit/junit --> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.12</version> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<!-- https://mvnrepository.com/artifact/org.assertj/assertj-core --> | ||
<dependency> | ||
<groupId>org.assertj</groupId> | ||
<artifactId>assertj-core</artifactId> | ||
<version>3.8.0</version> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<!-- https://mvnrepository.com/artifact/org.mockito/mockito-all --> | ||
<dependency> | ||
<groupId>org.mockito</groupId> | ||
<artifactId>mockito-all</artifactId> | ||
<version>1.10.19</version> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --> | ||
<dependency> | ||
<groupId>org.projectlombok</groupId> | ||
<artifactId>lombok</artifactId> | ||
<version>1.16.18</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
</dependencies> | ||
</project> |
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,98 @@ | ||
package es.apache.poi; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.net.URL; | ||
|
||
import org.apache.poi.ss.usermodel.Cell; | ||
import org.apache.poi.ss.usermodel.Color; | ||
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.XSSFColor; | ||
import org.apache.poi.xssf.usermodel.XSSFWorkbook; | ||
|
||
import lombok.Getter; | ||
|
||
/** | ||
* Attention! There is a difference btw. excel files < 2007 and > 2007. Look the usage of POI for | ||
* both. | ||
* | ||
* @author msanchez | ||
* @since Oct 19, 2017 - 1.0.0 | ||
*/ | ||
public class ExcelParser { | ||
@Getter | ||
private FileInputStream inputStream; | ||
|
||
private Workbook workbook; | ||
|
||
@Getter | ||
private String output; | ||
|
||
/** | ||
* Saves the excel file contents into a FileInputStream. | ||
* | ||
* @param fileName | ||
* Excel file which we want to parse. | ||
* @author msanchez | ||
* @since Oct 19, 2017 | ||
*/ | ||
public void saveFileIntoStream(final String fileName) { | ||
try { | ||
final URL resource = this.getClass().getResource(fileName); | ||
final File file = new File(resource.toURI()); | ||
this.inputStream = new FileInputStream(file); | ||
} catch (final Exception ex) { | ||
ex.printStackTrace(); | ||
} | ||
} | ||
|
||
/** | ||
* Parses the contents of the file loaded into this parser instance. | ||
* | ||
* @throws IOException | ||
* @author msanchez | ||
* @since Oct 19, 2017 - 1.0.0 | ||
*/ | ||
public void parseExcel() throws IOException { | ||
final Sheet firstSheet = this.setUp(); | ||
this.workRowsInSheet(firstSheet); | ||
this.tearDown(); | ||
} | ||
|
||
private Sheet setUp() throws IOException { | ||
this.output = new String(); | ||
this.workbook = new XSSFWorkbook(this.inputStream); | ||
return this.workbook.getSheetAt(0); // Hardcoded! must change this so it parses all the sheets. | ||
} | ||
|
||
private void workRowsInSheet(final Sheet sheet) { | ||
for (final Row excelRow : sheet) { | ||
this.workSingleRow(excelRow); | ||
} | ||
} | ||
|
||
private void workSingleRow(final Row row) { | ||
for (final Cell cell : row) { | ||
this.workSingleCell(cell); | ||
} | ||
this.output += "\n"; | ||
} | ||
|
||
private void workSingleCell(final Cell cell) { | ||
final String value = cell.toString(); | ||
final String rgb = this.extractRGBColorFromCell(cell); | ||
this.output += value + ", " + rgb + "; "; | ||
} | ||
|
||
private String extractRGBColorFromCell(final Cell cell) { | ||
final Color color = cell.getCellStyle().getFillForegroundColorColor(); | ||
return color == null ? "FFFFFFFF" : XSSFColor.toXSSFColor(color).getARGBHex(); | ||
} | ||
|
||
private void tearDown() throws IOException { | ||
this.workbook.close(); | ||
} | ||
} |
Binary file not shown.
77 changes: 77 additions & 0 deletions
77
apache-poi/src/test/java/es/apache/poi/ExcelParserTest.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,77 @@ | ||
package es.apache.poi; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
|
||
import org.junit.Before; | ||
import org.junit.Ignore; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.runners.MockitoJUnitRunner; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class ExcelParserTest { | ||
private final String FILE_NAME = "excel-file.xlsx"; | ||
|
||
@InjectMocks | ||
private ExcelParser parser; | ||
|
||
@Before | ||
public void setUp() { | ||
this.parser.saveFileIntoStream(this.FILE_NAME); | ||
} | ||
|
||
/** | ||
* @author msanchez | ||
* @since Oct 19, 2017 - 1.0.0 | ||
*/ | ||
@Test | ||
@Ignore | ||
public void whenNeededThenShowDebugOutput() throws IOException { | ||
// Given | ||
|
||
// When | ||
this.parser.parseExcel(); | ||
final String output = this.parser.getOutput(); | ||
|
||
// Then | ||
assertThat(output).isNotEmpty(); | ||
System.out.println(output); | ||
} | ||
|
||
/** | ||
* @author msanchez | ||
* @since Oct 19, 2017 - 1.0.0 | ||
*/ | ||
@Test | ||
public void fileExists() { | ||
// Given | ||
final File file = new File(this.getClass().getResource(this.FILE_NAME).getPath()); | ||
|
||
// When | ||
final boolean exists = file.exists(); | ||
|
||
// Then | ||
assertThat(exists).isTrue(); | ||
} | ||
|
||
/** | ||
* @author msanchez | ||
* @since Oct 19, 2017 - 1.0.0 | ||
*/ | ||
@Test | ||
public void whenOpenFileThenInputStreamIsNotEmpty() throws IOException { | ||
// Given | ||
FileInputStream inputStream; | ||
|
||
// When | ||
inputStream = this.parser.getInputStream(); | ||
|
||
// Then | ||
assertThat(inputStream.available()).isNotZero(); | ||
} | ||
} |