Allow specifying SpreadsheetVersion when instantiating blank HSSFWorkbook

This commit is contained in:
Travis Burtrum 2017-05-30 16:14:35 -04:00
parent 8ab388eb78
commit 4727ab718d
10 changed files with 76 additions and 40 deletions

View File

@ -70,6 +70,7 @@ import org.apache.poi.hssf.record.aggregates.RecordAggregate.PositionTrackingVis
import org.apache.poi.hssf.record.aggregates.RecordAggregate.RecordVisitor; import org.apache.poi.hssf.record.aggregates.RecordAggregate.RecordVisitor;
import org.apache.poi.hssf.record.aggregates.RowRecordsAggregate; import org.apache.poi.hssf.record.aggregates.RowRecordsAggregate;
import org.apache.poi.hssf.record.aggregates.WorksheetProtectionBlock; import org.apache.poi.hssf.record.aggregates.WorksheetProtectionBlock;
import org.apache.poi.ss.SpreadsheetVersion;
import org.apache.poi.ss.formula.FormulaShifter; import org.apache.poi.ss.formula.FormulaShifter;
import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.PaneInformation; import org.apache.poi.ss.util.PaneInformation;
@ -444,9 +445,12 @@ public final class InternalSheet {
* @return Sheet object with all values set to defaults * @return Sheet object with all values set to defaults
*/ */
public static InternalSheet createSheet() { public static InternalSheet createSheet() {
return new InternalSheet(); return new InternalSheet(SpreadsheetVersion.EXCEL97);
} }
private InternalSheet() { public static InternalSheet createSheet(final SpreadsheetVersion spreadsheetVersion) {
return new InternalSheet(spreadsheetVersion);
}
private InternalSheet(final SpreadsheetVersion spreadsheetVersion) {
_mergedCellsTable = new MergedCellsTable(); _mergedCellsTable = new MergedCellsTable();
List<RecordBase> records = new ArrayList<RecordBase>(32); List<RecordBase> records = new ArrayList<RecordBase>(32);
@ -487,7 +491,7 @@ public final class InternalSheet {
_columnInfos = columns; _columnInfos = columns;
_dimensions = createDimensions(); _dimensions = createDimensions();
records.add(_dimensions); records.add(_dimensions);
_rowsAggregate = new RowRecordsAggregate(); _rowsAggregate = new RowRecordsAggregate(spreadsheetVersion);
records.add(_rowsAggregate); records.add(_rowsAggregate);
// 'Sheet View Settings' // 'Sheet View Settings'
records.add(windowTwo = createWindowTwo()); records.add(windowTwo = createWindowTwo());

View File

@ -40,6 +40,7 @@ public final class RowRecordsAggregate extends RecordAggregate {
private final ValueRecordsAggregate _valuesAgg; private final ValueRecordsAggregate _valuesAgg;
private final List<Record> _unknownRecords; private final List<Record> _unknownRecords;
private final SharedValueManager _sharedValueManager; private final SharedValueManager _sharedValueManager;
private final SpreadsheetVersion _spreadsheetVersion;
// Cache values to speed up performance of // Cache values to speed up performance of
// getStartRowNumberForBlock / getEndRowNumberForBlock, see Bugzilla 47405 // getStartRowNumberForBlock / getEndRowNumberForBlock, see Bugzilla 47405
@ -47,9 +48,15 @@ public final class RowRecordsAggregate extends RecordAggregate {
/** Creates a new instance of ValueRecordsAggregate */ /** Creates a new instance of ValueRecordsAggregate */
public RowRecordsAggregate() { public RowRecordsAggregate() {
this(SharedValueManager.createEmpty()); this(SharedValueManager.createEmpty(), SpreadsheetVersion.EXCEL97);
} }
private RowRecordsAggregate(SharedValueManager svm) { public RowRecordsAggregate(final SpreadsheetVersion spreadsheetVersion) {
this(SharedValueManager.createEmpty(), spreadsheetVersion);
if (spreadsheetVersion == null) {
throw new IllegalArgumentException("SpreadsheetVersion must be provided.");
}
}
private RowRecordsAggregate(SharedValueManager svm, final SpreadsheetVersion spreadsheetVersion) {
if (svm == null) { if (svm == null) {
throw new IllegalArgumentException("SharedValueManager must be provided."); throw new IllegalArgumentException("SharedValueManager must be provided.");
} }
@ -57,6 +64,7 @@ public final class RowRecordsAggregate extends RecordAggregate {
_valuesAgg = new ValueRecordsAggregate(); _valuesAgg = new ValueRecordsAggregate();
_unknownRecords = new ArrayList<Record>(); _unknownRecords = new ArrayList<Record>();
_sharedValueManager = svm; _sharedValueManager = svm;
_spreadsheetVersion = spreadsheetVersion;
} }
/** /**
@ -66,7 +74,7 @@ public final class RowRecordsAggregate extends RecordAggregate {
* and table records of the current sheet). Never <code>null</code>. * and table records of the current sheet). Never <code>null</code>.
*/ */
public RowRecordsAggregate(RecordStream rs, SharedValueManager svm) { public RowRecordsAggregate(RecordStream rs, SharedValueManager svm) {
this(svm); this(svm, SpreadsheetVersion.EXCEL97);
while(rs.hasNext()) { while(rs.hasNext()) {
Record rec = rs.getNext(); Record rec = rs.getNext();
switch (rec.getSid()) { switch (rec.getSid()) {
@ -143,7 +151,7 @@ public final class RowRecordsAggregate extends RecordAggregate {
} }
public RowRecord getRow(int rowIndex) { public RowRecord getRow(int rowIndex) {
int maxrow = SpreadsheetVersion.EXCEL97.getLastRowIndex(); int maxrow = _spreadsheetVersion.getLastRowIndex();
if (rowIndex < 0 || rowIndex > maxrow) { if (rowIndex < 0 || rowIndex > maxrow) {
throw new IllegalArgumentException("The row number must be between 0 and " + maxrow + ", but had: " + rowIndex); throw new IllegalArgumentException("The row number must be between 0 and " + maxrow + ", but had: " + rowIndex);
} }

View File

@ -70,11 +70,6 @@ import org.apache.poi.util.LocaleUtil;
*/ */
public class HSSFCell implements Cell { public class HSSFCell implements Cell {
private static final String FILE_FORMAT_NAME = "BIFF8"; private static final String FILE_FORMAT_NAME = "BIFF8";
/**
* The maximum number of columns in BIFF8
*/
public static final int LAST_COLUMN_NUMBER = SpreadsheetVersion.EXCEL97.getLastColumnIndex(); // 2^8 - 1
private static final String LAST_COLUMN_NAME = SpreadsheetVersion.EXCEL97.getLastColumnName();
public final static short ENCODING_UNCHANGED = -1; public final static short ENCODING_UNCHANGED = -1;
public final static short ENCODING_COMPRESSED_UNICODE = 0; public final static short ENCODING_COMPRESSED_UNICODE = 0;
@ -105,7 +100,7 @@ public class HSSFCell implements Cell {
*/ */
protected HSSFCell(HSSFWorkbook book, HSSFSheet sheet, int row, short col) protected HSSFCell(HSSFWorkbook book, HSSFSheet sheet, int row, short col)
{ {
checkBounds(col); checkBounds(col, book);
_stringValue = null; _stringValue = null;
_book = book; _book = book;
_sheet = sheet; _sheet = sheet;
@ -150,7 +145,7 @@ public class HSSFCell implements Cell {
protected HSSFCell(HSSFWorkbook book, HSSFSheet sheet, int row, short col, protected HSSFCell(HSSFWorkbook book, HSSFSheet sheet, int row, short col,
CellType type) CellType type)
{ {
checkBounds(col); checkBounds(col, book);
_cellType = CellType._NONE; // Force 'setCellType' to create a first Record _cellType = CellType._NONE; // Force 'setCellType' to create a first Record
_stringValue = null; _stringValue = null;
_book = book; _book = book;
@ -570,8 +565,8 @@ public class HSSFCell implements Cell {
return; return;
} }
if(value.length() > SpreadsheetVersion.EXCEL97.getMaxTextLength()){ if(value.length() > _book.getSpreadsheetVersion().getMaxTextLength()){
throw new IllegalArgumentException("The maximum length of cell contents (text) is 32,767 characters"); throw new IllegalArgumentException("The maximum length of cell contents (text) is "+_book.getSpreadsheetVersion().getMaxTextLength()+" characters");
} }
if (_cellType == CellType.FORMULA) { if (_cellType == CellType.FORMULA) {
@ -977,8 +972,10 @@ public class HSSFCell implements Cell {
/** /**
* @throws RuntimeException if the bounds are exceeded. * @throws RuntimeException if the bounds are exceeded.
*/ */
private static void checkBounds(int cellIndex) { private static void checkBounds(int cellIndex, final HSSFWorkbook book) {
final int LAST_COLUMN_NUMBER = book.getSpreadsheetVersion().getLastColumnIndex(); // 2^8 - 1
if (cellIndex < 0 || cellIndex > LAST_COLUMN_NUMBER) { if (cellIndex < 0 || cellIndex > LAST_COLUMN_NUMBER) {
final String LAST_COLUMN_NAME = book.getSpreadsheetVersion().getLastColumnName();
throw new IllegalArgumentException("Invalid column index (" + cellIndex throw new IllegalArgumentException("Invalid column index (" + cellIndex
+ "). Allowable column range for " + FILE_FORMAT_NAME + " is (0.." + "). Allowable column range for " + FILE_FORMAT_NAME + " is (0.."
+ LAST_COLUMN_NUMBER + ") or ('A'..'" + LAST_COLUMN_NAME + "')"); + LAST_COLUMN_NUMBER + ") or ('A'..'" + LAST_COLUMN_NAME + "')");

View File

@ -296,7 +296,7 @@ public final class HSSFEvaluationWorkbook implements FormulaRenderingWorkbook, E
@Override @Override
public SpreadsheetVersion getSpreadsheetVersion(){ public SpreadsheetVersion getSpreadsheetVersion(){
return SpreadsheetVersion.EXCEL97; return _uBook.getSpreadsheetVersion();
} }
/** /**

View File

@ -129,7 +129,7 @@ public final class HSSFName implements Name {
* @throws IllegalArgumentException if the name is invalid or the name already exists (case-insensitive) * @throws IllegalArgumentException if the name is invalid or the name already exists (case-insensitive)
*/ */
public void setNameName(String nameName){ public void setNameName(String nameName){
validateName(nameName); validateName(nameName, _book.getSpreadsheetVersion());
InternalWorkbook wb = _book.getWorkbook(); InternalWorkbook wb = _book.getWorkbook();
_definedNameRec.setNameText(nameName); _definedNameRec.setNameText(nameName);
@ -178,7 +178,7 @@ public final class HSSFName implements Name {
* *
* @param name * @param name
*/ */
private static void validateName(String name) { private static void validateName(String name, SpreadsheetVersion spreadsheetVersion) {
if (name.length() == 0) { if (name.length() == 0) {
throw new IllegalArgumentException("Name cannot be blank"); throw new IllegalArgumentException("Name cannot be blank");
@ -212,7 +212,7 @@ public final class HSSFName implements Name {
if (name.matches("[A-Za-z]+\\d+")) { if (name.matches("[A-Za-z]+\\d+")) {
String col = name.replaceAll("\\d", ""); String col = name.replaceAll("\\d", "");
String row = name.replaceAll("[A-Za-z]", ""); String row = name.replaceAll("[A-Za-z]", "");
if (CellReference.cellReferenceIsWithinRange(col, row, SpreadsheetVersion.EXCEL97)) { if (CellReference.cellReferenceIsWithinRange(col, row, spreadsheetVersion)) {
throw new IllegalArgumentException("Invalid name: '"+name+"': cannot be $A$1-style cell reference"); throw new IllegalArgumentException("Invalid name: '"+name+"': cannot be $A$1-style cell reference");
} }
} }

View File

@ -247,7 +247,7 @@ public final class HSSFRow implements Row, Comparable<HSSFRow> {
*/ */
@Override @Override
public void setRowNum(int rowIndex) { public void setRowNum(int rowIndex) {
int maxrow = SpreadsheetVersion.EXCEL97.getLastRowIndex(); int maxrow = book.getSpreadsheetVersion().getLastRowIndex();
if ((rowIndex < 0) || (rowIndex > maxrow)) { if ((rowIndex < 0) || (rowIndex > maxrow)) {
throw new IllegalArgumentException("Invalid row number (" + rowIndex throw new IllegalArgumentException("Invalid row number (" + rowIndex
+ ") outside allowable range (0.." + maxrow + ")"); + ") outside allowable range (0.." + maxrow + ")");

View File

@ -123,7 +123,7 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
* @see org.apache.poi.hssf.usermodel.HSSFWorkbook#createSheet() * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#createSheet()
*/ */
protected HSSFSheet(HSSFWorkbook workbook) { protected HSSFSheet(HSSFWorkbook workbook) {
_sheet = InternalSheet.createSheet(); _sheet = InternalSheet.createSheet(workbook.getSpreadsheetVersion());
_rows = new TreeMap<Integer, HSSFRow>(); _rows = new TreeMap<Integer, HSSFRow>();
this._workbook = workbook; this._workbook = workbook;
this._book = workbook.getWorkbook(); this._book = workbook.getWorkbook();
@ -715,7 +715,7 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
if (region.getNumberOfCells() < 2) { if (region.getNumberOfCells() < 2) {
throw new IllegalArgumentException("Merged region " + region.formatAsString() + " must contain 2 or more cells"); throw new IllegalArgumentException("Merged region " + region.formatAsString() + " must contain 2 or more cells");
} }
region.validate(SpreadsheetVersion.EXCEL97); region.validate(_workbook.getSpreadsheetVersion());
if (validate) { if (validate) {
// throw IllegalStateException if the argument CellRangeAddress intersects with // throw IllegalStateException if the argument CellRangeAddress intersects with
@ -1453,7 +1453,7 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
*/ */
@Override @Override
public void showInPane(int toprow, int leftcol) { public void showInPane(int toprow, int leftcol) {
int maxrow = SpreadsheetVersion.EXCEL97.getLastRowIndex(); final int maxrow = _workbook.getSpreadsheetVersion().getLastRowIndex();
if (toprow > maxrow) throw new IllegalArgumentException("Maximum row number is " + maxrow); if (toprow > maxrow) throw new IllegalArgumentException("Maximum row number is " + maxrow);
showInPane((short)toprow, (short)leftcol); showInPane((short)toprow, (short)leftcol);
@ -1530,10 +1530,10 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
* *
* @param row the row number * @param row the row number
*/ */
private static int clip(int row) { private int clip(int row) {
return Math.min( return Math.min(
Math.max(0, row), Math.max(0, row),
SpreadsheetVersion.EXCEL97.getLastRowIndex()); _workbook.getSpreadsheetVersion().getLastRowIndex());
} }
/** /**
@ -1677,7 +1677,7 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
} }
} }
if (endRow + n > _lastrow) { if (endRow + n > _lastrow) {
_lastrow = Math.min(endRow + n, SpreadsheetVersion.EXCEL97.getLastRowIndex()); _lastrow = Math.min(endRow + n, _workbook.getSpreadsheetVersion().getLastRowIndex());
} }
} else { } else {
// Rows are moving up // Rows are moving up
@ -1687,7 +1687,7 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
if (endRow == _lastrow) { if (endRow == _lastrow) {
// Need to walk backward to find the last non-blank row // Need to walk backward to find the last non-blank row
// NOTE: n is always negative here // NOTE: n is always negative here
_lastrow = Math.min(endRow + n, SpreadsheetVersion.EXCEL97.getLastRowIndex()); _lastrow = Math.min(endRow + n, _workbook.getSpreadsheetVersion().getLastRowIndex());
for (int i = endRow - 1; i > endRow + n; i--) { for (int i = endRow - 1; i > endRow + n; i--) {
if (getRow(i) != null) { if (getRow(i) != null) {
_lastrow = i; _lastrow = i;
@ -1703,7 +1703,7 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
String sheetName = _workbook.getSheetName(sheetIndex); String sheetName = _workbook.getSheetName(sheetIndex);
short externSheetIndex = _book.checkExternSheet(sheetIndex); short externSheetIndex = _book.checkExternSheet(sheetIndex);
FormulaShifter shifter = FormulaShifter.createForRowShift( FormulaShifter shifter = FormulaShifter.createForRowShift(
externSheetIndex, sheetName, startRow, endRow, n, SpreadsheetVersion.EXCEL97); externSheetIndex, sheetName, startRow, endRow, n, _workbook.getSpreadsheetVersion());
_sheet.updateFormulasAfterCellShift(shifter, externSheetIndex); _sheet.updateFormulasAfterCellShift(shifter, externSheetIndex);
int nSheets = _workbook.getNumberOfSheets(); int nSheets = _workbook.getNumberOfSheets();
@ -1923,7 +1923,7 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
@Override @Override
public void setColumnBreak(int column) { public void setColumnBreak(int column) {
validateColumn((short) column); validateColumn((short) column);
_sheet.getPageSettings().setColumnBreak((short) column, (short) 0, (short) SpreadsheetVersion.EXCEL97.getLastRowIndex()); _sheet.getPageSettings().setColumnBreak((short) column, (short) 0, (short) _workbook.getSpreadsheetVersion().getLastRowIndex());
} }
/** /**
@ -1953,7 +1953,7 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
* @param row the index of the row to validate, zero-based * @param row the index of the row to validate, zero-based
*/ */
protected void validateRow(int row) { protected void validateRow(int row) {
int maxrow = SpreadsheetVersion.EXCEL97.getLastRowIndex(); final int maxrow = _workbook.getSpreadsheetVersion().getLastRowIndex();
if (row > maxrow) throw new IllegalArgumentException("Maximum row number is " + maxrow); if (row > maxrow) throw new IllegalArgumentException("Maximum row number is " + maxrow);
if (row < 0) throw new IllegalArgumentException("Minumum row number is 0"); if (row < 0) throw new IllegalArgumentException("Minumum row number is 0");
} }
@ -1964,7 +1964,7 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
* @param column the index of the column to validate, zero-based * @param column the index of the column to validate, zero-based
*/ */
protected void validateColumn(int column) { protected void validateColumn(int column) {
int maxcol = SpreadsheetVersion.EXCEL97.getLastColumnIndex(); final int maxcol = _workbook.getSpreadsheetVersion().getLastColumnIndex();
if (column > maxcol) throw new IllegalArgumentException("Maximum column number is " + maxcol); if (column > maxcol) throw new IllegalArgumentException("Maximum column number is " + maxcol);
if (column < 0) throw new IllegalArgumentException("Minimum column number is 0"); if (column < 0) throw new IllegalArgumentException("Minimum column number is 0");
} }
@ -2496,8 +2496,8 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
private void setRepeatingRowsAndColumns( private void setRepeatingRowsAndColumns(
CellRangeAddress rowDef, CellRangeAddress colDef) { CellRangeAddress rowDef, CellRangeAddress colDef) {
int sheetIndex = _workbook.getSheetIndex(this); int sheetIndex = _workbook.getSheetIndex(this);
int maxRowIndex = SpreadsheetVersion.EXCEL97.getLastRowIndex(); final int maxRowIndex = _workbook.getSpreadsheetVersion().getLastRowIndex();
int maxColIndex = SpreadsheetVersion.EXCEL97.getLastColumnIndex(); final int maxColIndex = _workbook.getSpreadsheetVersion().getLastColumnIndex();
int col1 = -1; int col1 = -1;
int col2 = -1; int col2 = -1;
@ -2582,8 +2582,8 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
return null; return null;
} }
int maxRowIndex = SpreadsheetVersion.EXCEL97.getLastRowIndex(); final int maxRowIndex = _workbook.getSpreadsheetVersion().getLastRowIndex();
int maxColIndex = SpreadsheetVersion.EXCEL97.getLastColumnIndex(); final int maxColIndex = _workbook.getSpreadsheetVersion().getLastColumnIndex();
for (Ptg ptg : nameDefinition) { for (Ptg ptg : nameDefinition) {

View File

@ -174,7 +174,7 @@ public final class HSSFSheetConditionalFormatting implements SheetConditionalFor
if (regions == null) { if (regions == null) {
throw new IllegalArgumentException("regions must not be null"); throw new IllegalArgumentException("regions must not be null");
} }
for(CellRangeAddress range : regions) range.validate(SpreadsheetVersion.EXCEL97); for(CellRangeAddress range : regions) range.validate(_sheet._workbook.getSpreadsheetVersion());
if (cfRules == null) { if (cfRules == null) {
throw new IllegalArgumentException("cfRules must not be null"); throw new IllegalArgumentException("cfRules must not be null");

View File

@ -143,6 +143,11 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss
public final static int INITIAL_CAPACITY = Configurator.getIntValue("HSSFWorkbook.SheetInitialCapacity",3); public final static int INITIAL_CAPACITY = Configurator.getIntValue("HSSFWorkbook.SheetInitialCapacity",3);
/**
* SpreadsheetVersion used by this workbook, EXCEL97 in every case where we need to read or write to disk
*/
public final SpreadsheetVersion spreadsheetVersion;
/** /**
* this is the reference to the low level Workbook object * this is the reference to the low level Workbook object
*/ */
@ -207,8 +212,19 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss
this(InternalWorkbook.createWorkbook()); this(InternalWorkbook.createWorkbook());
} }
public HSSFWorkbook(final SpreadsheetVersion spreadsheetVersion) {
this(InternalWorkbook.createWorkbook(), spreadsheetVersion);
if(spreadsheetVersion == null)
throw new IllegalArgumentException("SpreadsheetVersion must be non-null");
}
private HSSFWorkbook(InternalWorkbook book) { private HSSFWorkbook(InternalWorkbook book) {
this(book, SpreadsheetVersion.EXCEL97);
}
private HSSFWorkbook(InternalWorkbook book, final SpreadsheetVersion spreadsheetVersion) {
super((DirectoryNode)null); super((DirectoryNode)null);
this.spreadsheetVersion = spreadsheetVersion;
workbook = book; workbook = book;
_sheets = new ArrayList<HSSFSheet>(INITIAL_CAPACITY); _sheets = new ArrayList<HSSFSheet>(INITIAL_CAPACITY);
names = new ArrayList<HSSFName>(INITIAL_CAPACITY); names = new ArrayList<HSSFName>(INITIAL_CAPACITY);
@ -328,6 +344,7 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss
throws IOException throws IOException
{ {
super(directory); super(directory);
spreadsheetVersion = SpreadsheetVersion.EXCEL97;
String workbookName = getWorkbookDirEntryName(directory); String workbookName = getWorkbookDirEntryName(directory);
this.preserveNodes = preserveNodes; this.preserveNodes = preserveNodes;
@ -1363,6 +1380,12 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss
super.close(); super.close();
} }
protected void validateSpreadsheetVersionWritePossible() throws IllegalStateException {
if (this.spreadsheetVersion != SpreadsheetVersion.EXCEL97) {
throw new IllegalStateException("SpreadsheetVersion not EXCEL97, cannot write file meant only for in-memory calculations");
}
}
/** /**
* Write out this workbook to the currently open {@link File} via the * Write out this workbook to the currently open {@link File} via the
* writeable {@link POIFSFileSystem} it was opened as. * writeable {@link POIFSFileSystem} it was opened as.
@ -1375,6 +1398,7 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss
*/ */
@Override @Override
public void write() throws IOException { public void write() throws IOException {
validateSpreadsheetVersionWritePossible();
validateInPlaceWritePossible(); validateInPlaceWritePossible();
final DirectoryNode dir = getDirectory(); final DirectoryNode dir = getDirectory();
@ -1408,6 +1432,7 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss
*/ */
@Override @Override
public void write(File newFile) throws IOException { public void write(File newFile) throws IOException {
validateSpreadsheetVersionWritePossible();
POIFSFileSystem fs = POIFSFileSystem.create(newFile); POIFSFileSystem fs = POIFSFileSystem.create(newFile);
try { try {
write(fs); write(fs);
@ -1434,6 +1459,7 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss
*/ */
@Override @Override
public void write(OutputStream stream) throws IOException { public void write(OutputStream stream) throws IOException {
validateSpreadsheetVersionWritePossible();
NPOIFSFileSystem fs = new NPOIFSFileSystem(); NPOIFSFileSystem fs = new NPOIFSFileSystem();
try { try {
write(fs); write(fs);
@ -1445,6 +1471,7 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss
/** Writes the workbook out to a brand new, empty POIFS */ /** Writes the workbook out to a brand new, empty POIFS */
private void write(NPOIFSFileSystem fs) throws IOException { private void write(NPOIFSFileSystem fs) throws IOException {
validateSpreadsheetVersionWritePossible();
// For tracking what we've written out, used if we're // For tracking what we've written out, used if we're
// going to be preserving nodes // going to be preserving nodes
List<String> excepts = new ArrayList<String>(1); List<String> excepts = new ArrayList<String>(1);
@ -2325,6 +2352,6 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss
*/ */
@Override @Override
public SpreadsheetVersion getSpreadsheetVersion() { public SpreadsheetVersion getSpreadsheetVersion() {
return SpreadsheetVersion.EXCEL97; return this.spreadsheetVersion;
} }
} }

View File

@ -402,7 +402,7 @@ public final class XSSFName implements Name {
if (name.matches("[A-Za-z]+\\d+")) { if (name.matches("[A-Za-z]+\\d+")) {
String col = name.replaceAll("\\d", ""); String col = name.replaceAll("\\d", "");
String row = name.replaceAll("[A-Za-z]", ""); String row = name.replaceAll("[A-Za-z]", "");
if (CellReference.cellReferenceIsWithinRange(col, row, SpreadsheetVersion.EXCEL97)) { if (CellReference.cellReferenceIsWithinRange(col, row, SpreadsheetVersion.EXCEL97)) { // todo: is EXCEL97 correct? sounds wrong...
throw new IllegalArgumentException("Invalid name: '"+name+"': cannot be $A$1-style cell reference"); throw new IllegalArgumentException("Invalid name: '"+name+"': cannot be $A$1-style cell reference");
} }
} }