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.

TestXSSFEventBasedExcelExtractor.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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.util.regex.Matcher;
  17. import java.util.regex.Pattern;
  18. import org.apache.poi.POITextExtractor;
  19. import org.apache.poi.hssf.HSSFTestDataSamples;
  20. import org.apache.poi.hssf.extractor.ExcelExtractor;
  21. import org.apache.poi.xssf.XSSFTestDataSamples;
  22. import junit.framework.TestCase;
  23. /**
  24. * Tests for {@link XSSFEventBasedExcelExtractor}
  25. */
  26. public class TestXSSFEventBasedExcelExtractor extends TestCase {
  27. protected XSSFEventBasedExcelExtractor getExtractor(String sampleName) throws Exception {
  28. return new XSSFEventBasedExcelExtractor(XSSFTestDataSamples.
  29. openSamplePackage(sampleName));
  30. }
  31. /**
  32. * Get text out of the simple file
  33. */
  34. public void testGetSimpleText() throws Exception {
  35. // a very simple file
  36. XSSFEventBasedExcelExtractor extractor = getExtractor("sample.xlsx");
  37. extractor.getText();
  38. String text = extractor.getText();
  39. assertTrue(text.length() > 0);
  40. // Check sheet names
  41. assertTrue(text.startsWith("Sheet1"));
  42. assertTrue(text.endsWith("Sheet3\n"));
  43. // Now without, will have text
  44. extractor.setIncludeSheetNames(false);
  45. text = extractor.getText();
  46. String CHUNK1 =
  47. "Lorem\t111\n" +
  48. "ipsum\t222\n" +
  49. "dolor\t333\n" +
  50. "sit\t444\n" +
  51. "amet\t555\n" +
  52. "consectetuer\t666\n" +
  53. "adipiscing\t777\n" +
  54. "elit\t888\n" +
  55. "Nunc\t999\n";
  56. String CHUNK2 =
  57. "The quick brown fox jumps over the lazy dog\n" +
  58. "hello, xssf hello, xssf\n" +
  59. "hello, xssf hello, xssf\n" +
  60. "hello, xssf hello, xssf\n" +
  61. "hello, xssf hello, xssf\n";
  62. assertEquals(
  63. CHUNK1 +
  64. "at\t4995\n" +
  65. CHUNK2
  66. , text);
  67. // Now get formulas not their values
  68. extractor.setFormulasNotResults(true);
  69. text = extractor.getText();
  70. assertEquals(
  71. CHUNK1 +
  72. "at\tSUM(B1:B9)\n" +
  73. CHUNK2, text);
  74. // With sheet names too
  75. extractor.setIncludeSheetNames(true);
  76. text = extractor.getText();
  77. assertEquals(
  78. "Sheet1\n" +
  79. CHUNK1 +
  80. "at\tSUM(B1:B9)\n" +
  81. "rich test\n" +
  82. CHUNK2 +
  83. "Sheet3\n"
  84. , text);
  85. extractor.close();
  86. }
  87. public void testGetComplexText() throws Exception {
  88. // A fairly complex file
  89. XSSFEventBasedExcelExtractor extractor = getExtractor("AverageTaxRates.xlsx");
  90. extractor.getText();
  91. String text = extractor.getText();
  92. assertTrue(text.length() > 0);
  93. // Might not have all formatting it should do!
  94. assertTrue(text.startsWith(
  95. "Avgtxfull\n" +
  96. "(iii) AVERAGE TAX RATES ON ANNUAL"
  97. ));
  98. extractor.close();
  99. }
  100. public void testInlineStrings() throws Exception {
  101. XSSFEventBasedExcelExtractor extractor = getExtractor("InlineStrings.xlsx");
  102. extractor.setFormulasNotResults(true);
  103. String text = extractor.getText();
  104. // Numbers
  105. assertTrue("Unable to find expected word in text\n" + text, text.contains("43"));
  106. assertTrue("Unable to find expected word in text\n" + text, text.contains("22"));
  107. // Strings
  108. assertTrue("Unable to find expected word in text\n" + text, text.contains("ABCDE"));
  109. assertTrue("Unable to find expected word in text\n" + text, text.contains("Long Text"));
  110. // Inline Strings
  111. assertTrue("Unable to find expected word in text\n" + text, text.contains("1st Inline String"));
  112. assertTrue("Unable to find expected word in text\n" + text, text.contains("And More"));
  113. // Formulas
  114. assertTrue("Unable to find expected word in text\n" + text, text.contains("A2"));
  115. assertTrue("Unable to find expected word in text\n" + text, text.contains("A5-A$2"));
  116. extractor.close();
  117. }
  118. /**
  119. * Test that we return pretty much the same as
  120. * ExcelExtractor does, when we're both passed
  121. * the same file, just saved as xls and xlsx
  122. */
  123. public void testComparedToOLE2() throws Exception {
  124. // A fairly simple file - ooxml
  125. XSSFEventBasedExcelExtractor ooxmlExtractor = getExtractor("SampleSS.xlsx");
  126. ExcelExtractor ole2Extractor =
  127. new ExcelExtractor(HSSFTestDataSamples.openSampleWorkbook("SampleSS.xls"));
  128. POITextExtractor[] extractors =
  129. new POITextExtractor[] { ooxmlExtractor, ole2Extractor };
  130. for (int i = 0; i < extractors.length; i++) {
  131. POITextExtractor extractor = extractors[i];
  132. String text = extractor.getText().replaceAll("[\r\t]", "");
  133. assertTrue(text.startsWith("First Sheet\nTest spreadsheet\n2nd row2nd row 2nd column\n"));
  134. Pattern pattern = Pattern.compile(".*13(\\.0+)?\\s+Sheet3.*", Pattern.DOTALL);
  135. Matcher m = pattern.matcher(text);
  136. assertTrue(m.matches());
  137. }
  138. ole2Extractor.close();
  139. ooxmlExtractor.close();
  140. }
  141. /**
  142. * Test text extraction from text box using getShapes()
  143. * @throws Exception
  144. */
  145. public void testShapes() throws Exception{
  146. XSSFEventBasedExcelExtractor ooxmlExtractor = getExtractor("WithTextBox.xlsx");
  147. String text = ooxmlExtractor.getText();
  148. assertTrue(text.indexOf("Line 1") > -1);
  149. assertTrue(text.indexOf("Line 2") > -1);
  150. assertTrue(text.indexOf("Line 3") > -1);
  151. }
  152. /**
  153. * Test that we return the same output for unstyled numbers as the
  154. * non-event-based XSSFExcelExtractor.
  155. */
  156. public void testUnstyledNumbersComparedToNonEventBasedExtractor()
  157. throws Exception {
  158. String expectedOutput = "Sheet1\n99.99\n";
  159. XSSFExcelExtractor extractor = new XSSFExcelExtractor(
  160. XSSFTestDataSamples.openSampleWorkbook("56011.xlsx"));
  161. try {
  162. assertEquals(expectedOutput, extractor.getText().replace(",", "."));
  163. } finally {
  164. extractor.close();
  165. }
  166. XSSFEventBasedExcelExtractor fixture =
  167. new XSSFEventBasedExcelExtractor(
  168. XSSFTestDataSamples.openSamplePackage("56011.xlsx"));
  169. try {
  170. assertEquals(expectedOutput, fixture.getText().replace(",", "."));
  171. } finally {
  172. fixture.close();
  173. }
  174. }
  175. /**
  176. * Test that we return the same output headers and footers as the
  177. * non-event-based XSSFExcelExtractor.
  178. */
  179. public void testHeadersAndFootersComparedToNonEventBasedExtractor()
  180. throws Exception {
  181. String expectedOutputWithHeadersAndFooters =
  182. "Sheet1\n" +
  183. "&\"Calibri,Regular\"&K000000top left\t&\"Calibri,Regular\"&K000000top center\t&\"Calibri,Regular\"&K000000top right\n" +
  184. "abc\t123\n" +
  185. "&\"Calibri,Regular\"&K000000bottom left\t&\"Calibri,Regular\"&K000000bottom center\t&\"Calibri,Regular\"&K000000bottom right\n";
  186. String expectedOutputWithoutHeadersAndFooters =
  187. "Sheet1\n" +
  188. "abc\t123\n";
  189. XSSFExcelExtractor extractor = new XSSFExcelExtractor(
  190. XSSFTestDataSamples.openSampleWorkbook("headerFooterTest.xlsx"));
  191. assertEquals(expectedOutputWithHeadersAndFooters, extractor.getText());
  192. extractor.setIncludeHeadersFooters(false);
  193. assertEquals(expectedOutputWithoutHeadersAndFooters, extractor.getText());
  194. XSSFEventBasedExcelExtractor fixture =
  195. new XSSFEventBasedExcelExtractor(
  196. XSSFTestDataSamples.openSamplePackage("headerFooterTest.xlsx"));
  197. assertEquals(expectedOutputWithHeadersAndFooters, fixture.getText());
  198. fixture.setIncludeHeadersFooters(false);
  199. assertEquals(expectedOutputWithoutHeadersAndFooters, fixture.getText());
  200. }
  201. /**
  202. * Test that XSSFEventBasedExcelExtractor outputs comments when specified.
  203. * The output will contain two improvements over the output from
  204. * XSSFExcelExtractor in that (1) comments from empty cells will be
  205. * outputted, and (2) the author will not be outputted twice.
  206. * <p>
  207. * This test will need to be modified if these improvements are ported to
  208. * XSSFExcelExtractor.
  209. */
  210. public void testCommentsComparedToNonEventBasedExtractor()
  211. throws Exception {
  212. String expectedOutputWithoutComments =
  213. "Sheet1\n" +
  214. "\n" +
  215. "abc\n" +
  216. "\n" +
  217. "123\n" +
  218. "\n" +
  219. "\n" +
  220. "\n";
  221. String nonEventBasedExtractorOutputWithComments =
  222. "Sheet1\n" +
  223. "\n" +
  224. "abc Comment by Shaun Kalley: Shaun Kalley: Comment A2\n" +
  225. "\n" +
  226. "123 Comment by Shaun Kalley: Shaun Kalley: Comment B4\n" +
  227. "\n" +
  228. "\n" +
  229. "\n";
  230. String eventBasedExtractorOutputWithComments =
  231. "Sheet1\n" +
  232. "Comment by Shaun Kalley: Comment A1\tComment by Shaun Kalley: Comment B1\n" +
  233. "abc Comment by Shaun Kalley: Comment A2\tComment by Shaun Kalley: Comment B2\n" +
  234. "Comment by Shaun Kalley: Comment A3\tComment by Shaun Kalley: Comment B3\n" +
  235. "Comment by Shaun Kalley: Comment A4\t123 Comment by Shaun Kalley: Comment B4\n" +
  236. "Comment by Shaun Kalley: Comment A5\tComment by Shaun Kalley: Comment B5\n" +
  237. "Comment by Shaun Kalley: Comment A7\tComment by Shaun Kalley: Comment B7\n" +
  238. "Comment by Shaun Kalley: Comment A8\tComment by Shaun Kalley: Comment B8\n";
  239. XSSFExcelExtractor extractor = new XSSFExcelExtractor(
  240. XSSFTestDataSamples.openSampleWorkbook("commentTest.xlsx"));
  241. try {
  242. assertEquals(expectedOutputWithoutComments, extractor.getText());
  243. extractor.setIncludeCellComments(true);
  244. assertEquals(nonEventBasedExtractorOutputWithComments, extractor.getText());
  245. } finally {
  246. extractor.close();
  247. }
  248. XSSFEventBasedExcelExtractor fixture =
  249. new XSSFEventBasedExcelExtractor(
  250. XSSFTestDataSamples.openSamplePackage("commentTest.xlsx"));
  251. try {
  252. assertEquals(expectedOutputWithoutComments, fixture.getText());
  253. fixture.setIncludeCellComments(true);
  254. assertEquals(eventBasedExtractorOutputWithComments, fixture.getText());
  255. } finally {
  256. fixture.close();
  257. }
  258. }
  259. }