Avoid crashing Excel when sorting XSSFSheet autofilter, see Bugzilla 50315

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1041407 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yegor Kozlov 2010-12-02 15:23:35 +00:00
parent 4c6dd246e2
commit 0f86c22ae8
5 changed files with 27 additions and 6 deletions

View File

@ -34,6 +34,7 @@
<changes> <changes>
<release version="3.8-beta1" date="2010-??-??"> <release version="3.8-beta1" date="2010-??-??">
<action dev="poi-developers" type="add">50315 - Avoid crashing Excel when sorting XSSFSheet autofilter</action>
<action dev="poi-developers" type="add">50076 - Allow access from XSSFReader to sheet comments and headers/footers</action> <action dev="poi-developers" type="add">50076 - Allow access from XSSFReader to sheet comments and headers/footers</action>
<action dev="poi-developers" type="add">50076 - Refactor XSSFEventBasedExcelExtractor to make it easier for you to have control over outputting the cell contents</action> <action dev="poi-developers" type="add">50076 - Refactor XSSFEventBasedExcelExtractor to make it easier for you to have control over outputting the cell contents</action>
<action dev="poi-developers" type="fix">50258 - avoid corruption of XSSFWorkbook after applying XSSFRichTextRun#applyFont</action> <action dev="poi-developers" type="fix">50258 - avoid corruption of XSSFWorkbook after applying XSSFRichTextRun#applyFont</action>

View File

@ -85,7 +85,7 @@ public final class XSSFName implements Name {
* <li> This defined name refers to a range to which an AutoFilter has been * <li> This defined name refers to a range to which an AutoFilter has been
* applied * applied
*/ */
public final static String BUILTIN_FILTER_DB = "_xlnm._FilterDatabase:"; public final static String BUILTIN_FILTER_DB = "_xlnm._FilterDatabase";
/** /**
* A built-in defined name that refers to a consolidation area * A built-in defined name that refers to a consolidation area

View File

@ -2940,6 +2940,18 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
String ref = norm.formatAsString(); String ref = norm.formatAsString();
af.setRef(ref); af.setRef(ref);
XSSFWorkbook wb = getWorkbook();
int sheetIndex = getWorkbook().getSheetIndex(this);
XSSFName name = wb.getBuiltInName(XSSFName.BUILTIN_FILTER_DB, sheetIndex);
if (name == null) {
name = wb.createBuiltInName(XSSFName.BUILTIN_FILTER_DB, sheetIndex);
name.getCTName().setHidden(true);
CellReference r1 = new CellReference(getSheetName(), range.getFirstRow(), range.getFirstColumn(), true, true);
CellReference r2 = new CellReference(null, range.getLastRow(), range.getLastColumn(), true, true);
String fmla = r1.formatAsString() + ":" + r2.formatAsString();
name.setRefersToFormula(fmla);
}
return new XSSFAutoFilter(this); return new XSSFAutoFilter(this);
} }
} }

View File

@ -941,7 +941,6 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
XSSFName name = getBuiltInName(XSSFName.BUILTIN_PRINT_AREA, sheetIndex); XSSFName name = getBuiltInName(XSSFName.BUILTIN_PRINT_AREA, sheetIndex);
if (name == null) { if (name == null) {
name = createBuiltInName(XSSFName.BUILTIN_PRINT_AREA, sheetIndex); name = createBuiltInName(XSSFName.BUILTIN_PRINT_AREA, sheetIndex);
namedRanges.add(name);
} }
//short externSheetIndex = getWorkbook().checkExternSheet(sheetIndex); //short externSheetIndex = getWorkbook().checkExternSheet(sheetIndex);
//name.setExternSheetNumber(externSheetIndex); //name.setExternSheetNumber(externSheetIndex);
@ -1014,7 +1013,6 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
} }
if (name == null) { if (name == null) {
name = createBuiltInName(XSSFName.BUILTIN_PRINT_TITLE, sheetIndex); name = createBuiltInName(XSSFName.BUILTIN_PRINT_TITLE, sheetIndex);
namedRanges.add(name);
} }
String reference = getReferenceBuiltInRecord(name.getSheetName(), startColumn, endColumn, startRow, endRow); String reference = getReferenceBuiltInRecord(name.getSheetName(), startColumn, endColumn, startRow, endRow);
@ -1061,7 +1059,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
return "$" + colRef.getCellRefParts()[2] + "$" + colRef.getCellRefParts()[1] + ":$" + colRef2.getCellRefParts()[2] + "$" + colRef2.getCellRefParts()[1]; return "$" + colRef.getCellRefParts()[2] + "$" + colRef.getCellRefParts()[1] + ":$" + colRef2.getCellRefParts()[2] + "$" + colRef2.getCellRefParts()[1];
} }
private XSSFName getBuiltInName(String builtInCode, int sheetNumber) { XSSFName getBuiltInName(String builtInCode, int sheetNumber) {
for (XSSFName name : namedRanges) { for (XSSFName name : namedRanges) {
if (name.getNameName().equalsIgnoreCase(builtInCode) && name.getSheetIndex() == sheetNumber) { if (name.getNameName().equalsIgnoreCase(builtInCode) && name.getSheetIndex() == sheetNumber) {
return name; return name;
@ -1077,7 +1075,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
* @throws IllegalArgumentException if sheetNumber is invalid * @throws IllegalArgumentException if sheetNumber is invalid
* @throws POIXMLException if such a name already exists in the workbook * @throws POIXMLException if such a name already exists in the workbook
*/ */
private XSSFName createBuiltInName(String builtInName, int sheetNumber) { XSSFName createBuiltInName(String builtInName, int sheetNumber) {
validateSheetIndex(sheetNumber); validateSheetIndex(sheetNumber);
CTDefinedNames names = workbook.getDefinedNames() == null ? workbook.addNewDefinedNames() : workbook.getDefinedNames(); CTDefinedNames names = workbook.getDefinedNames() == null ? workbook.addNewDefinedNames() : workbook.getDefinedNames();
@ -1092,6 +1090,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
+ ") already exists for sheet (" + sheetNumber + ")"); + ") already exists for sheet (" + sheetNumber + ")");
} }
namedRanges.add(name);
return name; return name;
} }

View File

@ -980,10 +980,19 @@ public final class TestXSSFSheet extends BaseTestSheet {
public void testSetAutoFilter() { public void testSetAutoFilter() {
XSSFWorkbook wb = new XSSFWorkbook(); XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet(); XSSFSheet sheet = wb.createSheet("new sheet");
sheet.setAutoFilter(CellRangeAddress.valueOf("A1:D100")); sheet.setAutoFilter(CellRangeAddress.valueOf("A1:D100"));
assertEquals("A1:D100", sheet.getCTWorksheet().getAutoFilter().getRef()); assertEquals("A1:D100", sheet.getCTWorksheet().getAutoFilter().getRef());
// auto-filter must be registered in workboook.xml, see Bugzilla 50315
XSSFName nm = wb.getBuiltInName(XSSFName.BUILTIN_FILTER_DB, 0);
assertNotNull(nm);
assertEquals(0, nm.getCTName().getLocalSheetId());
assertEquals(true, nm.getCTName().getHidden());
assertEquals("_xlnm._FilterDatabase", nm.getCTName().getName());
assertEquals("'new sheet'!$A$1:$D$100", nm.getCTName().getStringValue());
} }
public void testProtectSheet_lowlevel() { public void testProtectSheet_lowlevel() {