diff options
3 files changed, 40 insertions, 7 deletions
diff --git a/src/java/org/apache/poi/hssf/model/Sheet.java b/src/java/org/apache/poi/hssf/model/Sheet.java index 746c9348ad..140b5c1715 100644 --- a/src/java/org/apache/poi/hssf/model/Sheet.java +++ b/src/java/org/apache/poi/hssf/model/Sheet.java @@ -783,7 +783,7 @@ public class Sheet implements Model int dbCellOffset = 0; for (int block=0;block<blockCount;block++) { rowBlockOffset += rows.getRowBlockSize(block); - cellBlockOffset += cells.getRowCellBlockSize(rows.getStartRowNumberForBlock(block), + cellBlockOffset += null == cells ? 0 : cells.getRowCellBlockSize(rows.getStartRowNumberForBlock(block), rows.getEndRowNumberForBlock(block)); //Note: The offsets are relative to the Workbook BOF. Assume that this is //0 for now..... diff --git a/src/java/org/apache/poi/hssf/record/aggregates/RowRecordsAggregate.java b/src/java/org/apache/poi/hssf/record/aggregates/RowRecordsAggregate.java index 49898b181c..abef0159c5 100644 --- a/src/java/org/apache/poi/hssf/record/aggregates/RowRecordsAggregate.java +++ b/src/java/org/apache/poi/hssf/record/aggregates/RowRecordsAggregate.java @@ -213,7 +213,7 @@ public class RowRecordsAggregate //Note: Cell references start from the second row... int cellRefOffset = (rowBlockSize-20); for (int row=startRowNumber;row<=endRowNumber;row++) { - if (cells.rowHasCells(row)) { + if (null != cells && cells.rowHasCells(row)) { final int rowCellSize = cells.serializeCellRow(row, pos, data); pos += rowCellSize; //Add the offset to the first cell for the row into the DBCellRecord. diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java index 5fc578ad46..2a8561ebd6 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java @@ -19,9 +19,7 @@ package org.apache.poi.hssf.usermodel; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; +import java.io.*; import junit.framework.TestCase; @@ -481,8 +479,43 @@ public class TestHSSFSheet HSSFCell c = r.createCell((short)0); assertEquals("style should match", style.getIndex(), c.getCellStyle().getIndex()); } - - + + + /** + * + */ + public void testAddEmptyRow() throws Exception { + //try to add 5 empty rows to a new sheet + HSSFWorkbook workbook = new HSSFWorkbook(); + HSSFSheet sheet = workbook.createSheet(); + for (int i = 0; i < 5; i++) sheet.createRow(i); + + ByteArrayOutputStream out = new ByteArrayOutputStream(); + workbook.write(out); + out.close(); + + workbook = new HSSFWorkbook(new ByteArrayInputStream(out.toByteArray())); + assertTrue("No Exceptions while reading file", true); + + //try adding empty rows in an existing worksheet + String cwd = System.getProperty("HSSF.testdata.path"); + FileInputStream in = new FileInputStream(new File(cwd, "Simple.xls")); + workbook = new HSSFWorkbook(in); + in.close(); + assertTrue("No Exceptions while reading file", true); + + sheet = workbook.getSheetAt(0); + for (int i = 3; i < 10; i++) sheet.createRow(i); + + out = new ByteArrayOutputStream(); + workbook.write(out); + out.close(); + + workbook = new HSSFWorkbook(new ByteArrayInputStream(out.toByteArray())); + assertTrue("No Exceptions while reading file", true); + + } + public static void main(java.lang.String[] args) { junit.textui.TestRunner.run(TestHSSFSheet.class); } |