Files
backend/buisnesslogic/src/main/java/com/plannaplan/configutils/FileReader.java
Marcin Woźniak 6d9c5a8a5d Added
Signed-off-by: Marcin Woźniak <y0rune@aol.com>
2020-11-20 14:55:20 +01:00

60 lines
1.5 KiB
Java
Executable File

package com.plannaplan.configutils;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
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;
/**
* FileReader is used for reading xls file from input stream.
*/
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();
HashMap<String, Integer> keys = new HashMap<>();
int index = 0;
while (cellIt.hasNext()) {
Cell c = cellIt.next();
keys.put(c.toString(), index);
index += 1;
}
rowIt.remove();
result = new FileData(keys, rowIt);
workbook.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
}