private String extractHeaderFooter(HeaderFooter hf) { return ExcelExtractor._extractHeaderFooter(hf); } }
public OldExcelExtractor(InputStream input) throws IOException { open(input); }
cmdArgs = new CommandArgs(args); } catch (CommandParseException e) { System.err.println(e.getMessage()); printUsageMessage(System.err); System.exit(1); return; // suppress compiler error if (cmdArgs.isRequestHelp()) { printUsageMessage(System.out); return; if(cmdArgs.getInputFile() == null) { is = System.in; } else { is = new FileInputStream(cmdArgs.getInputFile()); ExcelExtractor extractor = new ExcelExtractor(wb); extractor.setIncludeSheetNames(cmdArgs.shouldShowSheetNames()); extractor.setFormulasNotResults(!cmdArgs.shouldEvaluateFormulas()); extractor.setIncludeCellComments(cmdArgs.shouldShowCellComments()); extractor.setIncludeBlankCells(cmdArgs.shouldShowBlankCells()); extractor.setIncludeHeadersFooters(cmdArgs.shouldIncludeHeadersFooters()); System.out.println(extractor.getText()); extractor.close(); wb.close();
/** * 获取 {@link ExcelExtractor} 对象 * * @return {@link ExcelExtractor} * @since 4.1.0 */ public ExcelExtractor getExtractor() { ExcelExtractor extractor; Workbook wb = this.workbook; if (wb instanceof HSSFWorkbook) { extractor = new org.apache.poi.hssf.extractor.ExcelExtractor((HSSFWorkbook) wb); } else { extractor = new XSSFExcelExtractor((XSSFWorkbook) wb); } return extractor; }
if (poifsDir.hasEntry(workbookName)) { if (getPreferEventExtractor()) { return new EventBasedExcelExtractor(poifsDir); return new ExcelExtractor(poifsDir);
public static void main(String[] args) throws IOException { if (args.length < 1) { System.err.println("Use:"); System.err.println(" OldExcelExtractor <filename>"); System.exit(1); } OldExcelExtractor extractor = new OldExcelExtractor(new File(args[0])); System.out.println(extractor.getText()); extractor.close(); }
throw new CommandParseException("Expected filename after '-i'"); throw new CommandParseException("Only one input file can be supplied"); throw new CommandParseException("Specified input file '" + arg + "' does not exist"); throw new CommandParseException("Specified input file '" + arg + "' is a directory"); showSheetNames = parseBoolArg(args, ++i); continue; evaluateFormulas = parseBoolArg(args, ++i); continue; showCellComments = parseBoolArg(args, ++i); continue; showBlankCells = parseBoolArg(args, ++i); continue; headersFooters = parseBoolArg(args, ++i); continue; throw new CommandParseException("Invalid argument '" + arg + "'");
private static boolean parseBoolArg(String[] args, int i) throws CommandParseException { if (i >= args.length) { throw new CommandParseException("Expected value after '" + args[i-1] + "'"); } String value = args[i].toUpperCase(Locale.ROOT); if ("Y".equals(value) || "YES".equals(value) || "ON".equals(value) || "TRUE".equals(value)) { return true; } if ("N".equals(value) || "NO".equals(value) || "OFF".equals(value) || "FALSE".equals(value)) { return false; } throw new CommandParseException("Invalid value '" + args[i] + "' for '" + args[i-1] + "'. Expected 'Y' or 'N'"); } public boolean isRequestHelp() {
/** * Retreives the text contents of the file */ public String getText() { String text; try { TextListener tl = triggerExtraction(); text = tl._text.toString(); if(! text.endsWith("\n")) { text = text + "\n"; } } catch(IOException e) { throw new RuntimeException(e); } return text; }
private void open(InputStream biffStream) throws IOException { BufferedInputStream bis = (biffStream instanceof BufferedInputStream) ? (BufferedInputStream)biffStream : new BufferedInputStream(biffStream, 8); if (FileMagic.valueOf(bis) == FileMagic.OLE2) { POIFSFileSystem poifs = new POIFSFileSystem(bis); try { open(poifs); toClose = poifs; // Fixed by GR, we should not close it here } finally { if (toClose == null) { poifs.close(); } } } else { ris = new RecordInputStream(bis); toClose = bis; prepare(); } }
private TextListener triggerExtraction() throws IOException { TextListener tl = new TextListener(); FormatTrackingHSSFListener ft = new FormatTrackingHSSFListener(tl); tl._ft = ft; // Register and process HSSFEventFactory factory = new HSSFEventFactory(); HSSFRequest request = new HSSFRequest(); request.addListenerForAllRecords(ft); factory.processWorkbookEvents(request, _dir); return tl; }
handleNumericCell(text, nr.getValue()); break; case OldFormulaRecord.biff2_sid: FormulaRecord fr = new FormulaRecord(ris); if (fr.getCachedResultType() == CellType.NUMERIC.getCode()) { handleNumericCell(text, fr.getValue()); handleNumericCell(text, fr.getValue()); case RKRecord.sid: RKRecord rr = new RKRecord(ris); handleNumericCell(text, rr.getRKNumber()); break; close(); ris = null;
private void open(DirectoryNode directory) throws IOException { DocumentNode book; try { book = (DocumentNode)directory.getEntry(OLD_WORKBOOK_DIR_ENTRY_NAME); } catch (FileNotFoundException e) { // some files have "Workbook" instead book = (DocumentNode)directory.getEntry(WORKBOOK_DIR_ENTRY_NAMES[0]); } if (book == null) { throw new IOException("No Excel 5/95 Book stream found"); } ris = new RecordInputStream(directory.createDocumentInputStream(book)); prepare(); }
/** * 获取 {@link ExcelExtractor} 对象 * * @return {@link ExcelExtractor} * @since 4.1.0 */ public ExcelExtractor getExtractor() { ExcelExtractor extractor; Workbook wb = this.workbook; if (wb instanceof HSSFWorkbook) { extractor = new org.apache.poi.hssf.extractor.ExcelExtractor((HSSFWorkbook) wb); } else { extractor = new XSSFExcelExtractor((XSSFWorkbook) wb); } return extractor; }
text.append(_extractHeaderFooter(sheet.getHeader())); text.append(_extractHeaderFooter(sheet.getFooter()));
public OldExcelExtractor(DirectoryNode directory) throws IOException { open(directory); }
protected void extractHeaderFooter(String hf, XHTMLContentHandler xhtml) throws SAXException { String content = ExcelExtractor._extractHeaderFooter( new HeaderFooterFromString(hf)); if (content.length() > 0) { xhtml.element("p", content); } }
public OldExcelExtractor(POIFSFileSystem fs) throws IOException { open(fs); }
private void open(POIFSFileSystem fs) throws IOException { open(fs.getRoot()); }
public OldExcelExtractor(File f) throws IOException { POIFSFileSystem poifs = null; try { poifs = new POIFSFileSystem(f); open(poifs); toClose = poifs; return; } catch (OldExcelFormatException | NotOLE2FileException e) { // will be handled by workaround below } finally { if (toClose == null) { IOUtils.closeQuietly(poifs); } } @SuppressWarnings("resource") FileInputStream biffStream = new FileInputStream(f); // NOSONAR try { open(biffStream); } catch (IOException | RuntimeException e) { // ensure that the stream is properly closed here if an Exception // is thrown while opening biffStream.close(); throw e; } }