You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TestXSSFBEventBasedExcelExtractor.java 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.xssf.extractor;
  16. import static org.junit.Assert.assertEquals;
  17. import static org.junit.Assert.assertTrue;
  18. import org.apache.poi.xssf.XSSFTestDataSamples;
  19. import org.junit.Test;
  20. /**
  21. * Tests for {@link XSSFBEventBasedExcelExtractor}
  22. */
  23. public class TestXSSFBEventBasedExcelExtractor {
  24. protected XSSFEventBasedExcelExtractor getExtractor(String sampleName) throws Exception {
  25. return new XSSFBEventBasedExcelExtractor(XSSFTestDataSamples.
  26. openSamplePackage(sampleName));
  27. }
  28. /**
  29. * Get text out of the simple file
  30. */
  31. @Test
  32. public void testGetSimpleText() throws Exception {
  33. // a very simple file
  34. XSSFEventBasedExcelExtractor extractor = getExtractor("sample.xlsb");
  35. extractor.setIncludeCellComments(true);
  36. extractor.getText();
  37. String text = extractor.getText();
  38. assertTrue(text.length() > 0);
  39. // Check sheet names
  40. assertTrue(text.startsWith("Sheet1"));
  41. assertTrue(text.endsWith("Sheet3\n"));
  42. // Now without, will have text
  43. extractor.setIncludeSheetNames(false);
  44. text = extractor.getText();
  45. String CHUNK1 =
  46. "Lorem\t111\n" +
  47. "ipsum\t222\n" +
  48. "dolor\t333\n" +
  49. "sit\t444\n" +
  50. "amet\t555\n" +
  51. "consectetuer\t666\n" +
  52. "adipiscing\t777\n" +
  53. "elit\t888\n" +
  54. "Nunc\t999\n";
  55. String CHUNK2 =
  56. "The quick brown fox jumps over the lazy dog\n" +
  57. "hello, xssf hello, xssf\n" +
  58. "hello, xssf hello, xssf\n" +
  59. "hello, xssf hello, xssf\n" +
  60. "hello, xssf hello, xssf\n";
  61. assertEquals(
  62. CHUNK1 +
  63. "at\t4995\n" +
  64. CHUNK2
  65. , text);
  66. }
  67. /**
  68. * Test text extraction from text box using getShapes()
  69. *
  70. * @throws Exception
  71. */
  72. @Test
  73. public void testShapes() throws Exception {
  74. XSSFEventBasedExcelExtractor ooxmlExtractor = getExtractor("WithTextBox.xlsb");
  75. try {
  76. String text = ooxmlExtractor.getText();
  77. assertTrue(text.indexOf("Line 1") > -1);
  78. assertTrue(text.indexOf("Line 2") > -1);
  79. assertTrue(text.indexOf("Line 3") > -1);
  80. } finally {
  81. ooxmlExtractor.close();
  82. }
  83. }
  84. }