/* ==================================================================== Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ==================================================================== */ package org.apache.poi.ss.usermodel; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import java.awt.font.FontRenderContext; import java.awt.font.TextAttribute; import java.awt.font.TextLayout; import java.io.IOException; import java.text.AttributedString; import java.util.HashMap; import java.util.Map; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.util.PaneInformation; import org.apache.poi.ss.ITestDataProvider; import org.apache.poi.ss.SpreadsheetVersion; import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.ss.util.SheetUtil; import org.junit.Assume; import org.junit.Ignore; import org.junit.Test; /** * A base class for bugzilla issues that can be described in terms of common ss interfaces. * * @author Yegor Kozlov */ public abstract class BaseTestBugzillaIssues { private final ITestDataProvider _testDataProvider; protected BaseTestBugzillaIssues(ITestDataProvider testDataProvider) { _testDataProvider = testDataProvider; } public static void assertAlmostEquals(double expected, double actual, float factor) { double diff = Math.abs(expected - actual); double fuzz = expected * factor; if (diff > fuzz) fail(actual + " not within " + fuzz + " of " + expected); } /** * Test writing a hyperlink * Open resulting sheet in Excel and check that A1 contains a hyperlink * * Also tests bug 15353 (problems with hyperlinks to Google) */ @Test public final void bug23094() throws IOException { Workbook wb1 = _testDataProvider.createWorkbook(); Sheet s = wb1.createSheet(); Row r = s.createRow(0); r.createCell(0).setCellFormula("HYPERLINK(\"http://jakarta.apache.org\",\"Jakarta\")"); r.createCell(1).setCellFormula("HYPERLINK(\"http://google.com\",\"Google\")"); Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1); wb1.close(); r = wb2.getSheetAt(0).getRow(0); Cell cell_0 = r.getCell(0); assertEquals("HYPERLINK(\"http://jakarta.apache.org\",\"Jakarta\")", cell_0.getCellFormula()); Cell cell_1 = r.getCell(1); assertEquals("HYPERLINK(\"http://google.com\",\"Google\")", cell_1.getCellFormula()); wb2.close(); } /** * test writing a file with large number of unique strings, * open resulting file in Excel to check results! * @param num the number of strings to generate */ public final void bug15375(int num) throws IOException { Workbook wb1 = _testDataProvider.createWorkbook(); Sheet sheet = wb1.createSheet(); CreationHelper factory = wb1.getCreationHelper(); String tmp1 = null; String tmp2 = null; String tmp3 = null; for (int i = 0; i < num; i++) { tmp1 = "Test1" + i; tmp2 = "Test2" + i; tmp3 = "Test3" + i; Row row = sheet.createRow(i); Cell cell = row.createCell(0); cell.setCellValue(factory.createRichTextString(tmp1)); cell = row.createCell(1); cell.setCellValue(factory.createRichTextString(tmp2)); cell = row.createCell(2); cell.setCellValue(factory.createRichTextString(tmp3)); } Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1); wb1.close(); sheet = wb2.getSheetAt(0); for (int i = 0; i < num; i++) { tmp1 = "Test1" + i; tmp2 = "Test2" + i; tmp3 = "Test3" + i; Row row = sheet.getRow(i); assertEquals(tmp1, row.getCell(0).getStringCellValue()); assertEquals(tmp2, row.getCell(1).getStringCellValue()); assertEquals(tmp3, row.getCell(2).getStringCellValue()); } wb2.close(); } /** * Merged regions were being removed from the parent in cloned sheets */ @Test public void bug22720() throws IOException { Workbook wb = _testDataProvider.createWorkbook(); wb.createSheet("TEST"); Sheet template = wb.getSheetAt(0); template.addMergedRegion(new CellRangeAddress(0, 1, 0, 2)); template.addMergedRegion(new CellRangeAddress(2, 3, 0, 2)); Sheet clone = wb.cloneSheet(0); int originalMerged = template.getNumMergedRegions(); assertEquals("2 merged regions", 2, originalMerged); //remove merged regions from clone for (int i=template.getNumMergedRegions()-1; i>=0; i--) { clone.removeMergedRegion(i); } assertEquals("Original Sheet's Merged Regions were removed", originalMerged, template.getNumMergedRegions()); //check if template's merged regions are OK if (template.getNumMergedRegions()>0) { // fetch the first merged region...EXCEPTION OCCURS HERE template.getMergedRegion(0); } //make sure we dont exception wb.close(); } @Test public final void bug28031() throws IOException { Workbook wb1 = _testDataProvider.createWorkbook(); Sheet sheet = wb1.createSheet(); wb1.setSheetName(0, "Sheet1"); Row row = sheet.createRow(0); Cell cell = row.createCell(0); String formulaText = "IF(ROUND(A2*B2*C2,2)>ROUND(B2*D2,2),ROUND(A2*B2*C2,2),ROUND(B2*D2,2))"; cell.setCellFormula(formulaText); assertEquals(formulaText, cell.getCellFormula()); Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1); wb1.close(); cell = wb2.getSheetAt(0).getRow(0).getCell(0); assertEquals("IF(ROUND(A2*B2*C2,2)>ROUND(B2*D2,2),ROUND(A2*B2*C2,2),ROUND(B2*D2,2))", cell.getCellFormula()); wb2.close(); } /** * Bug 21334: "File error: data may have been lost" with a file * that contains macros and this formula: * {=SUM(IF(FREQUENCY(IF(LEN(V4:V220)>0,MATCH(V4:V220,V4:V220,0),""),IF(LEN(V4:V220)>0,MATCH(V4:V220,V4:V220,0),""))>0,1))} */ @Test public final void bug21334() throws IOException { Workbook wb1 = _testDataProvider.createWorkbook(); Sheet sh = wb1.createSheet(); Cell cell = sh.createRow(0).createCell(0); String formula = "SUM(IF(FREQUENCY(IF(LEN(V4:V220)>0,MATCH(V4:V220,V4:V220,0),\"\"),IF(LEN(V4:V220)>0,MATCH(V4:V220,V4:V220,0),\"\"))>0,1))"; cell.setCellFormula(formula); Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1); wb1.close(); Cell cell_sv = wb2.getSheetAt(0).getRow(0).getCell(0); assertEquals(formula, cell_sv.getCellFormula()); wb2.close(); } /** another test for the number of unique strings issue *test opening the resulting file in Excel*/ @Test public final void bug22568() throws IOException { int r=2000;int c=3; Workbook wb1 = _testDataProvider.createWorkbook(); Sheet sheet = wb1.createSheet("ExcelTest") ; int col_cnt=0, rw_cnt=0 ; col_cnt = c; rw_cnt = r; Row rw ; rw = sheet.createRow(0) ; //Header row for(int j=0; j 0) fmla.append(','); fmla.append("A1"); } fmla.append(")"); return fmla.toString(); } @Test public final void bug50681_testAutoSize() throws IOException { Workbook wb = _testDataProvider.createWorkbook(); BaseTestSheetAutosizeColumn.fixFonts(wb); Sheet sheet = wb.createSheet("Sheet1"); Row row = sheet.createRow(0); Cell cell0 = row.createCell(0); String longValue = "www.hostname.com, www.hostname.com, " + "www.hostname.com, www.hostname.com, www.hostname.com, " + "www.hostname.com, www.hostname.com, www.hostname.com, " + "www.hostname.com, www.hostname.com, www.hostname.com, " + "www.hostname.com, www.hostname.com, www.hostname.com, " + "www.hostname.com, www.hostname.com, www.hostname.com, www.hostname.com"; cell0.setCellValue(longValue); // autoSize will fail if required fonts are not installed, skip this test then Font font = wb.getFontAt(cell0.getCellStyle().getFontIndex()); Assume.assumeTrue("Cannot verify autoSizeColumn() because the necessary Fonts are not installed on this machine: " + font, SheetUtil.canComputeColumnWidht(font)); assertEquals("Expecting no indentation in this test", 0, cell0.getCellStyle().getIndention()); assertEquals("Expecting no rotation in this test", 0, cell0.getCellStyle().getRotation()); // check computing size up to a large size // StringBuilder b = new StringBuilder(); // for(int i = 0;i < longValue.length()*5;i++) { // b.append("w"); // assertTrue("Had zero length starting at length " + i, computeCellWidthFixed(font, b.toString()) > 0); // } double widthManual = computeCellWidthManually(cell0, font); double widthBeforeCell = SheetUtil.getCellWidth(cell0, 8, null, false); double widthBeforeCol = SheetUtil.getColumnWidth(sheet, 0, false); String info = widthManual + "/" + widthBeforeCell + "/" + widthBeforeCol + "/" + SheetUtil.canComputeColumnWidht(font) + "/" + computeCellWidthFixed(font, "1") + "/" + computeCellWidthFixed(font, "w") + "/" + computeCellWidthFixed(font, "1w") + "/" + computeCellWidthFixed(font, "0000") + "/" + computeCellWidthFixed(font, longValue); assertTrue("Expected to have cell width > 0 when computing manually, but had " + info, widthManual > 0); assertTrue("Expected to have cell width > 0 BEFORE auto-size, but had " + info, widthBeforeCell > 0); assertTrue("Expected to have column width > 0 BEFORE auto-size, but had " + info, widthBeforeCol > 0); sheet.autoSizeColumn(0); double width = SheetUtil.getColumnWidth(sheet, 0, false); assertTrue("Expected to have column width > 0 AFTER auto-size, but had " + width, width > 0); width = SheetUtil.getCellWidth(cell0, 8, null, false); assertTrue("Expected to have cell width > 0 AFTER auto-size, but had " + width, width > 0); assertEquals(255*256, sheet.getColumnWidth(0)); // maximum column width is 255 characters sheet.setColumnWidth(0, sheet.getColumnWidth(0)); // Bug 50681 reports exception at this point wb.close(); } private double computeCellWidthManually(Cell cell0, Font font) { final FontRenderContext fontRenderContext = new FontRenderContext(null, true, true); RichTextString rt = cell0.getRichStringCellValue(); String[] lines = rt.getString().split("\\n"); assertEquals(1, lines.length); String txt = lines[0] + "0"; AttributedString str = new AttributedString(txt); copyAttributes(font, str, 0, txt.length()); if (rt.numFormattingRuns() > 0) { // TODO: support rich text fragments } TextLayout layout = new TextLayout(str.getIterator(), fontRenderContext); return ((layout.getBounds().getWidth() / 1) / 8); } private double computeCellWidthFixed(Font font, String txt) { final FontRenderContext fontRenderContext = new FontRenderContext(null, true, true); AttributedString str = new AttributedString(txt); copyAttributes(font, str, 0, txt.length()); TextLayout layout = new TextLayout(str.getIterator(), fontRenderContext); return layout.getBounds().getWidth(); } private static void copyAttributes(Font font, AttributedString str, int startIdx, int endIdx) { str.addAttribute(TextAttribute.FAMILY, font.getFontName(), startIdx, endIdx); str.addAttribute(TextAttribute.SIZE, (float)font.getFontHeightInPoints()); if (font.getBoldweight() == Font.BOLDWEIGHT_BOLD) str.addAttribute(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD, startIdx, endIdx); if (font.getItalic() ) str.addAttribute(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE, startIdx, endIdx); if (font.getUnderline() == Font.U_SINGLE ) str.addAttribute(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON, startIdx, endIdx); } /** * CreateFreezePane column/row order check */ @Test public void bug49381() throws IOException { Workbook wb = _testDataProvider.createWorkbook(); int colSplit = 1; int rowSplit = 2; int leftmostColumn = 3; int topRow = 4; Sheet s = wb.createSheet(); // Populate for(int rn=0; rn<= topRow; rn++) { Row r = s.createRow(rn); for(int cn=0; cn evaluators = new HashMap(); evaluators.put(refLocal, wb3.getCreationHelper().createFormulaEvaluator()); evaluators.put(refHttp, wb3.getCreationHelper().createFormulaEvaluator()); FormulaEvaluator evaluator = wb2.getCreationHelper().createFormulaEvaluator(); evaluator.setupReferencedWorkbooks(evaluators); // Try to evaluate, with the other file evaluator.evaluateFormulaCell(c1); evaluator.evaluateFormulaCell(c2); assertEquals(otherCellText, c1.getStringCellValue()); assertEquals(otherCellText, c2.getStringCellValue()); wb3.close(); wb2.close(); } @Test public void test56574OverwriteExistingRow() throws IOException { Workbook wb = _testDataProvider.createWorkbook(); Sheet sheet = wb.createSheet(); { // create the Formula-Cell Row row = sheet.createRow(0); Cell cell = row.createCell(0); cell.setCellFormula("A2"); } { // check that it is there now Row row = sheet.getRow(0); /* CTCell[] cArray = ((XSSFRow)row).getCTRow().getCArray(); assertEquals(1, cArray.length);*/ Cell cell = row.getCell(0); assertEquals(Cell.CELL_TYPE_FORMULA, cell.getCellType()); } { // overwrite the row Row row = sheet.createRow(0); assertNotNull(row); } { // creating a row in place of another should remove the existing data, // check that the cell is gone now Row row = sheet.getRow(0); /*CTCell[] cArray = ((XSSFRow)row).getCTRow().getCArray(); assertEquals(0, cArray.length);*/ Cell cell = row.getCell(0); assertNull(cell); } // the calculation chain in XSSF is empty in a newly created workbook, so we cannot check if it is correctly updated /*assertNull(((XSSFWorkbook)wb).getCalculationChain()); assertNotNull(((XSSFWorkbook)wb).getCalculationChain().getCTCalcChain()); assertNotNull(((XSSFWorkbook)wb).getCalculationChain().getCTCalcChain().getCArray()); assertEquals(0, ((XSSFWorkbook)wb).getCalculationChain().getCTCalcChain().getCArray().length);*/ wb.close(); } /** * With HSSF, if you create a font, don't change it, and * create a 2nd, you really do get two fonts that you * can alter as and when you want. * With XSSF, that wasn't the case, but this verfies * that it now is again */ @Test public void bug48718() throws IOException { Workbook wb = _testDataProvider.createWorkbook(); int startingFonts = wb instanceof HSSFWorkbook ? 4 : 1; assertEquals(startingFonts, wb.getNumberOfFonts()); // Get a font, and slightly change it Font a = wb.createFont(); assertEquals(startingFonts+1, wb.getNumberOfFonts()); a.setFontHeightInPoints((short)23); assertEquals(startingFonts+1, wb.getNumberOfFonts()); // Get two more, unchanged /*Font b =*/ wb.createFont(); assertEquals(startingFonts+2, wb.getNumberOfFonts()); /*Font c =*/ wb.createFont(); assertEquals(startingFonts+3, wb.getNumberOfFonts()); wb.close(); } @Test public void bug57430() throws IOException { Workbook wb = _testDataProvider.createWorkbook(); wb.createSheet("Sheet1"); Name name1 = wb.createName(); name1.setNameName("FMLA"); name1.setRefersToFormula("Sheet1!$B$3"); wb.close(); } @Test public void bug56981() throws IOException { Workbook wb = _testDataProvider.createWorkbook(); CellStyle vertTop = wb.createCellStyle(); vertTop.setVerticalAlignment(CellStyle.VERTICAL_TOP); CellStyle vertBottom = wb.createCellStyle(); vertBottom.setVerticalAlignment(CellStyle.VERTICAL_BOTTOM); Sheet sheet = wb.createSheet("Sheet 1"); Row row = sheet.createRow(0); Cell top = row.createCell(0); Cell bottom = row.createCell(1); top.setCellValue("Top"); top.setCellStyle(vertTop); // comment this out to get all bottom-aligned // cells bottom.setCellValue("Bottom"); bottom.setCellStyle(vertBottom); row.setHeightInPoints(85.75f); // make it obvious /*FileOutputStream out = new FileOutputStream("c:\\temp\\56981.xlsx"); try { wb.write(out); } finally { out.close(); }*/ wb.close(); } @Test public void test57973() throws IOException { Workbook wb = _testDataProvider.createWorkbook(); CreationHelper factory = wb.getCreationHelper(); Sheet sheet = wb.createSheet(); Drawing drawing = sheet.createDrawingPatriarch(); ClientAnchor anchor = factory.createClientAnchor(); Cell cell0 = sheet.createRow(0).createCell(0); cell0.setCellValue("Cell0"); Comment comment0 = drawing.createCellComment(anchor); RichTextString str0 = factory.createRichTextString("Hello, World1!"); comment0.setString(str0); comment0.setAuthor("Apache POI"); cell0.setCellComment(comment0); anchor = factory.createClientAnchor(); anchor.setCol1(1); anchor.setCol2(1); anchor.setRow1(1); anchor.setRow2(1); Cell cell1 = sheet.createRow(3).createCell(5); cell1.setCellValue("F4"); Comment comment1 = drawing.createCellComment(anchor); RichTextString str1 = factory.createRichTextString("Hello, World2!"); comment1.setString(str1); comment1.setAuthor("Apache POI"); cell1.setCellComment(comment1); Cell cell2 = sheet.createRow(2).createCell(2); cell2.setCellValue("C3"); anchor = factory.createClientAnchor(); anchor.setCol1(2); anchor.setCol2(2); anchor.setRow1(2); anchor.setRow2(2); Comment comment2 = drawing.createCellComment(anchor); RichTextString str2 = factory.createRichTextString("XSSF can set cell comments"); //apply custom font to the text in the comment Font font = wb.createFont(); font.setFontName("Arial"); font.setFontHeightInPoints((short)14); font.setBoldweight(Font.BOLDWEIGHT_BOLD); font.setColor(IndexedColors.RED.getIndex()); str2.applyFont(font); comment2.setString(str2); comment2.setAuthor("Apache POI"); comment2.setColumn(2); comment2.setRow(2); wb.close(); } /** * Ensures that XSSF and HSSF agree with each other, * and with the docs on when fetching the wrong * kind of value from a Formula cell */ @Test public void bug47815() throws IOException { Workbook wb = _testDataProvider.createWorkbook(); Sheet s = wb.createSheet(); Row r = s.createRow(0); // Setup Cell cn = r.createCell(0, Cell.CELL_TYPE_NUMERIC); cn.setCellValue(1.2); Cell cs = r.createCell(1, Cell.CELL_TYPE_STRING); cs.setCellValue("Testing"); Cell cfn = r.createCell(2, Cell.CELL_TYPE_FORMULA); cfn.setCellFormula("A1"); Cell cfs = r.createCell(3, Cell.CELL_TYPE_FORMULA); cfs.setCellFormula("B1"); FormulaEvaluator fe = wb.getCreationHelper().createFormulaEvaluator(); assertEquals(Cell.CELL_TYPE_NUMERIC, fe.evaluate(cfn).getCellType()); assertEquals(Cell.CELL_TYPE_STRING, fe.evaluate(cfs).getCellType()); fe.evaluateFormulaCell(cfn); fe.evaluateFormulaCell(cfs); // Now test assertEquals(Cell.CELL_TYPE_NUMERIC, cn.getCellType()); assertEquals(Cell.CELL_TYPE_STRING, cs.getCellType()); assertEquals(Cell.CELL_TYPE_FORMULA, cfn.getCellType()); assertEquals(Cell.CELL_TYPE_NUMERIC, cfn.getCachedFormulaResultType()); assertEquals(Cell.CELL_TYPE_FORMULA, cfs.getCellType()); assertEquals(Cell.CELL_TYPE_STRING, cfs.getCachedFormulaResultType()); // Different ways of retrieving assertEquals(1.2, cn.getNumericCellValue(), 0); try { cn.getRichStringCellValue(); fail(); } catch(IllegalStateException e) {} assertEquals("Testing", cs.getStringCellValue()); try { cs.getNumericCellValue(); fail(); } catch(IllegalStateException e) {} assertEquals(1.2, cfn.getNumericCellValue(), 0); try { cfn.getRichStringCellValue(); fail(); } catch(IllegalStateException e) {} assertEquals("Testing", cfs.getStringCellValue()); try { cfs.getNumericCellValue(); fail(); } catch(IllegalStateException e) {} wb.close(); } @Test public void test58113() throws IOException { Workbook wb = _testDataProvider.createWorkbook(); Sheet sheet = wb.createSheet( "Test" ); Row row = sheet.createRow(0); Cell cell = row.createCell(0); // verify that null-values can be set, this was possible up to 3.11, but broken in 3.12 cell.setCellValue((String)null); String value = cell.getStringCellValue(); assertTrue("HSSF will currently return empty string, XSSF/SXSSF will return null, but had: " + value, value == null || value.length() == 0); cell = row.createCell(1); // also verify that setting formulas to null works cell.setCellType(Cell.CELL_TYPE_FORMULA); cell.setCellValue((String)null); wb.getCreationHelper().createFormulaEvaluator().evaluateAll(); value = cell.getStringCellValue(); assertTrue("HSSF will currently return empty string, XSSF/SXSSF will return null, but had: " + value, value == null || value.length() == 0); // set some value cell.setCellType(Cell.CELL_TYPE_STRING); cell.setCellValue("somevalue"); value = cell.getStringCellValue(); assertTrue("can set value afterwards: " + value, value.equals("somevalue")); // verify that the null-value is actually set even if there was some value in the cell before cell.setCellValue((String)null); value = cell.getStringCellValue(); assertTrue("HSSF will currently return empty string, XSSF/SXSSF will return null, but had: " + value, value == null || value.length() == 0); wb.close(); } /** * Formulas with Nested Ifs, or If with text functions like * Mid in it, can give #VALUE in Excel */ @Test public void bug55747() throws IOException { Workbook wb1 = _testDataProvider.createWorkbook(); FormulaEvaluator ev = wb1.getCreationHelper().createFormulaEvaluator(); Sheet s = wb1.createSheet(); Row row = s.createRow(0); row.createCell(0).setCellValue("abc"); row.createCell(1).setCellValue(""); row.createCell(2).setCellValue(3); Cell cell = row.createCell(5); cell.setCellFormula("IF(A1<>\"\",MID(A1,1,2),\" \")"); ev.evaluateAll(); assertEquals("ab", cell.getStringCellValue()); cell = row.createCell(6); cell.setCellFormula("IF(B1<>\"\",MID(A1,1,2),\"empty\")"); ev.evaluateAll(); assertEquals("empty", cell.getStringCellValue()); cell = row.createCell(7); cell.setCellFormula("IF(A1<>\"\",IF(C1<>\"\",MID(A1,1,2),\"c1\"),\"c2\")"); ev.evaluateAll(); assertEquals("ab", cell.getStringCellValue()); // Write it back out, and re-read Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1); wb1.close(); ev = wb2.getCreationHelper().createFormulaEvaluator(); s = wb2.getSheetAt(0); row = s.getRow(0); // Check read ok, and re-evaluate fine cell = row.getCell(5); assertEquals("ab", cell.getStringCellValue()); ev.evaluateFormulaCell(cell); assertEquals("ab", cell.getStringCellValue()); cell = row.getCell(6); assertEquals("empty", cell.getStringCellValue()); ev.evaluateFormulaCell(cell); assertEquals("empty", cell.getStringCellValue()); cell = row.getCell(7); assertEquals("ab", cell.getStringCellValue()); ev.evaluateFormulaCell(cell); assertEquals("ab", cell.getStringCellValue()); wb2.close(); } @Test public void bug58260() throws IOException { //Create workbook and worksheet Workbook wb = _testDataProvider.createWorkbook(); //Sheet worksheet = wb.createSheet("sample"); //Loop through and add all values from array list // use a fixed seed to always produce the same file which makes comparing stuff easier //Random rnd = new Random(4352345); int maxStyles = (wb instanceof HSSFWorkbook) ? 4009 : 64000; for(int i = 0;i < maxStyles;i++) { //Create new row //Row row = worksheet.createRow(i); //Create cell style CellStyle style = null; try { style = wb.createCellStyle(); } catch (IllegalStateException e) { fail("Failed for row " + i); } style.setAlignment(CellStyle.ALIGN_RIGHT); if((wb instanceof HSSFWorkbook)) { // there are some predefined styles assertEquals(i+21, style.getIndex()); } else { // getIndex() returns short, which is not sufficient for > 32767 // we should really change the API to be "int" for getIndex() but // that needs API changes assertEquals(i+1, style.getIndex() & 0xffff); } //Create cell //Cell cell = row.createCell(0); //Set cell style //cell.setCellStyle(style); //Set cell value //cell.setCellValue("r" + rnd.nextInt()); } // should fail if we try to add more now try { wb.createCellStyle(); fail("Should fail after " + maxStyles + " styles, but did not fail"); } catch (IllegalStateException e) { // expected here } /*//add column width for appearance sake worksheet.setColumnWidth(0, 5000); // Write the output to a file System.out.println("Writing..."); OutputStream fileOut = new FileOutputStream("C:\\temp\\58260." + _testDataProvider.getStandardFileNameExtension()); // the resulting file can be compressed nicely, so we need to disable the zip bomb detection here double before = ZipSecureFile.getMinInflateRatio(); try { ZipSecureFile.setMinInflateRatio(0.00001); wb.write(fileOut); } finally { fileOut.close(); ZipSecureFile.setMinInflateRatio(before); }*/ wb.close(); } @Test public void test50319() throws IOException { Workbook wb = new HSSFWorkbook(); Sheet sheet = wb.createSheet("Test"); sheet.createRow(0); sheet.groupRow(0, 0); sheet.setRowGroupCollapsed(0, true); sheet.groupColumn(0, 0); sheet.setColumnGroupCollapsed(0, true); wb.close(); } }