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.

TestXSSFExcelExtractor.java 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 java.io.IOException;
  17. import java.util.regex.Matcher;
  18. import java.util.regex.Pattern;
  19. import junit.framework.TestCase;
  20. import org.apache.poi.POITextExtractor;
  21. import org.apache.poi.hssf.HSSFTestDataSamples;
  22. import org.apache.poi.hssf.extractor.ExcelExtractor;
  23. import org.apache.poi.xssf.XSSFTestDataSamples;
  24. /**
  25. * Tests for {@link XSSFExcelExtractor}
  26. */
  27. public class TestXSSFExcelExtractor extends TestCase {
  28. protected XSSFExcelExtractor getExtractor(String sampleName) {
  29. return new XSSFExcelExtractor(XSSFTestDataSamples.openSampleWorkbook(sampleName));
  30. }
  31. /**
  32. * Get text out of the simple file
  33. * @throws IOException
  34. */
  35. public void testGetSimpleText() throws IOException {
  36. // a very simple file
  37. XSSFExcelExtractor extractor = getExtractor("sample.xlsx");
  38. extractor.getText();
  39. String text = extractor.getText();
  40. assertTrue(text.length() > 0);
  41. // Check sheet names
  42. assertTrue(text.startsWith("Sheet1"));
  43. assertTrue(text.endsWith("Sheet3\n"));
  44. // Now without, will have text
  45. extractor.setIncludeSheetNames(false);
  46. text = extractor.getText();
  47. String CHUNK1 =
  48. "Lorem\t111\n" +
  49. "ipsum\t222\n" +
  50. "dolor\t333\n" +
  51. "sit\t444\n" +
  52. "amet\t555\n" +
  53. "consectetuer\t666\n" +
  54. "adipiscing\t777\n" +
  55. "elit\t888\n" +
  56. "Nunc\t999\n";
  57. String CHUNK2 =
  58. "The quick brown fox jumps over the lazy dog\n" +
  59. "hello, xssf hello, xssf\n" +
  60. "hello, xssf hello, xssf\n" +
  61. "hello, xssf hello, xssf\n" +
  62. "hello, xssf hello, xssf\n";
  63. assertEquals(
  64. CHUNK1 +
  65. "at\t4995\n" +
  66. CHUNK2
  67. , text);
  68. // Now get formulas not their values
  69. extractor.setFormulasNotResults(true);
  70. text = extractor.getText();
  71. assertEquals(
  72. CHUNK1 +
  73. "at\tSUM(B1:B9)\n" +
  74. CHUNK2, text);
  75. // With sheet names too
  76. extractor.setIncludeSheetNames(true);
  77. text = extractor.getText();
  78. assertEquals(
  79. "Sheet1\n" +
  80. CHUNK1 +
  81. "at\tSUM(B1:B9)\n" +
  82. "rich test\n" +
  83. CHUNK2 +
  84. "Sheet3\n"
  85. , text);
  86. extractor.close();
  87. }
  88. public void testGetComplexText() throws IOException {
  89. // A fairly complex file
  90. XSSFExcelExtractor extractor = getExtractor("AverageTaxRates.xlsx");
  91. extractor.getText();
  92. String text = extractor.getText();
  93. assertTrue(text.length() > 0);
  94. // Might not have all formatting it should do!
  95. // TODO decide if we should really have the "null" in there
  96. assertTrue(text.startsWith(
  97. "Avgtxfull\n" +
  98. "null\t(iii) AVERAGE TAX RATES ON ANNUAL"
  99. ));
  100. extractor.close();
  101. }
  102. /**
  103. * Test that we return pretty much the same as
  104. * ExcelExtractor does, when we're both passed
  105. * the same file, just saved as xls and xlsx
  106. * @throws IOException
  107. */
  108. public void testComparedToOLE2() throws IOException {
  109. // A fairly simple file - ooxml
  110. XSSFExcelExtractor ooxmlExtractor = getExtractor("SampleSS.xlsx");
  111. ExcelExtractor ole2Extractor =
  112. new ExcelExtractor(HSSFTestDataSamples.openSampleWorkbook("SampleSS.xls"));
  113. POITextExtractor[] extractors =
  114. new POITextExtractor[] { ooxmlExtractor, ole2Extractor };
  115. for (POITextExtractor extractor : extractors) {
  116. String text = extractor.getText().replaceAll("[\r\t]", "");
  117. assertTrue(text.startsWith("First Sheet\nTest spreadsheet\n2nd row2nd row 2nd column\n"));
  118. Pattern pattern = Pattern.compile(".*13(\\.0+)?\\s+Sheet3.*", Pattern.DOTALL);
  119. Matcher m = pattern.matcher(text);
  120. assertTrue(m.matches());
  121. }
  122. ole2Extractor.close();
  123. ooxmlExtractor.close();
  124. }
  125. /**
  126. * From bug #45540
  127. * @throws IOException
  128. */
  129. public void testHeaderFooter() throws IOException {
  130. String[] files = new String[] {
  131. "45540_classic_Header.xlsx", "45540_form_Header.xlsx",
  132. "45540_classic_Footer.xlsx", "45540_form_Footer.xlsx",
  133. };
  134. for(String sampleName : files) {
  135. XSSFExcelExtractor extractor = getExtractor(sampleName);
  136. String text = extractor.getText();
  137. assertTrue("Unable to find expected word in text from " + sampleName + "\n" + text, text.contains("testdoc"));
  138. assertTrue("Unable to find expected word in text\n" + text, text.contains("test phrase"));
  139. extractor.close();
  140. }
  141. }
  142. /**
  143. * From bug #45544
  144. * @throws IOException
  145. */
  146. public void testComments() throws IOException {
  147. XSSFExcelExtractor extractor = getExtractor("45544.xlsx");
  148. String text = extractor.getText();
  149. // No comments there yet
  150. assertFalse("Unable to find expected word in text\n" + text, text.contains("testdoc"));
  151. assertFalse("Unable to find expected word in text\n" + text, text.contains("test phrase"));
  152. // Turn on comment extraction, will then be
  153. extractor.setIncludeCellComments(true);
  154. text = extractor.getText();
  155. assertTrue("Unable to find expected word in text\n" + text, text.contains("testdoc"));
  156. assertTrue("Unable to find expected word in text\n" + text, text.contains("test phrase"));
  157. extractor.close();
  158. }
  159. public void testInlineStrings() throws IOException {
  160. XSSFExcelExtractor extractor = getExtractor("InlineStrings.xlsx");
  161. extractor.setFormulasNotResults(true);
  162. String text = extractor.getText();
  163. // Numbers
  164. assertTrue("Unable to find expected word in text\n" + text, text.contains("43"));
  165. assertTrue("Unable to find expected word in text\n" + text, text.contains("22"));
  166. // Strings
  167. assertTrue("Unable to find expected word in text\n" + text, text.contains("ABCDE"));
  168. assertTrue("Unable to find expected word in text\n" + text, text.contains("Long Text"));
  169. // Inline Strings
  170. assertTrue("Unable to find expected word in text\n" + text, text.contains("1st Inline String"));
  171. assertTrue("Unable to find expected word in text\n" + text, text.contains("And More"));
  172. // Formulas
  173. assertTrue("Unable to find expected word in text\n" + text, text.contains("A2"));
  174. assertTrue("Unable to find expected word in text\n" + text, text.contains("A5-A$2"));
  175. extractor.close();
  176. }
  177. /**
  178. * Simple test for text box text
  179. * @throws IOException
  180. */
  181. public void testTextBoxes() throws IOException {
  182. XSSFExcelExtractor extractor = getExtractor("WithTextBox.xlsx");
  183. try {
  184. extractor.setFormulasNotResults(true);
  185. String text = extractor.getText();
  186. assertTrue(text.indexOf("Line 1") > -1);
  187. assertTrue(text.indexOf("Line 2") > -1);
  188. assertTrue(text.indexOf("Line 3") > -1);
  189. } finally {
  190. extractor.close();
  191. }
  192. }
  193. public void testPhoneticRuns() throws Exception {
  194. XSSFExcelExtractor extractor = getExtractor("51519.xlsx");
  195. try {
  196. String text = extractor.getText();
  197. assertTrue(text.contains("\u8C4A\u7530"));
  198. //this shows up only as a phonetic run and should not appear
  199. //in the extracted text
  200. assertFalse(text.contains("\u30CB\u30DB\u30F3"));
  201. } finally {
  202. extractor.close();
  203. }
  204. }
  205. }