您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

TestXSSFExcelExtractor.java 8.5KB

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