mirror of https://github.com/apache/poi.git
Fix NullPointerException in Autosize introduced via #64981
Only happened when alignment is set Add a test reproducing the problem Also verify auto-size on SXSSF git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1884653 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
d20fa44305
commit
01dabc0d1b
|
@ -592,7 +592,7 @@ public class XSSFCellStyle implements CellStyle, Duplicatable {
|
||||||
@Override
|
@Override
|
||||||
public short getRotation() {
|
public short getRotation() {
|
||||||
CTCellAlignment align = _cellXf.getAlignment();
|
CTCellAlignment align = _cellXf.getAlignment();
|
||||||
return align == null ? 0 : align.getTextRotation().shortValue();
|
return align == null || align.getTextRotation() == null ? 0 : align.getTextRotation().shortValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -0,0 +1,39 @@
|
||||||
|
/* ====================================================================
|
||||||
|
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.xssf.usermodel;
|
||||||
|
|
||||||
|
import org.apache.poi.ss.usermodel.BaseTestSheetAutosizeColumn;
|
||||||
|
import org.apache.poi.ss.usermodel.Sheet;
|
||||||
|
import org.apache.poi.ss.usermodel.Workbook;
|
||||||
|
import org.apache.poi.xssf.SXSSFITestDataProvider;
|
||||||
|
import org.apache.poi.xssf.streaming.SXSSFSheet;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Yegor Kozlov
|
||||||
|
*/
|
||||||
|
public final class TestSXSSFSheetAutosizeColumn extends BaseTestSheetAutosizeColumn {
|
||||||
|
|
||||||
|
public TestSXSSFSheetAutosizeColumn(){
|
||||||
|
super(SXSSFITestDataProvider.instance);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void trackColumnsForAutoSizingIfSXSSF(Sheet sheet) {
|
||||||
|
((SXSSFSheet)sheet).trackAllColumnsForAutoSizing();
|
||||||
|
}
|
||||||
|
}
|
|
@ -21,6 +21,7 @@ import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
|
import java.util.Date;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
||||||
import org.apache.poi.ss.ITestDataProvider;
|
import org.apache.poi.ss.ITestDataProvider;
|
||||||
|
@ -40,22 +41,22 @@ public abstract class BaseTestSheetAutosizeColumn {
|
||||||
private final ITestDataProvider _testDataProvider;
|
private final ITestDataProvider _testDataProvider;
|
||||||
|
|
||||||
private static Locale userLocale;
|
private static Locale userLocale;
|
||||||
|
|
||||||
@BeforeClass
|
@BeforeClass
|
||||||
public static void initLocale() {
|
public static void initLocale() {
|
||||||
userLocale = LocaleUtil.getUserLocale();
|
userLocale = LocaleUtil.getUserLocale();
|
||||||
LocaleUtil.setUserLocale(Locale.ROOT);
|
LocaleUtil.setUserLocale(Locale.ROOT);
|
||||||
}
|
}
|
||||||
|
|
||||||
@AfterClass
|
@AfterClass
|
||||||
public static void resetLocale() {
|
public static void resetLocale() {
|
||||||
LocaleUtil.setUserLocale(userLocale);
|
LocaleUtil.setUserLocale(userLocale);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected BaseTestSheetAutosizeColumn(ITestDataProvider testDataProvider) {
|
protected BaseTestSheetAutosizeColumn(ITestDataProvider testDataProvider) {
|
||||||
_testDataProvider = testDataProvider;
|
_testDataProvider = testDataProvider;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void trackColumnsForAutoSizingIfSXSSF(Sheet sheet) {
|
protected void trackColumnsForAutoSizingIfSXSSF(Sheet sheet) {
|
||||||
// do nothing for Sheet base class. This will be overridden for SXSSFSheets.
|
// do nothing for Sheet base class. This will be overridden for SXSSFSheets.
|
||||||
}
|
}
|
||||||
|
@ -98,7 +99,7 @@ public abstract class BaseTestSheetAutosizeColumn {
|
||||||
assertEquals(sheet.getColumnWidth(1), sheet.getColumnWidth(2)); // columns 1, 2 and 3 should have the same width
|
assertEquals(sheet.getColumnWidth(1), sheet.getColumnWidth(2)); // columns 1, 2 and 3 should have the same width
|
||||||
assertEquals(sheet.getColumnWidth(2), sheet.getColumnWidth(3)); // columns 1, 2 and 3 should have the same width
|
assertEquals(sheet.getColumnWidth(2), sheet.getColumnWidth(3)); // columns 1, 2 and 3 should have the same width
|
||||||
assertEquals(sheet.getColumnWidth(4), sheet.getColumnWidth(5)); // 10.0000 and '10.0000'
|
assertEquals(sheet.getColumnWidth(4), sheet.getColumnWidth(5)); // 10.0000 and '10.0000'
|
||||||
|
|
||||||
workbook.close();
|
workbook.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -196,7 +197,7 @@ public abstract class BaseTestSheetAutosizeColumn {
|
||||||
assertTrue(sheet.getColumnWidth(5) > sheet.getColumnWidth(3)); // 'mmm/dd/yyyy' is wider than 'mmm'
|
assertTrue(sheet.getColumnWidth(5) > sheet.getColumnWidth(3)); // 'mmm/dd/yyyy' is wider than 'mmm'
|
||||||
assertEquals(sheet.getColumnWidth(6), sheet.getColumnWidth(5)); // date formatted as 'mmm/dd/yyyy'
|
assertEquals(sheet.getColumnWidth(6), sheet.getColumnWidth(5)); // date formatted as 'mmm/dd/yyyy'
|
||||||
assertEquals(sheet.getColumnWidth(4), sheet.getColumnWidth(7)); // date formula formatted as 'mmm'
|
assertEquals(sheet.getColumnWidth(4), sheet.getColumnWidth(7)); // date formula formatted as 'mmm'
|
||||||
|
|
||||||
workbook.close();
|
workbook.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -206,7 +207,7 @@ public abstract class BaseTestSheetAutosizeColumn {
|
||||||
Sheet sheet = workbook.createSheet();
|
Sheet sheet = workbook.createSheet();
|
||||||
trackColumnsForAutoSizingIfSXSSF(sheet);
|
trackColumnsForAutoSizingIfSXSSF(sheet);
|
||||||
Row row = sheet.createRow(0);
|
Row row = sheet.createRow(0);
|
||||||
|
|
||||||
Font defaultFont = workbook.getFontAt(0);
|
Font defaultFont = workbook.getFontAt(0);
|
||||||
|
|
||||||
CellStyle style1 = workbook.createCellStyle();
|
CellStyle style1 = workbook.createCellStyle();
|
||||||
|
@ -230,7 +231,7 @@ public abstract class BaseTestSheetAutosizeColumn {
|
||||||
assertTrue(2*sheet.getColumnWidth(1) < sheet.getColumnWidth(2));
|
assertTrue(2*sheet.getColumnWidth(1) < sheet.getColumnWidth(2));
|
||||||
assertEquals(sheet.getColumnWidth(4), sheet.getColumnWidth(3));
|
assertEquals(sheet.getColumnWidth(4), sheet.getColumnWidth(3));
|
||||||
assertTrue(sheet.getColumnWidth(5) > sheet.getColumnWidth(4)); //larger font results in a wider column width
|
assertTrue(sheet.getColumnWidth(5) > sheet.getColumnWidth(4)); //larger font results in a wider column width
|
||||||
|
|
||||||
workbook.close();
|
workbook.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -257,7 +258,7 @@ public abstract class BaseTestSheetAutosizeColumn {
|
||||||
int w1 = sheet.getColumnWidth(1);
|
int w1 = sheet.getColumnWidth(1);
|
||||||
|
|
||||||
assertTrue(w0*5 < w1); // rotated text occupies at least five times less horizontal space than normal text
|
assertTrue(w0*5 < w1); // rotated text occupies at least five times less horizontal space than normal text
|
||||||
|
|
||||||
workbook.close();
|
workbook.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -280,7 +281,7 @@ public abstract class BaseTestSheetAutosizeColumn {
|
||||||
|
|
||||||
sheet.autoSizeColumn(0, true);
|
sheet.autoSizeColumn(0, true);
|
||||||
assertTrue(sheet.getColumnWidth(0) > defaulWidth);
|
assertTrue(sheet.getColumnWidth(0) > defaulWidth);
|
||||||
|
|
||||||
workbook.close();
|
workbook.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -293,40 +294,40 @@ public abstract class BaseTestSheetAutosizeColumn {
|
||||||
Workbook workbook = _testDataProvider.createWorkbook();
|
Workbook workbook = _testDataProvider.createWorkbook();
|
||||||
Sheet sheet = workbook.createSheet();
|
Sheet sheet = workbook.createSheet();
|
||||||
trackColumnsForAutoSizingIfSXSSF(sheet);
|
trackColumnsForAutoSizingIfSXSSF(sheet);
|
||||||
|
|
||||||
Row r0 = sheet.createRow(0);
|
Row r0 = sheet.createRow(0);
|
||||||
r0.createCell(0).setCellValue("I am ROW 0");
|
r0.createCell(0).setCellValue("I am ROW 0");
|
||||||
Row r200 = sheet.createRow(200);
|
Row r200 = sheet.createRow(200);
|
||||||
r200.createCell(0).setCellValue("I am ROW 200");
|
r200.createCell(0).setCellValue("I am ROW 200");
|
||||||
|
|
||||||
// This should work fine
|
// This should work fine
|
||||||
sheet.autoSizeColumn(0);
|
sheet.autoSizeColumn(0);
|
||||||
|
|
||||||
// Get close to 32767
|
// Get close to 32767
|
||||||
Row r32765 = sheet.createRow(32765);
|
Row r32765 = sheet.createRow(32765);
|
||||||
r32765.createCell(0).setCellValue("Nearly there...");
|
r32765.createCell(0).setCellValue("Nearly there...");
|
||||||
sheet.autoSizeColumn(0);
|
sheet.autoSizeColumn(0);
|
||||||
|
|
||||||
// To it
|
// To it
|
||||||
Row r32767 = sheet.createRow(32767);
|
Row r32767 = sheet.createRow(32767);
|
||||||
r32767.createCell(0).setCellValue("At the boundary");
|
r32767.createCell(0).setCellValue("At the boundary");
|
||||||
sheet.autoSizeColumn(0);
|
sheet.autoSizeColumn(0);
|
||||||
|
|
||||||
// And passed it
|
// And passed it
|
||||||
Row r32768 = sheet.createRow(32768);
|
Row r32768 = sheet.createRow(32768);
|
||||||
r32768.createCell(0).setCellValue("Passed");
|
r32768.createCell(0).setCellValue("Passed");
|
||||||
Row r32769 = sheet.createRow(32769);
|
Row r32769 = sheet.createRow(32769);
|
||||||
r32769.createCell(0).setCellValue("More Passed");
|
r32769.createCell(0).setCellValue("More Passed");
|
||||||
sheet.autoSizeColumn(0);
|
sheet.autoSizeColumn(0);
|
||||||
|
|
||||||
// Long way passed
|
// Long way passed
|
||||||
Row r60708 = sheet.createRow(60708);
|
Row r60708 = sheet.createRow(60708);
|
||||||
r60708.createCell(0).setCellValue("Near the end");
|
r60708.createCell(0).setCellValue("Near the end");
|
||||||
sheet.autoSizeColumn(0);
|
sheet.autoSizeColumn(0);
|
||||||
|
|
||||||
workbook.close();
|
workbook.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO should we have this stuff in the FormulaEvaluator?
|
// TODO should we have this stuff in the FormulaEvaluator?
|
||||||
private void evaluateWorkbook(Workbook workbook){
|
private void evaluateWorkbook(Workbook workbook){
|
||||||
FormulaEvaluator eval = workbook.getCreationHelper().createFormulaEvaluator();
|
FormulaEvaluator eval = workbook.getCreationHelper().createFormulaEvaluator();
|
||||||
|
@ -341,4 +342,22 @@ public abstract class BaseTestSheetAutosizeColumn {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testExcelExporter() {
|
||||||
|
final Workbook wb = _testDataProvider.createWorkbook();
|
||||||
|
final Sheet sheet = wb.createSheet("test");
|
||||||
|
trackColumnsForAutoSizingIfSXSSF(sheet);
|
||||||
|
final Row row = sheet.createRow(0);
|
||||||
|
final Cell cell = row.createCell(0);
|
||||||
|
|
||||||
|
CellStyle csDateTime = wb.createCellStyle();
|
||||||
|
csDateTime.setAlignment(HorizontalAlignment.LEFT);
|
||||||
|
|
||||||
|
cell.setCellValue(new Date(Long.parseLong("1439800763994")));
|
||||||
|
cell.setCellStyle(csDateTime);
|
||||||
|
|
||||||
|
sheet.autoSizeColumn(0);
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue