Esempio n. 1
0
  /**
   * Initiates the processing of the XLS workbook file to CSV.
   *
   * @throws IOException
   * @throws OpenXML4JException
   * @throws ParserConfigurationException
   * @throws SAXException
   */
  public void process()
      throws IOException, OpenXML4JException, ParserConfigurationException, SAXException {

    ReadonlySharedStringsTable strings = new ReadonlySharedStringsTable(this.xlsxPackage);
    XSSFReader xssfReader = new XSSFReader(this.xlsxPackage);
    StylesTable styles = xssfReader.getStylesTable();
    XSSFReader.SheetIterator iter = (XSSFReader.SheetIterator) xssfReader.getSheetsData();
    int index = 0;
    while (iter.hasNext()) {
      InputStream stream = iter.next();
      String sheetName = iter.getSheetName();
      this.output.println();
      this.output.println(sheetName + " [index=" + index + "]:");
      processSheet(styles, strings, stream);
      stream.close();
      ++index;
    }
  }
Esempio n. 2
0
  public Object call() throws Exception {
    OPCPackage pkg = null;
    try {
      if (fileURL != null) {
        pkg = OPCPackage.open(fileURL);
      } else {
        pkg = PackageHelper.open(is);
      }
      XSSFReader r = new XSSFReader(pkg);

      StylesTable styles = r.getStylesTable();
      ReadOnlySharedStringsTable strings = new ReadOnlySharedStringsTable(pkg);
      sheetContentsHandler = new DefaultTalendSheetContentsHandler(cache);
      DataFormatter formatter = new DataFormatter();
      boolean formulasNotResults = false;

      XMLReader parser = XMLReaderFactory.createXMLReader();
      ContentHandler handler =
          new TalendXSSFSheetXMLHandler(
              styles, strings, sheetContentsHandler, formatter, formulasNotResults);
      parser.setContentHandler(handler);

      XSSFReader.SheetIterator sheets = (XSSFReader.SheetIterator) r.getSheetsData();
      List<InputStream> iss = new ArrayList<InputStream>();
      while (sheets.hasNext()) {
        InputStream sheet = sheets.next();
        String sheetName = sheets.getSheetName();

        boolean match = false;

        for (int i = 0; i < sheetNames.size(); i++) {
          if ((asRegexs.get(i) && sheetName.matches(sheetNames.get(i)))
              || (!asRegexs.get(i) && sheetName.equals(sheetNames.get(i)))) {
            match = true;
            iss.add(sheet);
            break;
          }
        }

        if (!match) {
          sheet.close();
        }
      }

      if (iss.size() < 1) {
        throw new RuntimeException("No match sheets");
      }

      for (InputStream is : iss) {
        try {
          InputSource sheetSource = new InputSource(is);
          sheetSource.setEncoding(charset);
          parser.parse(sheetSource);
        } finally {
          is.close();
        }
      }

    } finally {
      if (pkg != null) {
        pkg.revert();
      }
      cache.notifyErrorOccurred();
    }
    return null;
  }