Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

SpreadsheetHandler.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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.stress;
  16. import static org.junit.Assert.assertNotNull;
  17. import java.io.ByteArrayInputStream;
  18. import java.io.ByteArrayOutputStream;
  19. import java.io.IOException;
  20. import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
  21. import org.apache.poi.ss.extractor.EmbeddedData;
  22. import org.apache.poi.ss.extractor.EmbeddedExtractor;
  23. import org.apache.poi.ss.usermodel.Cell;
  24. import org.apache.poi.ss.usermodel.Row;
  25. import org.apache.poi.ss.usermodel.Sheet;
  26. import org.apache.poi.ss.usermodel.Workbook;
  27. import org.apache.poi.ss.usermodel.WorkbookFactory;
  28. import org.apache.poi.util.RecordFormatException;
  29. import org.apache.poi.xssf.usermodel.XSSFChartSheet;
  30. public abstract class SpreadsheetHandler extends AbstractFileHandler {
  31. public void handleWorkbook(Workbook wb) throws IOException {
  32. // try to access some of the content
  33. readContent(wb);
  34. // write out the file
  35. writeToArray(wb);
  36. // access some more content (we had cases where writing corrupts the data in memory)
  37. readContent(wb);
  38. // write once more
  39. ByteArrayOutputStream out = writeToArray(wb);
  40. // read in the written file
  41. Workbook read;
  42. try {
  43. read = WorkbookFactory.create(new ByteArrayInputStream(out.toByteArray()));
  44. } catch (InvalidFormatException e) {
  45. throw new IllegalStateException(e);
  46. }
  47. assertNotNull(read);
  48. readContent(read);
  49. extractEmbedded(read);
  50. modifyContent(read);
  51. read.close();
  52. }
  53. private ByteArrayOutputStream writeToArray(Workbook wb) throws IOException {
  54. ByteArrayOutputStream stream = new ByteArrayOutputStream();
  55. try {
  56. wb.write(stream);
  57. } finally {
  58. stream.close();
  59. }
  60. return stream;
  61. }
  62. private void readContent(Workbook wb) {
  63. for(int i = 0;i < wb.getNumberOfSheets();i++) {
  64. Sheet sheet = wb.getSheetAt(i);
  65. assertNotNull(wb.getSheet(sheet.getSheetName()));
  66. sheet.groupColumn((short) 4, (short) 5);
  67. sheet.setColumnGroupCollapsed(4, true);
  68. sheet.setColumnGroupCollapsed(4, false);
  69. // don't do this for very large sheets as it will take a long time
  70. if(sheet.getPhysicalNumberOfRows() > 1000) {
  71. continue;
  72. }
  73. for(Row row : sheet) {
  74. for(Cell cell : row) {
  75. assertNotNull(cell.toString());
  76. }
  77. }
  78. }
  79. }
  80. private void extractEmbedded(Workbook wb) throws IOException {
  81. EmbeddedExtractor ee = new EmbeddedExtractor();
  82. for (Sheet s : wb) {
  83. for (EmbeddedData ed : ee.extractAll(s)) {
  84. assertNotNull(ed.getFilename());
  85. assertNotNull(ed.getEmbeddedData());
  86. assertNotNull(ed.getShape());
  87. }
  88. }
  89. }
  90. private void modifyContent(Workbook wb) {
  91. /* a number of file fail because of various things: udf, unimplemented functions, ...
  92. we would need quite a list of excludes and the large regression tests would probably
  93. take a lot longer to run...
  94. try {
  95. // try to re-compute all formulas to find cases where parsing fails
  96. wb.getCreationHelper().createFormulaEvaluator().evaluateAll();
  97. } catch (RuntimeException e) {
  98. // only allow a specific exception which indicates that an external
  99. // reference was not found
  100. if(!e.getMessage().contains("Could not resolve external workbook name")) {
  101. throw e;
  102. }
  103. }*/
  104. for (int i=wb.getNumberOfSheets()-1; i>=0; i--) {
  105. if(wb.getSheetAt(i) instanceof XSSFChartSheet) {
  106. // clone for chart-sheets is not supported
  107. continue;
  108. }
  109. try {
  110. wb.cloneSheet(i);
  111. } catch (RecordFormatException e) {
  112. if (e.getCause() instanceof CloneNotSupportedException) {
  113. // ignore me
  114. continue;
  115. }
  116. throw e;
  117. } catch (RuntimeException e) {
  118. if ("Could not find 'internal references' EXTERNALBOOK".equals(e.getMessage()) ||
  119. "CountryRecord not found".equals(e.getMessage()) ||
  120. "Cannot add more than 65535 shapes".equals(e.getMessage()) ) {
  121. // ignore these here for now
  122. continue;
  123. }
  124. throw e;
  125. }
  126. }
  127. }
  128. }