Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

TestXSSFBEventBasedExcelExtractor.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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.assertStartsWith;
  18. import static org.apache.poi.POITestCase.assertEndsWith;
  19. import static org.junit.Assert.assertEquals;
  20. import static org.junit.Assert.assertTrue;
  21. import org.apache.poi.xssf.XSSFTestDataSamples;
  22. import org.junit.Test;
  23. import java.io.BufferedReader;
  24. import java.nio.charset.StandardCharsets;
  25. import java.nio.file.Files;
  26. /**
  27. * Tests for {@link XSSFBEventBasedExcelExtractor}
  28. */
  29. public class TestXSSFBEventBasedExcelExtractor {
  30. protected XSSFEventBasedExcelExtractor getExtractor(String sampleName) throws Exception {
  31. return new XSSFBEventBasedExcelExtractor(XSSFTestDataSamples.
  32. openSamplePackage(sampleName));
  33. }
  34. /**
  35. * Get text out of the simple file
  36. */
  37. @Test
  38. public void testGetSimpleText() throws Exception {
  39. // a very simple file
  40. XSSFEventBasedExcelExtractor extractor = getExtractor("sample.xlsb");
  41. extractor.setIncludeCellComments(true);
  42. extractor.getText();
  43. String text = extractor.getText();
  44. assertTrue(text.length() > 0);
  45. // Check sheet names
  46. assertStartsWith(text, "Sheet1");
  47. assertEndsWith(text, "Sheet3\n");
  48. // Now without, will have text
  49. extractor.setIncludeSheetNames(false);
  50. text = extractor.getText();
  51. String CHUNK1 =
  52. "Lorem\t111\n" +
  53. "ipsum\t222\n" +
  54. "dolor\t333\n" +
  55. "sit\t444\n" +
  56. "amet\t555\n" +
  57. "consectetuer\t666\n" +
  58. "adipiscing\t777\n" +
  59. "elit\t888\n" +
  60. "Nunc\t999\n";
  61. String CHUNK2 =
  62. "The quick brown fox jumps over the lazy dog\n" +
  63. "hello, xssf hello, xssf\n" +
  64. "hello, xssf hello, xssf\n" +
  65. "hello, xssf hello, xssf\n" +
  66. "hello, xssf hello, xssf\n";
  67. assertEquals(
  68. CHUNK1 +
  69. "at\t4995\n" +
  70. CHUNK2
  71. , text);
  72. }
  73. /**
  74. * Test text extraction from text box using getShapes()
  75. *
  76. * @throws Exception
  77. */
  78. @Test
  79. public void testShapes() throws Exception {
  80. XSSFEventBasedExcelExtractor ooxmlExtractor = getExtractor("WithTextBox.xlsb");
  81. try {
  82. String text = ooxmlExtractor.getText();
  83. assertContains(text, "Line 1");
  84. assertContains(text, "Line 2");
  85. assertContains(text, "Line 3");
  86. } finally {
  87. ooxmlExtractor.close();
  88. }
  89. }
  90. @Test
  91. public void testBeta() throws Exception {
  92. XSSFEventBasedExcelExtractor extractor = getExtractor("Simple.xlsb");
  93. extractor.setIncludeCellComments(true);
  94. String text = extractor.getText();
  95. assertContains(text,
  96. "This is an example spreadsheet created with Microsoft Excel 2007 Beta 2.");
  97. }
  98. @Test
  99. public void test62815() throws Exception {
  100. //test file based on http://oss.sheetjs.com/test_files/RkNumber.xlsb
  101. XSSFEventBasedExcelExtractor extractor = getExtractor("62815.xlsb");
  102. extractor.setIncludeCellComments(true);
  103. String[] rows = extractor.getText().split("[\r\n]+");
  104. assertEquals(283, rows.length);
  105. BufferedReader reader = Files.newBufferedReader(XSSFTestDataSamples.getSampleFile("62815.xlsb.txt").toPath(),
  106. StandardCharsets.UTF_8);
  107. String line = reader.readLine();
  108. for (int i = 0; i < rows.length; i++) {
  109. assertEquals(line, rows[i]);
  110. line = reader.readLine();
  111. while (line != null && line.startsWith("#")) {
  112. line = reader.readLine();
  113. }
  114. }
  115. }
  116. }