Skip to content
Snippets Groups Projects
Commit b9b1e0f1 authored by Erik Diers's avatar Erik Diers
Browse files

Fixed xlsx read errors

parent a25fb7a9
Branches
No related tags found
No related merge requests found
No preview for this file type
......@@ -225,6 +225,13 @@ public class Main {
obj.put("Subject age", currentRow[1]);
obj.put("Subject sex", SFB_JSON_Util.getGenderFromStringStart(currentRow[2]).toString());
if (currentRow[3].equals("patient")) {
currentRow[3] = "Patient";
}
if (currentRow[3].toLowerCase().equals("healthy control subject")) {
currentRow[3] = "Healthy control subject";
}
if (!currentRow[3].equals("Healthy test subject") && !currentRow[3].equals("Patient") && !currentRow[3].equals("Healthy control subject"))
currentRow[3] = "Healthy test subject";
......
......@@ -5,15 +5,11 @@ import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Calendar;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.io.InputStream;
public class CSV_Reader {
private String[] headers;
......@@ -198,14 +194,18 @@ public class CSV_Reader {
LinkedList<CellData> cellDatas = null;
String[] sharedStrings = null;
LinkedList<Integer> dateStyles = null;
while(ze != null) {
String fileName = ze.getName();
if ((sheet == null && fileName.contains("worksheets")) || (sheet != null && fileName.endsWith(sheet + ".xml"))) {
int workSheetsIndx = fileName.indexOf("/worksheets/");
if ((sheet == null && workSheetsIndx != -1 && fileName.lastIndexOf("/") == workSheetsIndx + 11 && fileName.endsWith(".xml")) || (sheet != null && fileName.endsWith(sheet + ".xml"))) {
cellDatas = cellDataFromXLSXextractedXML(readZipEntryToString(zis));
} else if (fileName.endsWith("sharedStrings.xml")) {
sharedStrings = sharedStringsFromXML(readZipEntryToString(zis));
} else if (fileName.endsWith("styles.xml")) {
dateStyles = dateStylesFromXML(readZipEntryToString(zis));
}
......@@ -241,7 +241,14 @@ public class CSV_Reader {
continue;
}
c.value = sharedStrings[val];
}
//Handle Styled Entries
if(c.type == CellData.Type.STYLED) {
if (dateStyles.contains(c.style)) {
c.switchType(CellData.Type.DATE);
} else {
c.switchType(CellData.Type.NUMBER);
}
}
}
int w = maxX - minX + 1;
......@@ -282,6 +289,34 @@ public class CSV_Reader {
return xml.toString();
}
private static LinkedList<Integer> dateStylesFromXML(String xml) {
int cellStylesIndx = xml.indexOf("<cellXfs");
LinkedList<Integer> dateStyles = new LinkedList<>();
if (cellStylesIndx == -1)
return dateStyles;
xml = xml.substring(cellStylesIndx, xml.indexOf("</cellXfs>", cellStylesIndx));
int index = 0;
int elementIndex = -1;
while((index = xml.indexOf("<xf", index)) != -1) {
elementIndex++;
int endOfTag = xml.indexOf('>', index);
int numFormatIdIndx = xml.indexOf("numFmtId=\"", index);
if (numFormatIdIndx > endOfTag) {
index = endOfTag;
continue;
}
numFormatIdIndx += 10;
int formatId = Integer.parseInt(xml.substring(numFormatIdIndx, xml.indexOf('\"', numFormatIdIndx)));
if (formatId == 14 || formatId == 15 || formatId == 16 || formatId == 17 || formatId == 22)
dateStyles.add(elementIndex);
index = endOfTag;
}
return dateStyles;
}
private static String[] sharedStringsFromXML(String xml) {
LinkedList<String> res = new LinkedList<>();
......@@ -308,16 +343,19 @@ public class CSV_Reader {
CellData.Type type = CellData.Type.NUMBER;
int typeString = sheetData.indexOf("t=\"s\"", index);
boolean isStr = typeString != -1 && typeString < endOfTag;
int style = 0;
if (isStr) {
type = CellData.Type.STRING;
}
int typeDate = sheetData.indexOf("s=\"1\"", index);
boolean isDate = typeDate != -1 && typeDate < endOfTag;
} else {
if (isDate) {
type = CellData.Type.DATE;
int typeStyled = sheetData.indexOf("s=\"", index);
boolean isStyled = typeStyled != -1 && typeStyled < endOfTag;
if (isStyled) {
type = CellData.Type.STYLED;
style = Integer.parseInt(sheetData.substring (typeStyled + 3, sheetData.indexOf('\"', typeStyled + 3)));
}
}
int valOpen = sheetData.indexOf("<v>", endOfTag);
......@@ -329,7 +367,7 @@ public class CSV_Reader {
}
String value = sheetData.substring(valOpen + 3, valClose);
cellData.add(new CellData(cellID, value, type));
cellData.add(new CellData(cellID, value, type, style));
index = endOfTag;
}
......@@ -338,18 +376,24 @@ public class CSV_Reader {
private static class CellData {
private static final Calendar CALENDAR = Calendar.getInstance();
public static enum Type {NUMBER, STRING, DATE}
public static enum Type {NUMBER, STRING, DATE, STYLED}
public String cellID;
public String value;
public Type type;
public int style;
public CellData(String cellID, String value, Type type) {
public CellData(String cellID, String value, Type type, int style) {
this.cellID = cellID;
this.value = value;
this.type = type;
if (type == Type.DATE) {
this.style = style;
}
public void switchType(Type newType) {
type = newType;
if (newType == Type.DATE) {
if (this.value.contains(".")) {
this.value = this.value.substring(0, value.indexOf("."));
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment