Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[New] Initial implementation of the xls files support in TabularModule
- Loading branch information
Showing
5 changed files
with
98 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
51 changes: 51 additions & 0 deletions
51
...es-modules/module-tabular/src/main/java/cz/cvut/spipes/modules/util/XLS2TSVConvertor.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,51 @@ | ||
package cz.cvut.spipes.modules.util; | ||
|
||
import cz.cvut.spipes.registry.StreamResource; | ||
import cz.cvut.spipes.registry.StringStreamResource; | ||
import org.apache.poi.hssf.usermodel.HSSFWorkbook; | ||
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 java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
|
||
/** | ||
* Module for converting tabular data from XLS to TSV. | ||
* Currently, converts only first sheet. | ||
*/ | ||
public class XLS2TSVConvertor { | ||
public StringStreamResource convertToTSV(StreamResource streamResource){ | ||
try { | ||
Workbook workbook = new HSSFWorkbook(new ByteArrayInputStream(streamResource.getContent())); | ||
Sheet sheet = workbook.getSheetAt(0); | ||
|
||
StringBuilder tsvStringBuilder = new StringBuilder(); | ||
for (Row row : sheet) { | ||
for (Cell cell : row) { | ||
tsvStringBuilder.append(cell.toString().replace('\t', ' ')); | ||
tsvStringBuilder.append('\t'); | ||
} | ||
tsvStringBuilder.deleteCharAt(tsvStringBuilder.length() - 1); | ||
tsvStringBuilder.append('\n'); | ||
} | ||
return new StringStreamResource( | ||
streamResource.getUri(), | ||
tsvStringBuilder.toString().getBytes(), | ||
"text/tsv" | ||
); | ||
} | ||
catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
public int getNumberOfSheets(StreamResource streamResource){ | ||
try { | ||
return new HSSFWorkbook(new ByteArrayInputStream(streamResource.getContent())).getNumberOfSheets(); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
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
Binary file not shown.