private Map<Integer, CTCellFormula> sharedFormulas;
private TreeMap<String,XSSFTable> tables;
private List<CellRangeAddress> arrayFormulas;
- private XSSFDataValidationHelper dataValidationHelper;
+ private XSSFDataValidationHelper dataValidationHelper;
/**
* Creates new XSSFSheet - called by XSSFWorkbook to create a sheet from scratch.
tables.put( p.getPackageRelationship().getId(), (XSSFTable)p );
}
}
-
+
// Process external hyperlinks for the sheet, if there are any
initHyperlinks();
}
}
@SuppressWarnings("deprecation") //YK: getXYZArray() array accessors are deprecated in xmlbeans with JDK 1.5 support
- private void initRows(CTWorksheet worksheet) {
+ private void initRows(CTWorksheet worksheetParam) {
_rows = new TreeMap<Integer, XSSFRow>();
tables = new TreeMap<String, XSSFTable>();
sharedFormulas = new HashMap<Integer, CTCellFormula>();
arrayFormulas = new ArrayList<CellRangeAddress>();
- for (CTRow row : worksheet.getSheetData().getRowArray()) {
+ for (CTRow row : worksheetParam.getSheetData().getRowArray()) {
XSSFRow r = new XSSFRow(row, this);
_rows.put(r.getRowNum(), r);
}
}
return drawing;
}
-
+
protected CTDrawing getCTDrawing() {
return worksheet.getDrawing();
}
} else {
if(pane.isSetYSplit()) pane.unsetYSplit();
}
-
+
pane.setState(STPaneState.FROZEN);
if (rowSplit == 0) {
pane.setTopLeftCell(new CellReference(0, leftmostColumn).formatAsString());
public boolean getProtect() {
return worksheet.isSetSheetProtection() && sheetProtectionEnabled();
}
-
+
/**
* Enables sheet protection and sets the password for the sheet.
* Also sets some attributes on the {@link CTSheetProtection} that correspond to
* the default values used by Excel
- *
+ *
* @param password to set for protection. Pass <code>null</code> to remove protection
*/
public void protectSheet(String password) {
-
+
if(password != null) {
CTSheetProtection sheetProtection = worksheet.addNewSheetProtection();
sheetProtection.xsetPassword(stringToExcelPassword(password));
/**
* Converts a String to a {@link STUnsignedShortHex} value that contains the {@link PasswordRecord#hashPassword(String)}
* value in hexadecimal format
- *
+ *
* @param password the password string you wish convert to an {@link STUnsignedShortHex}
* @return {@link STUnsignedShortHex} that contains Excel hashed password in Hex format
*/
hexPassword.setStringValue(String.valueOf(HexDump.shortToHex(PasswordRecord.hashPassword(password))).substring(2));
return hexPassword;
}
-
+
/**
* Returns the logical row ( 0-based). If you ask for a row that is not
* defined you get a null. This is to say row 4 represents the fifth row on a sheet.
worksheet.setColsArray(0,ctCols);
setSheetFormatPrOutlineLevelCol();
}
-
+
/**
* Do not leave the width attribute undefined (see #52186).
*/
/**
* Whether Excel will be asked to recalculate all formulas when the
- * workbook is opened.
+ * workbook is opened.
*/
public boolean getForceFormulaRecalculation() {
if(worksheet.isSetSheetCalcPr()) {
}
return false;
}
-
+
/**
* @return an iterator of the PHYSICAL rows. Meaning the 3rd element may not
* be the third row if say for instance the second row is undefined.
*
* @param toprow the top row to show in desktop window pane
* @param leftcol the left column to show in desktop window pane
- *
- * @deprecated Use the version of showInPane() with ints as there can be more than 32767 rows.
+ *
+ * @deprecated Use the version of showInPane() with ints as there can be more than 32767 rows.
*/
- public void showInPane(short toprow, short leftcol) {
+ @Deprecated
+ public void showInPane(short toprow, short leftcol) {
showInPane((int)toprow, (int)leftcol);
}
if(worksheet.sizeOfColsArray() == 1) {
CTCols col = worksheet.getColsArray(0);
- if(col.sizeOfColArray() == 0) {
- worksheet.setColsArray(null);
- } else {
+ if(col.sizeOfColArray() != 0) {
setColWidthAttribute(col);
- }
+ } /*else {
+ remove, see Bug 52233: worksheet.setColsArray(null);
+ }*/
}
// Now re-generate our CTHyperlinks, if needed
public DataValidationHelper getDataValidationHelper() {
return dataValidationHelper;
}
-
+
@SuppressWarnings("deprecation") //YK: getXYZArray() array accessors are deprecated in xmlbeans with JDK 1.5 support
public List<XSSFDataValidation> getDataValidations() {
List<XSSFDataValidation> xssfValidations = new ArrayList<XSSFDataValidation>();
if( dataValidations!=null && dataValidations.getCount() > 0 ) {
for (CTDataValidation ctDataValidation : dataValidations.getDataValidationArray()) {
CellRangeAddressList addressList = new CellRangeAddressList();
-
+
@SuppressWarnings("unchecked")
List<String> sqref = ctDataValidation.getSqref();
for (String stRef : sqref) {
}
public void addValidationData(DataValidation dataValidation) {
- XSSFDataValidation xssfDataValidation = (XSSFDataValidation)dataValidation;
+ XSSFDataValidation xssfDataValidation = (XSSFDataValidation)dataValidation;
CTDataValidations dataValidations = worksheet.getDataValidations();
if( dataValidations==null ) {
dataValidations = worksheet.addNewDataValidations();
XSSFName name = wb.getBuiltInName(XSSFName.BUILTIN_FILTER_DB, sheetIndex);
if (name == null) {
name = wb.createBuiltInName(XSSFName.BUILTIN_FILTER_DB, sheetIndex);
- name.getCTName().setHidden(true);
+ 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();
return new XSSFAutoFilter(this);
}
-
+
/**
* Creates a new Table, and associates it with this Sheet
*/
if(! worksheet.isSetTableParts()) {
worksheet.addNewTableParts();
}
-
+
CTTableParts tblParts = worksheet.getTableParts();
CTTablePart tbl = tblParts.addNewTablePart();
-
+
// Table numbers need to be unique in the file, not just
// unique within the sheet. Find the next one
int tableNumber = getPackagePart().getPackage().getPartsByContentType(XSSFRelation.TABLE.getContentType()).size() + 1;
XSSFTable table = (XSSFTable)createRelationship(XSSFRelation.TABLE, XSSFFactory.getInstance(), tableNumber);
tbl.setId(table.getPackageRelationship().getId());
-
+
tables.put(tbl.getId(), table);
-
+
return table;
}
-
+
/**
* Returns any tables associated with this Sheet
*/
color.setIndexed(colorIndex);
pr.setTabColor(color);
}
-
-
+
+
public CellRangeAddress getRepeatingRows() {
return getRepeatingRowsOrColums(true);
}
setRepeatingRowsAndColumns(rowRangeRef, columnRangeRef);
}
-
+
public void setRepeatingColumns(CellRangeAddress columnRangeRef) {
CellRangeAddress rowRangeRef = getRepeatingRows();
setRepeatingRowsAndColumns(rowRangeRef, columnRangeRef);
}
-
+
private void setRepeatingRowsAndColumns(
CellRangeAddress rowDef, CellRangeAddress colDef) {
- int col1 = -1;
+ int col1 = -1;
int col2 = -1;
- int row1 = -1;
+ int row1 = -1;
int row2 = -1;
-
+
if (rowDef != null) {
row1 = rowDef.getFirstRow();
row2 = rowDef.getLastRow();
- if ((row1 == -1 && row2 != -1)
+ if ((row1 == -1 && row2 != -1)
|| row1 < -1 || row2 < -1 || row1 > row2) {
throw new IllegalArgumentException("Invalid row range specification");
}
if (colDef != null) {
col1 = colDef.getFirstColumn();
col2 = colDef.getLastColumn();
- if ((col1 == -1 && col2 != -1)
+ if ((col1 == -1 && col2 != -1)
|| col1 < -1 || col2 < -1 || col1 > col2) {
throw new IllegalArgumentException(
"Invalid column range specification");
}
}
-
+
int sheetIndex = getWorkbook().getSheetIndex(this);
boolean removeAll = rowDef == null && colDef == null;
private static String getReferenceBuiltInRecord(
String sheetName, int startC, int endC, int startR, int endR) {
- // Excel example for built-in title:
+ // Excel example for built-in title:
// 'second sheet'!$E:$F,'second sheet'!$2:$3
-
- CellReference colRef =
+
+ CellReference colRef =
new CellReference(sheetName, 0, startC, true, true);
- CellReference colRef2 =
+ CellReference colRef2 =
new CellReference(sheetName, 0, endC, true, true);
- CellReference rowRef =
+ CellReference rowRef =
new CellReference(sheetName, startR, 0, true, true);
- CellReference rowRef2 =
+ CellReference rowRef2 =
new CellReference(sheetName, endR, 0, true, true);
String escapedName = SheetNameFormatter.format(sheetName);
if(startC == -1 && endC == -1) {
} else {
- c = escapedName + "!$" + colRef.getCellRefParts()[2]
+ c = escapedName + "!$" + colRef.getCellRefParts()[2]
+ ":$" + colRef2.getCellRefParts()[2];
}
if (startR == -1 && endR == -1) {
-
- } else if (!rowRef.getCellRefParts()[1].equals("0")
+
+ } else if (!rowRef.getCellRefParts()[1].equals("0")
&& !rowRef2.getCellRefParts()[1].equals("0")) {
- r = escapedName + "!$" + rowRef.getCellRefParts()[1]
+ r = escapedName + "!$" + rowRef.getCellRefParts()[1]
+ ":$" + rowRef2.getCellRefParts()[1];
}
int maxColIndex = SpreadsheetVersion.EXCEL2007.getLastColumnIndex();
for (String part : parts) {
CellRangeAddress range = CellRangeAddress.valueOf(part);
- if ((range.getFirstColumn() == 0
+ if ((range.getFirstColumn() == 0
&& range.getLastColumn() == maxColIndex)
- || (range.getFirstColumn() == -1
+ || (range.getFirstColumn() == -1
&& range.getLastColumn() == -1)) {
if (rows) {
return range;
}
- } else if (range.getFirstRow() == 0
+ } else if (range.getFirstRow() == 0
&& range.getLastRow() == maxRowIndex
- || (range.getFirstRow() == -1
+ || (range.getFirstRow() == -1
&& range.getLastRow() == -1)) {
if (!rows) {
return range;
package org.apache.poi.xssf.usermodel;
+import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import org.apache.poi.POIXMLProperties;
import org.apache.poi.hssf.HSSFTestDataSamples;
-import org.apache.poi.openxml4j.opc.*;
+import org.apache.poi.openxml4j.opc.ContentTypes;
+import org.apache.poi.openxml4j.opc.OPCPackage;
+import org.apache.poi.openxml4j.opc.PackagePart;
+import org.apache.poi.openxml4j.opc.PackagePartName;
+import org.apache.poi.openxml4j.opc.PackagingURIHelper;
import org.apache.poi.openxml4j.opc.internal.PackagePropertiesPart;
-import org.apache.poi.ss.usermodel.*;
+import org.apache.poi.ss.usermodel.BaseTestWorkbook;
+import org.apache.poi.ss.usermodel.CellStyle;
+import org.apache.poi.ss.usermodel.Font;
+import org.apache.poi.ss.usermodel.IndexedColors;
+import org.apache.poi.ss.usermodel.RichTextString;
+import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.util.TempFile;
import org.apache.poi.xssf.XSSFITestDataProvider;
import org.apache.poi.xssf.XSSFTestDataSamples;
sheet1 = workbook.getSheetAt(0);
assertEquals(1.2, sheet1.getRow(0).getCell(0).getNumericCellValue(), 0.0001);
assertEquals("hello world", sheet1.getRow(1).getCell(0).getRichStringCellValue().getString());
+
+ pkg.close();
}
public void testExisting() throws Exception {
assertTrue(wbPart.hasRelationships());
assertEquals(6, wbPart.getRelationships().size());
+ pkg.close();
}
public void testGetCellStyleAt(){
assertEquals("Numbers", wb.getSheetName(0));
assertEquals("Chart", wb.getSheetName(1));
}
-
+
/**
* Problems with the count of the number of styles
* coming out wrong
assertEquals(IndexedColors.RED.index,
sh.getCTWorksheet().getSheetPr().getTabColor().getIndexed());
}
+
+ public void testColumnWidthPOI52233() throws Exception {
+ XSSFWorkbook workbook = new XSSFWorkbook();
+ XSSFSheet sheet = workbook.createSheet();
+ XSSFRow row = sheet.createRow(0);
+ XSSFCell cell = row.createCell(0);
+ cell.setCellValue("hello world");
+ assertEquals("hello world", workbook.getSheetAt(0).getRow(0).getCell(0).getStringCellValue());
+ assertEquals(2048, workbook.getSheetAt(0).getColumnWidth(0)); // <-works
+
+ ByteArrayOutputStream stream = new ByteArrayOutputStream();
+ try {
+ workbook.write(stream);
+ } finally {
+ stream.close();
+ }
+
+ assertEquals("hello world", workbook.getSheetAt(0).getRow(0).getCell(0).getStringCellValue());
+ assertEquals(2048, workbook.getSheetAt(0).getColumnWidth(0)); // <- did throw IndexOutOfBoundsException before fixing the bug
+ }
}