Added File Reader

This commit is contained in:
Filip Izydorczyk
2020-07-28 17:38:34 +02:00
parent 3dd8f0d82d
commit 7670401d6e
6 changed files with 138 additions and 12 deletions

View File

@ -0,0 +1,55 @@
package com.plannaplan.configutils;
import java.io.IOException;
import java.io.InputStream;
import java.util.Hashtable;
import java.util.Iterator;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import com.plannaplan.models.FileData;
public class FileReader {
private InputStream fileInputStream;
public FileReader(InputStream fileInputStream) {
this.fileInputStream = fileInputStream;
}
public FileData read() {
FileData result = null;
try {
InputStream fis = this.fileInputStream;
XSSFWorkbook workbook = new XSSFWorkbook(fis);
XSSFSheet sheet = workbook.getSheetAt(0);
Iterator<Row> rowIt = sheet.iterator();
Row row = rowIt.next();
Iterator<Cell> cellIt = row.cellIterator();
Hashtable<String, Integer> keys = new Hashtable<>();
int index = 0;
while (cellIt.hasNext()) {
Cell c = cellIt.next();
keys.put(c.toString(), index);
}
rowIt.remove();
result = new FileData(keys, rowIt);
workbook.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
}