private void processWorkBook(InputStream inputStream) throws NumberFormatException, IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8")); String line = null; while ((line = bufferedReader.readLine()) != null) { for (String sheetTag : StringUtilities.multiFindBetween(line, "<sheet ", "/>")) { String name = StringUtilities.findBetween(sheetTag, "name=\"", "\""); String order = StringUtilities.findBetween(sheetTag, "sheetId=\"", "\""); String rId = StringUtilities.findBetween(sheetTag, "r:id=\"", "\""); Sheet sheet = rIdToSheet.get(rId); sheet.setName(name); sheet.order = Integer.parseInt(order); } } }
private void processSharedStrings(ZipInputStream inputStream) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8")); StringBuilder fullFile = new StringBuilder(); String line; while ((line = bufferedReader.readLine()) != null) fullFile.append(line); for (String string : StringUtilities.multiFindBetween(fullFile.toString(), "<si>", "</si>")) if (string.trim().equals("</t>")) // Empty string sharedStrings.add(""); else { string = StringUtilities.findBetween(string, ">", "<"); sharedStrings.add(string); } }
private void processRels(ZipInputStream inputStream) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8")); String line = null; while ((line = bufferedReader.readLine()) != null) for (String tag : StringUtilities.multiFindBetween(line, "<Relationship", ">")) { String rId = StringUtilities.findBetween(tag, "Id=\"", "\""); String filename = "xl/" + StringUtilities.findBetween(tag, "Target=\"", "\""); if (filename.contains("/sheet")) { Sheet sheet = new Sheet(); add(sheet); rIdToSheet.put(rId, sheet); filenameToSheet.put(filename, sheet); } } }
public List<String> findCellValues(String string) { List<String> result = new ArrayList<String>(); int tagStart = -1; int stringStart = -1; int column = -1; boolean sharedString = false; for (int i = 0; i < string.length(); i++) { char ch = string.charAt(i); if (ch == '<') tagStart = i + 1; else if (ch == '>') { if (tagStart != -1 && i > tagStart) { String tag = string.substring(tagStart, i); if (tag.charAt(0) == 'c') { sharedString = tag.contains("t=\"s\""); column = parseColumn(StringUtilities.findBetween(tag, "r=\"", "\"")); } else if (tag.startsWith("v") || tag.startsWith("t")) stringStart = i + 1; else if (tag.equals("/v") || tag.equals("/t")) { if (stringStart != -1 && i > stringStart) { for (int j = result.size(); j <= column; j++) result.add(""); if (sharedString) { int index = Integer.parseInt(string.substring(stringStart, tagStart - 1)); result.set(column, sharedStrings.get(index)); } else result.set(column, decode(string.substring(stringStart, tagStart - 1))); } stringStart = -1; column = -1; } } } } return result; }
private void processSheet(String filename, ZipInputStream inputStream) throws IOException { Sheet sheet = filenameToSheet.get(filename); // System.out.println(filename + "\t" + sheet.name); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8")); String line; StringBuilder fullSheet = new StringBuilder(); while ((line = bufferedReader.readLine()) != null) fullSheet.append(line); for (String rowLine : StringUtilities.multiFindBetween(fullSheet.toString(), "<row", "</row>")) { Row row = new Row(); row.addAll(findCellValues(rowLine)); if (row.size() != 0) sheet.add(row); } }