PageRenderTime 61ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/poi/src/main/java/com/qmetric/utilities/poi/xssf/XSSFWorkBookProcessor.java

https://github.com/qmetric/qmg-utilities
Java | 65 lines | 53 code | 12 blank | 0 comment | 0 complexity | 2a765e0855ac52a63fa667a22d438072 MD5 | raw file
  1. package com.qmetric.utilities.poi.xssf;
  2. import com.qmetric.utilities.file.FileUtils;
  3. import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
  4. import org.apache.poi.xssf.eventusermodel.XSSFReader;
  5. import org.apache.poi.xssf.model.SharedStringsTable;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. public class XSSFWorkBookProcessor
  9. {
  10. private final XSSFWorksheetParserFactory xssfWorksheetParserFactory;
  11. private final FileUtils fileUtils;
  12. private final XSSFReaderFactory xssfReaderFactory;
  13. public XSSFWorkBookProcessor(final XSSFWorksheetParserFactory xssfWorksheetParserFactory, final FileUtils fileUtils, final XSSFReaderFactory xssfReaderFactory)
  14. {
  15. this.xssfWorksheetParserFactory = xssfWorksheetParserFactory;
  16. this.fileUtils = fileUtils;
  17. this.xssfReaderFactory = xssfReaderFactory;
  18. }
  19. public void process(final String location, final SheetProcessor sheetProcessor)
  20. {
  21. process(fileUtils.inputStreamFrom(location), sheetProcessor);
  22. }
  23. public void process(final InputStream stream, final SheetProcessor sheetProcessor)
  24. {
  25. final XSSFReader xssfReader = xssfReaderFactory.create(stream);
  26. try
  27. {
  28. XSSFReader.SheetIterator sheetsData = (XSSFReader.SheetIterator) xssfReader.getSheetsData();
  29. processSheets(sheetsData, sheetProcessor, xssfReader.getSharedStringsTable());
  30. }
  31. catch (IOException e)
  32. {
  33. e.printStackTrace();
  34. }
  35. catch (InvalidFormatException e)
  36. {
  37. e.printStackTrace();
  38. }
  39. }
  40. private void processSheets(final XSSFReader.SheetIterator sheetsData, final SheetProcessor sheetProcessor, final SharedStringsTable sharedStringsTable)
  41. {
  42. while(sheetsData.hasNext())
  43. {
  44. processNext(sheetsData.next(), sheetsData.getSheetName(), sheetProcessor, sharedStringsTable);
  45. }
  46. }
  47. private void processNext(final InputStream next, final String sheetName, final SheetProcessor sheetProcessor, final SharedStringsTable sharedStringsTable)
  48. {
  49. final WorksheetParser worksheetParser =
  50. xssfWorksheetParserFactory.createParserFor(new RichTextSharedStringTableLookup(sharedStringsTable), sheetProcessor.getNewRowProcessor(sheetName));
  51. worksheetParser.parse(next);
  52. }
  53. }