1. implemented XSSFSheet.autosizeColumn(), for now mostly duplicated HSSF code, will be refactored in future.2. fixed bug #45974: XSSFCell.getCellStyle can return null3. more code cleanup and reaftoring, removed usages of obsolete XSSFCell.getCellNum() in favor of XSSFCell.getColumnIndex(), also more javadoc in core classes

4. fixed a blocker: calling XSSFSheet.getNumMergedRegions() resulted in unreadable workbook, this methods structurally modified worksheet and added unnecessary data 

git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@706691 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yegor Kozlov 2008-10-21 17:56:34 +00:00
parent 82a4bc8cb1
commit b15942d4a8
25 changed files with 448 additions and 296 deletions

View File

@ -92,7 +92,7 @@ row.getRowNum() %> </b>
%>
<%= "CELL col="
+ cell.getCellNum()
+ cell.getColumnIndex()
+ " VALUE=" + value %>
<%
}

View File

@ -143,7 +143,7 @@ public class FromQuickGuide {
public static void getCellContents(Sheet sheet) {
for (Row row : sheet) {
for (Cell cell : row) {
CellReference cellRef = new CellReference(row.getRowNum(), cell.getCellNum());
CellReference cellRef = new CellReference(row.getRowNum(), cell.getColumnIndex());
System.out.print(cellRef.formatAsString());
System.out.print(" - ");

View File

@ -28,7 +28,7 @@ import org.apache.poi.ss.usermodel.Row;
public class IterateCells {
public static void main(String[] args) throws Exception {
Workbook wb = new XSSFWorkbook(args[0]);
Workbook wb = null;
for (int i = 0; i < wb.getNumberOfSheets(); i++) {
Sheet sheet = wb.getSheetAt(i);
System.out.println(wb.getSheetName(i));

View File

@ -45,12 +45,12 @@ public interface Row extends Iterable<Cell> {
*
* @param column - the column number this cell represents
*
* @return HSSFCell a high level representation of the created cell.
* @return Cell a high level representation of the created cell.
*/
Cell createCell(int column, int type);
/**
* remove the HSSFCell from this row.
* remove the Cell from this row.
* @param cell to remove
*/
void removeCell(Cell cell);
@ -71,7 +71,7 @@ public interface Row extends Iterable<Cell> {
int getRowNum();
/**
* get the hssfcell representing a given column (logical cell) 0-based. If you
* get the cell representing a given column (logical cell) 0-based. If you
* ask for a cell that is not defined....you get a null.
*
* @param cellnum 0 based column number
@ -80,7 +80,7 @@ public interface Row extends Iterable<Cell> {
Cell getCell(int cellnum);
/**
* Get the hssfcell representing a given column (logical cell)
* Get the cell representing a given column (logical cell)
* 0-based. If you ask for a cell that is not defined, then
* your supplied policy says what to do
*
@ -158,12 +158,6 @@ public interface Row extends Iterable<Cell> {
*/
Iterator<Cell> cellIterator();
/**
* Alias for {@link #cellIterator()} to allow
* foreach loops
*/
Iterator<Cell> iterator();
/**
* Used to specify the different possible policies
* if for the case of null and blank cells

View File

@ -19,7 +19,6 @@ package org.apache.poi.ss.usermodel;
import java.util.Iterator;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.util.PaneInformation;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.Region;
@ -27,13 +26,17 @@ import org.apache.poi.ss.util.Region;
public interface Sheet extends Iterable<Row> {
/* Constants for margins */
public static final short LeftMargin = Sheet.LeftMargin;
public static final short LeftMargin = 0;
public static final short RightMargin = Sheet.RightMargin;
public static final short RightMargin = 1;
public static final short TopMargin = Sheet.TopMargin;
public static final short TopMargin = 2;
public static final short BottomMargin = Sheet.BottomMargin;
public static final short BottomMargin = 3;
public static final short HeaderMargin = 4;
public static final short FooterMargin = 5;
public static final byte PANE_LOWER_RIGHT = (byte) 0;
@ -57,7 +60,7 @@ public interface Sheet extends Iterable<Row> {
* @param rownum row number
* @return High level HSSFRow object representing a row in the sheet
* @see org.apache.poi.hssf.usermodel.HSSFRow
* @see #removeRow(HSSFRow)
* @see #removeRow(Row)
*/
Row createRow(int rownum);
@ -246,12 +249,6 @@ public interface Sheet extends Iterable<Row> {
* be the third row if say for instance the second row is undefined.
*/
Iterator<Row> rowIterator();
/**
* Alias for {@link #rowIterator()} to allow
* foreach loops
*/
Iterator<Row> iterator();
/**
* whether alternate expression evaluation is on
@ -274,13 +271,6 @@ public interface Sheet extends Iterable<Row> {
void setAutobreaks(boolean b);
/**
* set whether sheet is a dialog sheet or not
* @param b isDialog or not
*/
void setDialog(boolean b);
/**
* set whether to display the guts or not
*
@ -331,13 +321,6 @@ public interface Sheet extends Iterable<Row> {
boolean getAutobreaks();
/**
* get whether sheet is a dialog sheet or not
* @return isDialog or not
*/
boolean getDialog();
/**
* get whether to display the guts or not
*

View File

@ -171,22 +171,6 @@ public interface Workbook {
void removeSheetAt(int index);
/**
* determine whether the Excel GUI will backup the workbook when saving.
*
* @param backupValue true to indicate a backup will be performed.
*/
void setBackupFlag(boolean backupValue);
/**
* determine whether the Excel GUI will backup the workbook when saving.
*
* @return the current setting for backups.
*/
boolean getBackupFlag();
/**
* Sets the repeating rows and columns for a sheet (as found in
* File->PageSetup->Sheet). This is function is included in the workbook
@ -337,7 +321,7 @@ public interface Workbook {
* Sets the policy on what to do when
* getting missing or blank cells from a row.
* This will then apply to all calls to
* {@link Row.getCell()}. See
* {@link Row#getCell(int)} }. See
* {@link MissingCellPolicy}
*/
void setMissingCellPolicy(MissingCellPolicy missingCellPolicy);
@ -391,22 +375,10 @@ public interface Workbook {
*/
List getAllPictures();
/**
* protect a workbook with a password (not encypted, just sets writeprotect
* flags and the password.
* @param password to set
*/
void writeProtectWorkbook(String password, String username);
/**
* removes the write protect flag
*/
void unwriteProtectWorkbook();
/**
* Gets all embedded OLE2 objects from the Workbook.
*
* @return the list of embedded objects (a list of {@link HSSFObjectData} objects.)
* @return the list of embedded objects
*/
List getAllEmbeddedObjects();

View File

@ -23,5 +23,14 @@ package org.apache.poi.xssf.usermodel;
* @author Yegor Kozlov
*/
public abstract class XSSFAnchor {
public abstract int getDx1();
public abstract void setDx1( int dx1 );
public abstract int getDy1();
public abstract void setDy1( int dy1 );
public abstract int getDy2();
public abstract void setDy2( int dy2 );
public abstract int getDx2();
public abstract void setDx2( int dx2 );
}

View File

@ -90,7 +90,7 @@ public final class XSSFCell implements Cell {
}
public Comment getCellComment() {
return row.getSheet().getCellComment(row.getRowNum(), getCellNum());
return row.getSheet().getCellComment(row.getRowNum(), getColumnIndex());
}
public String getCellFormula() {
@ -113,12 +113,15 @@ public final class XSSFCell implements Cell {
return row.getRowNum();
}
/**
* Return the cell's style.
*
* @return the cell's style. Always not-null. Default cell style has zero index and can be obtained as
* <code>workbook.getCellStyleAt(0)</code>
*/
public XSSFCellStyle getCellStyle() {
// Zero is the empty default
if(this.cell.getS() > 0) {
return stylesSource.getStyleAt(this.cell.getS());
}
return null;
long idx = cell.isSetS() ? cell.getS() : 0;
return stylesSource.getStyleAt(idx);
}
public int getCellType() {
@ -156,7 +159,7 @@ public final class XSSFCell implements Cell {
public Date getDateCellValue() {
if (STCellType.N == this.cell.getT() || STCellType.STR == this.cell.getT()) {
double value = this.getNumericCellValue();
if (false /* book.isUsing1904DateWindowing() */) { // FIXME
if (row.getSheet().getWorkbook().isDate1904()) {
return DateUtil.getJavaDate(value,true);
}
else {
@ -269,7 +272,7 @@ public final class XSSFCell implements Cell {
public void setCellComment(Comment comment) {
String cellRef = new CellReference(row.getRowNum(), getCellNum()).formatAsString();
String cellRef = new CellReference(row.getRowNum(), getColumnIndex()).formatAsString();
row.getSheet().setCellComment(cellRef, (XSSFComment)comment);
}
@ -333,7 +336,7 @@ public final class XSSFCell implements Cell {
}
protected String formatPosition() {
int col = this.getCellNum();
int col = this.getColumnIndex();
String result = Character.valueOf((char) (col % 26 + 'A')).toString();
if (col >= 26){
col = col / 26;

View File

@ -145,7 +145,12 @@ public class XSSFCellStyle implements CellStyle, Cloneable {
* @return HorizontalAlignment - the type of alignment
*/
public HorizontalAlignment getAlignmentEnum() {
return getCellAlignment().getHorizontal();
CTCellAlignment align = cellXf.getAlignment();
if(align != null && align.isSetHorizontal()) {
return HorizontalAlignment.values()[align.getHorizontal().intValue()-1];
} else {
return HorizontalAlignment.GENERAL;
}
}
/**
@ -485,7 +490,8 @@ public class XSSFCellStyle implements CellStyle, Cloneable {
* @return indent - number of spaces
*/
public short getIndention() {
return (short) getCellAlignment().getIndent();
CTCellAlignment align = cellXf.getAlignment();
return (short)(align == null ? 0 : align.getIndent());
}
/**
@ -571,7 +577,8 @@ public class XSSFCellStyle implements CellStyle, Cloneable {
* @return rotation degrees (between 0 and 180 degrees)
*/
public short getRotation() {
return (short) getCellAlignment().getTextRotation();
CTCellAlignment align = cellXf.getAlignment();
return (short)(align == null ? 0 : align.getTextRotation());
}
/**
@ -619,7 +626,12 @@ public class XSSFCellStyle implements CellStyle, Cloneable {
* @see VerticalAlignment
*/
public VerticalAlignment getVerticalAlignmentEnum() {
return getCellAlignment().getVertical();
CTCellAlignment align = cellXf.getAlignment();
if(align != null && align.isSetVertical()) {
return VerticalAlignment.values()[align.getVertical().intValue()-1];
} else {
return VerticalAlignment.BOTTOM;
}
}
/**
@ -628,7 +640,8 @@ public class XSSFCellStyle implements CellStyle, Cloneable {
* @return a boolean value indicating if the text in a cell should be line-wrapped within the cell.
*/
public boolean getWrapText() {
return getCellAlignment().getWrapText();
CTCellAlignment align = cellXf.getAlignment();
return align != null && align.getWrapText();
}
/**

View File

@ -27,6 +27,7 @@ import org.openxml4j.opc.PackageRelationship;
import java.io.IOException;
public class XSSFDialogsheet extends XSSFSheet implements Sheet{
protected CTDialogsheet dialogsheet;
public XSSFDialogsheet(XSSFSheet sheet) {
this.packagePart = sheet.getPackagePart();
@ -96,4 +97,7 @@ public class XSSFDialogsheet extends XSSFSheet implements Sheet{
return dialogsheet.getSheetProtection();
}
public boolean getDialog(){
return true;
}
}

View File

@ -58,7 +58,7 @@ final class XSSFEvaluationCell implements EvaluationCell {
return _cell.getCellType();
}
public int getColumnIndex() {
return _cell.getCellNum();
return _cell.getColumnIndex();
}
public int getErrorCellValue() {
return _cell.getErrorCellValue();

View File

@ -488,9 +488,7 @@ public class XSSFFont implements Font {
public String toString() {
return "org.apache.poi.xssf.usermodel.XSSFFont{" +
ctFont +
"}";
return ctFont.toString();
}

View File

@ -33,13 +33,13 @@ public class XSSFName implements Name {
/**
* this defined name specifies the workbook's print area
*/
public final static String BUILTIN_PRINT_AREA = "_xlnm.Print_Area";
public final static String BUILTIN_PRINT_AREA = "_xlnm.Print_Area";
/**
* this defined name specifies the row(s) or column(s) to repeat
* at the top of each printed page.
* at the top of each printed page.
*/
public final static String BUILTIN_PRINT_TITLE = "_xlnm.Print_Titles";
public final static String BUILTIN_PRINT_TITLE = "_xlnm.Print_Titles";
//Filter & Advanced Filter
@ -47,7 +47,7 @@ public class XSSFName implements Name {
* this defined name refers to a range containing the criteria values
* to be used in applying an advanced filter to a range of data
*/
public final static String BUILTIN_CRITERIA = "_xlnm.Criteria:";
public final static String BUILTIN_CRITERIA = "_xlnm.Criteria:";
/**
@ -55,7 +55,7 @@ public class XSSFName implements Name {
* output values resulting from applying an advanced filter criteria to a source
* range
*/
public final static String BUILTIN_EXTRACT = "_xlnm.Extract:";
public final static String BUILTIN_EXTRACT = "_xlnm.Extract:";
/**
* can be one of the following
@ -64,123 +64,127 @@ public class XSSFName implements Name {
* b. This defined name refers to a range to which an AutoFilter has been
* applied
*/
public final static String BUILTIN_FILTER_DB = "_xlnm._FilterDatabase:";
public final static String BUILTIN_FILTER_DB = "_xlnm._FilterDatabase:";
//Miscellaneous
/**
* the defined name refers to a consolidation area
*/
public final static String BUILTIN_CONSOLIDATE_AREA = "_xlnm.Consolidate_Area";
public final static String BUILTIN_CONSOLIDATE_AREA = "_xlnm.Consolidate_Area";
/**
* the range specified in the defined name is from a database data source
*/
public final static String BUILTIN_DATABASE = "_xlnm.Database";
public final static String BUILTIN_DATABASE = "_xlnm.Database";
/**
* the defined name refers to a sheet title.
*/
public final static String BUILTIN_SHEET_TITLE = "_xlnm.Sheet_Title";
public final static String BUILTIN_SHEET_TITLE = "_xlnm.Sheet_Title";
private XSSFWorkbook workbook;
private CTDefinedName ctName;
protected XSSFName(XSSFWorkbook workbook) {
this.workbook = workbook;
this.ctName = CTDefinedName.Factory.newInstance();
this.workbook = workbook;
this.ctName = CTDefinedName.Factory.newInstance();
}
protected XSSFName(CTDefinedName name, XSSFWorkbook workbook) {
this.workbook = workbook;
this.ctName = name;
this.workbook = workbook;
this.ctName = name;
}
public boolean isFunctionName() {
// TODO Figure out how HSSF does this, and do the same!
return ctName.getFunction(); // maybe this works - verify
// TODO Figure out how HSSF does this, and do the same!
return ctName.getFunction(); // maybe this works - verify
}
/**
* Returns the underlying named range object
*/
protected CTDefinedName getCTName() {
return ctName;
return ctName;
}
public String getNameName() {
return ctName.getName();
return ctName.getName();
}
public void setNameName(String nameName) {
ctName.setName(nameName);
ctName.setName(nameName);
}
public String getReference() {
return ctName.getStringValue();
return ctName.getStringValue();
}
public void setReference(String ref) {
ctName.setStringValue(ref);
ctName.setStringValue(ref);
}
public void setLocalSheetId(int sheetId) {
ctName.setLocalSheetId(sheetId);
ctName.setLocalSheetId(sheetId);
}
public int getLocalSheetId() {
return (int)ctName.getLocalSheetId();
return (int) ctName.getLocalSheetId();
}
public void setFunction(boolean value) {
ctName.setFunction(value);
ctName.setFunction(value);
}
public boolean getFunction() {
return ctName.getFunction();
return ctName.getFunction();
}
public void setFunctionGroupId(int functionGroupId) {
ctName.setFunctionGroupId(functionGroupId);
ctName.setFunctionGroupId(functionGroupId);
}
public int getFunctionGroupId() {
return (int)ctName.getFunctionGroupId();
return (int) ctName.getFunctionGroupId();
}
public String getSheetName() {
if(ctName.isSetLocalSheetId()) {
// Given as explicit sheet id
long sheetId = ctName.getLocalSheetId();
if(sheetId >= 0) {
return workbook.getSheetName((int)sheetId);
}
} else {
// Is it embeded in the reference itself?
int excl = getReference().indexOf('!');
if(excl > -1) {
return getReference().substring(0, excl);
}
}
if (ctName.isSetLocalSheetId()) {
// Given as explicit sheet id
long sheetId = ctName.getLocalSheetId();
if (sheetId >= 0) {
return workbook.getSheetName((int) sheetId);
}
} else {
// Is it embeded in the reference itself?
int excl = getReference().indexOf('!');
if (excl > -1) {
return getReference().substring(0, excl);
}
}
// Not given at all
return null;
// Not given at all
return null;
}
public String getComment() {
return ctName.getComment();
return ctName.getComment();
}
public void setComment(String comment) {
ctName.setComment(comment);
ctName.setComment(comment);
}
public int hashCode(){
return ctName.toString().hashCode();
public int hashCode() {
return ctName.toString().hashCode();
}
public boolean equals(Object o){
if(!(o instanceof XSSFName)) return false;
XSSFName cf = (XSSFName)o;
return ctName.toString().equals(cf.getCTName().toString());
public boolean equals(Object o) {
if (!(o instanceof XSSFName)) return false;
XSSFName cf = (XSSFName) o;
return ctName.toString().equals(cf.getCTName().toString());
}

View File

@ -47,12 +47,6 @@ public class XSSFPicture extends XSSFShape {
*/
private static CTPicture prototype = null;
/**
* Width of one character in pixels. Same for Calibry and Arial.
*/
private static final float CHARACTER_WIDTH = 7.0017f;
/**
* This object specifies a picture object and all its properties
*/
@ -214,7 +208,7 @@ public class XSSFPicture extends XSSFShape {
XSSFSheet sheet = (XSSFSheet)getDrawing().getParent();
float numChars = (float)sheet.getColumnWidth(columnIndex)/256;
return numChars*CHARACTER_WIDTH;
return numChars*XSSFWorkbook.DEFAULT_CHARACTER_WIDTH;
}
private float getRowHeightInPixels(int rowIndex){

View File

@ -453,6 +453,7 @@ public class XSSFRichTextString implements RichTextString {
if(c1.isSetTheme()) c2.setTheme(c1.getTheme());
if(c1.isSetTint()) c2.setTint(c1.getTint());
}
if(pr.sizeOfSzArray() > 0) ctFont.addNewSz().setVal(pr.getSzArray(0).getVal());
if(pr.sizeOfRFontArray() > 0) ctFont.addNewName().setVal(pr.getRFontArray(0).getVal());
if(pr.sizeOfFamilyArray() > 0) ctFont.addNewFamily().setVal(pr.getFamilyArray(0).getVal());
if(pr.sizeOfSchemeArray() > 0) ctFont.addNewScheme().setVal(pr.getSchemeArray(0).getVal());

View File

@ -27,7 +27,9 @@ import org.apache.poi.ss.usermodel.Row;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRow;
/**
* High level representation of a row of a spreadsheet.
*/
public class XSSFRow implements Row, Comparable {
private CTRow row;
@ -45,20 +47,30 @@ public class XSSFRow implements Row, Comparable {
public XSSFRow(CTRow row, XSSFSheet sheet) {
this.row = row;
this.sheet = sheet;
this.cells = new LinkedList<Cell>();
this.cells = new LinkedList<Cell>();
for (CTCell c : row.getCArray()) {
this.cells.add(new XSSFCell(this, c));
}
}
/**
* Returns the XSSFSheet this row belongs to
*
* @return the XSSFSheet that owns this row
*/
public XSSFSheet getSheet() {
return this.sheet;
}
/**
* @return Cell iterator of the physically defined cells. Note element 4 may
* actually be row cell depending on how many are defined!
*/
public Iterator<Cell> cellIterator() {
return cells.iterator();
}
/**
* Alias for {@link #cellIterator()} to allow
* foreach loops
@ -102,7 +114,7 @@ public class XSSFRow implements Row, Comparable {
*
* @param column Cell column number.
* @param index Position where to insert cell.
* @param type TODO
* @param type cell type, one of Cell.CELL_TYPE_*
* @return The new cell.
*/
protected XSSFCell addCell(int column, int index, int type) {
@ -148,7 +160,7 @@ public class XSSFRow implements Row, Comparable {
Iterator<Cell> it = cellIterator();
for ( ; it.hasNext() ; ) {
Cell cell = it.next();
if (cell.getCellNum() == cellnum) {
if (cell.getColumnIndex() == cellnum) {
return (XSSFCell)cell;
}
}
@ -157,8 +169,9 @@ public class XSSFRow implements Row, Comparable {
/**
* Returns the cell at the given (0 based) index,
* with the {@link MissingCellPolicy} from the
* parent Workbook.
* with the {@link MissingCellPolicy} from the parent Workbook.
*
* @return the cell at the given (0 based) index
*/
public XSSFCell getCell(int cellnum) {
return getCell(cellnum, sheet.getWorkbook().getMissingCellPolicy());
@ -167,6 +180,8 @@ public class XSSFRow implements Row, Comparable {
/**
* Returns the cell at the given (0 based) index,
* with the specified {@link MissingCellPolicy}
*
* @return the cell at the given (0 based) index
*/
public XSSFCell getCell(int cellnum, MissingCellPolicy policy) {
XSSFCell cell = retrieveCell(cellnum);
@ -189,11 +204,16 @@ public class XSSFRow implements Row, Comparable {
throw new IllegalArgumentException("Illegal policy " + policy + " (" + policy.id + ")");
}
/**
* Get the number of the first cell contained in this row.
*
* @return short representing the first logical cell in the row, or -1 if the row does not contain any cells.
*/
public short getFirstCellNum() {
for (Iterator<Cell> it = cellIterator() ; it.hasNext() ; ) {
Cell cell = it.next();
if (cell != null) {
return cell.getCellNum();
return (short)cell.getColumnIndex();
}
}
return -1;
@ -248,12 +268,18 @@ public class XSSFRow implements Row, Comparable {
for (Iterator<Cell> it = cellIterator() ; it.hasNext() ; ) {
Cell cell = it.next();
if (cell != null) {
lastCellNum = (short)(cell.getCellNum() + 1);
lastCellNum = (short)(cell.getColumnIndex() + 1);
}
}
return lastCellNum;
}
/**
* Gets the number of defined cells (NOT number of cells in the actual row!).
* That is to say if only columns 0,4,5 have values then there would be 3.
*
* @return int representing the number of defined cells in the row.
*/
public int getPhysicalNumberOfCells() {
int count = 0;
for (Iterator<Cell> it = cellIterator() ; it.hasNext() ; ) {
@ -264,19 +290,34 @@ public class XSSFRow implements Row, Comparable {
return count;
}
/**
* Get row number this row represents
*
* @return the row number (0 based)
*/
public int getRowNum() {
return (int) (row.getR() - 1);
}
/**
* Get whether or not to display this row with 0 height
*
* @return - height is zero or not.
*/
public boolean getZeroHeight() {
return this.row.getHidden();
}
/**
* Remove the Cell from this row.
*
* @param cell to remove
*/
public void removeCell(Cell cell) {
int counter = 0;
for (Iterator<Cell> it = cellIterator(); it.hasNext(); ) {
Cell c = it.next();
if (c.getCellNum() == cell.getCellNum()) {
if (c.getColumnIndex() == cell.getColumnIndex()) {
it.remove();
row.removeC(counter);
continue;
@ -309,16 +350,31 @@ public class XSSFRow implements Row, Comparable {
setHeight((short)(height*20));
}
/**
* Set the row number of this row.
*
* @param rowNum the row number (0-based)
*/
public void setRowNum(int rowNum) {
this.row.setR(rowNum + 1);
}
/**
* Set whether or not to display this row with 0 height
*
* @param height height is zero or not.
*/
public void setZeroHeight(boolean height) {
this.row.setHidden(height);
}
/**
* Returns the underlying CTRow xml bean representing this row
*
* @return the underlying CTRow bean
*/
public CTRow getCTRow(){
return this.row;
}

View File

@ -71,20 +71,10 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
protected CTSheet sheet;
protected CTWorksheet worksheet;
protected CTDialogsheet dialogsheet;
protected List<Row> rows;
protected List<XSSFHyperlink> hyperlinks;
protected ColumnHelper columnHelper;
private CommentsSource sheetComments;
protected CTMergeCells ctMergeCells;
public static final short LeftMargin = 0;
public static final short RightMargin = 1;
public static final short TopMargin = 2;
public static final short BottomMargin = 3;
public static final short HeaderMargin = 4;
public static final short FooterMargin = 5;
public XSSFSheet() {
super(null, null);
@ -203,7 +193,9 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
public int addMergedRegion(Region region) {
addNewMergeCell(region);
CTMergeCells ctMergeCells = worksheet.isSetMergeCells() ? worksheet.getMergeCells() : worksheet.addNewMergeCells();
CTMergeCell ctMergeCell = ctMergeCells.addNewMergeCell();
ctMergeCell.setRef(region.getRegionRef());
return ctMergeCells.sizeOfMergeCellArray();
}
@ -234,8 +226,12 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
* @param useMergedCells whether to use the contents of merged cells when calculating the width of the column
*/
public void autoSizeColumn(short column, boolean useMergedCells) {
//TODO:
columnHelper.setColBestFit(column, true);
double width = ColumnHelper.getColumnWidth(this, column, useMergedCells);
if(width != -1){
columnHelper.setColBestFit(column, true);
columnHelper.setCustomWidth(column, true);
columnHelper.setColWidth(column, width);
}
}
/**
@ -474,14 +470,6 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
worksheet.addNewSheetFormatPr();
}
public boolean getDialog() {
if (dialogsheet != null) {
return true;
}
return false;
}
/**
* Get whether to display the guts or not,
* default value is true
@ -677,12 +665,30 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
}
public Region getMergedRegionAt(int index) {
CTMergeCell ctMergeCell = getMergedCells().getMergeCellArray(index);
CTMergeCells ctMergeCells = worksheet.getMergeCells();
if(ctMergeCells == null) throw new IllegalStateException("This worksheet does not contain merged regions");
CTMergeCell ctMergeCell = ctMergeCells.getMergeCellArray(index);
return new Region(ctMergeCell.getRef());
}
/**
* @return the merged region at the specified index
*/
public CellRangeAddress getMergedRegion(int index) {
CTMergeCells ctMergeCells = worksheet.getMergeCells();
if(ctMergeCells == null) throw new IllegalStateException("This worksheet does not contain merged regions");
CTMergeCell ctMergeCell = ctMergeCells.getMergeCellArray(index);
String ref = ctMergeCell.getRef();
CellReference cell1 = new CellReference(ref.substring(0, ref.indexOf(":")));
CellReference cell2 = new CellReference(ref.substring(ref.indexOf(":") + 1));
return new CellRangeAddress(cell1.getRow(), cell2.getRow(), cell1.getCol(), cell2.getCol());
}
public int getNumMergedRegions() {
return getMergedCells().sizeOfMergeCellArray();
CTMergeCells ctMergeCells = worksheet.getMergeCells();
return ctMergeCells == null ? 0 : ctMergeCells.sizeOfMergeCellArray();
}
public int getNumHyperlinks() {
@ -1040,17 +1046,24 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
}
/**
* Removes a merged region of cells (hence letting them free)
*
* @param index of the region to unmerge
*/
public void removeMergedRegion(int index) {
CTMergeCell[] mergeCellsArray = new CTMergeCell[getMergedCells().sizeOfMergeCellArray() - 1];
for (int i = 0 ; i < getMergedCells().sizeOfMergeCellArray() ; i++) {
CTMergeCells ctMergeCells = worksheet.getMergeCells();
CTMergeCell[] mergeCellsArray = new CTMergeCell[ctMergeCells.sizeOfMergeCellArray() - 1];
for (int i = 0 ; i < ctMergeCells.sizeOfMergeCellArray() ; i++) {
if (i < index) {
mergeCellsArray[i] = getMergedCells().getMergeCellArray(i);
mergeCellsArray[i] = ctMergeCells.getMergeCellArray(i);
}
else if (i > index) {
mergeCellsArray[i - 1] = getMergedCells().getMergeCellArray(i);
mergeCellsArray[i - 1] = ctMergeCells.getMergeCellArray(i);
}
}
getMergedCells().setMergeCellArray(mergeCellsArray);
ctMergeCells.setMergeCellArray(mergeCellsArray);
}
public void removeRow(Row row) {
@ -1192,15 +1205,6 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
}
public void setDialog(boolean b) {
if(b && dialogsheet == null){
CTDialogsheet dialogSheet = CTDialogsheet.Factory.newInstance();
dialogsheet = dialogSheet;
}else{
dialogsheet = null;
}
}
/**
* Sets the flag indicating whether this sheet should display formulas.
*
@ -1623,19 +1627,6 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
return sheetComments;
}
private void addNewMergeCell(Region region) {
ctMergeCells = getMergedCells();
CTMergeCell ctMergeCell = ctMergeCells.addNewMergeCell();
ctMergeCell.setRef(region.getRegionRef());
}
private CTMergeCells getMergedCells() {
if (ctMergeCells == null) {
ctMergeCells = worksheet.addNewMergeCells();
}
return ctMergeCells;
}
private CTPageSetUpPr getSheetTypePageSetUpPr() {
CTSheetPr sheetPr = getSheetTypeSheetPr();
return sheetPr.isSetPageSetUpPr() ? sheetPr.getPageSetUpPr() : sheetPr.addNewPageSetUpPr();
@ -1666,10 +1657,12 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
if(worksheet.getColsArray().length == 1) {
CTCols col = worksheet.getColsArray(0);
if(col.getColArray().length == 0) {
CTCol[] cols = col.getColArray();
if(cols.length == 0) {
worksheet.setColsArray(null);
}
}
// Now re-generate our CTHyperlinks, if needed
if(hyperlinks.size() > 0) {
if(worksheet.getHyperlinks() == null) {

View File

@ -51,6 +51,11 @@ import org.openxmlformats.schemas.officeDocument.x2006.relationships.STRelations
*/
public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<XSSFSheet> {
/**
* Width of one character of the default font in pixels. Same for Calibry and Arial.
*/
public static final float DEFAULT_CHARACTER_WIDTH = 7.0017f;
/**
* The underlying XML bean
*/
@ -140,7 +145,6 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
*/
private static Package ensureWriteAccess(Package pkg) throws IOException {
if(pkg.getPackageAccess() == PackageAccess.READ){
//YK: current implementation of OpenXML4J is funny.
try {
return PackageHelper.clone(pkg);
} catch (OpenXML4JException e){
@ -358,8 +362,8 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
xf.setBorderId(0);
xf.setXfId(0);
int xfSize=(stylesSource)._getStyleXfsSize();
long indexXf=(stylesSource).putCellXf(xf);
XSSFCellStyle style = new XSSFCellStyle(new Long(indexXf-1).intValue(), xfSize-1, stylesSource);
int indexXf=(stylesSource).putCellXf(xf);
XSSFCellStyle style = new XSSFCellStyle(indexXf-1, xfSize-1, stylesSource);
return style;
}
@ -513,11 +517,6 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
return pictures;
}
public boolean getBackupFlag() {
// TODO Auto-generated method stub
return false;
}
public XSSFCellStyle getCellStyleAt(short idx) {
return stylesSource.getStyleAt(idx);
}
@ -615,7 +614,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
* @return String Null if no print area has been defined
*/
public String getPrintArea(int sheetIndex) {
XSSFName name = getSpecificBuiltinRecord(XSSFName.BUILTIN_PRINT_AREA, sheetIndex);
XSSFName name = getBuiltInName(XSSFName.BUILTIN_PRINT_AREA, sheetIndex);
if (name == null) return null;
//adding one here because 0 indicates a global named region; doesnt make sense for print areas
return name.getReference();
@ -811,11 +810,6 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
}
}
public void setBackupFlag(boolean backupValue) {
// TODO Auto-generated method stub
}
/**
* Gets the first tab that is displayed in the list of tabs in excel.
*
@ -846,7 +840,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
* @param reference Valid name Reference for the Print Area
*/
public void setPrintArea(int sheetIndex, String reference) {
XSSFName name = getSpecificBuiltinRecord(XSSFName.BUILTIN_PRINT_AREA, sheetIndex);
XSSFName name = getBuiltInName(XSSFName.BUILTIN_PRINT_AREA, sheetIndex);
if (name == null) {
name = createBuiltInName(XSSFName.BUILTIN_PRINT_AREA, sheetIndex);
namedRanges.add(name);
@ -916,7 +910,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
}
public XSSFName getSpecificBuiltinRecord(String builtInCode, int sheetNumber) {
private XSSFName getBuiltInName(String builtInCode, int sheetNumber) {
for (XSSFName name : namedRanges) {
if (name.getNameName().equalsIgnoreCase(builtInCode) && name.getLocalSheetId() == sheetNumber) {
return name;
@ -929,7 +923,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
* Generates a NameRecord to represent a built-in region
* @return a new NameRecord
*/
public XSSFName createBuiltInName(String builtInName, int sheetNumber) {
private XSSFName createBuiltInName(String builtInName, int sheetNumber) {
if (sheetNumber < 0 || sheetNumber+1 > Short.MAX_VALUE) {
throw new IllegalArgumentException("Sheet number ["+sheetNumber+"]is not valid ");
}
@ -939,7 +933,6 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
nameRecord.setLocalSheetId(sheetNumber);
XSSFName name=new XSSFName(nameRecord,this);
//while(namedRanges.contains(name)) {
for(XSSFName nr : namedRanges){
if(nr.equals(name))
throw new RuntimeException("Builtin (" + builtInName
@ -993,11 +986,6 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
newcts.set(cts);
}
public void unwriteProtectWorkbook() {
// TODO Auto-generated method stub
}
/**
* marshal named ranges from the {@link #namedRanges} collection to the underlying CTWorkbook bean
*/
@ -1051,11 +1039,6 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
getPackage().save(stream);
}
public void writeProtectWorkbook(String password, String username) {
// TODO Auto-generated method stub
}
/**
* Returns SharedStringsTable - tha cache of string for this workbook
*
@ -1064,10 +1047,6 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
public SharedStringsTable getSharedStringSource() {
return this.sharedStringSource;
}
//TODO do we really need setSharedStringSource?
protected void setSharedStringSource(SharedStringsTable sharedStringSource) {
this.sharedStringSource = sharedStringSource;
}
/**
* Return a object representing a collection of shared objects used for styling content,
@ -1076,10 +1055,6 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
public StylesTable getStylesSource() {
return this.stylesSource;
}
//TODO do we really need setStylesSource?
protected void setStylesSource(StylesTable stylesSource) {
this.stylesSource = stylesSource;
}
/**
* Returns an object that handles instantiating concrete

View File

@ -18,10 +18,20 @@
package org.apache.poi.xssf.usermodel.helpers;
import java.util.Arrays;
import java.util.Iterator;
import java.text.AttributedString;
import java.text.NumberFormat;
import java.text.DecimalFormat;
import java.awt.font.TextLayout;
import java.awt.font.FontRenderContext;
import java.awt.font.TextAttribute;
import java.awt.geom.AffineTransform;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.util.CTColComparator;
import org.apache.poi.xssf.util.NumericRanges;
import org.apache.poi.xssf.usermodel.*;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCols;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet;
@ -222,6 +232,10 @@ public class ColumnHelper {
CTCol col = getOrCreateColumn1Based(index+1, false);
col.setBestFit(bestFit);
}
public void setCustomWidth(long index, boolean bestFit) {
CTCol col = getOrCreateColumn1Based(index+1, false);
col.setCustomWidth(bestFit);
}
public void setColWidth(long index, double width) {
CTCol col = getOrCreateColumn1Based(index+1, false);
@ -281,4 +295,164 @@ public class ColumnHelper {
}
return -1;
}
public static double getColumnWidth(XSSFSheet sheet, int column, boolean useMergedCells){
AttributedString str;
TextLayout layout;
/**
* Excel measures columns in units of 1/256th of a character width
* but the docs say nothing about what particular character is used.
* '0' looks to be a good choice.
*/
char defaultChar = '0';
/**
* This is the multiple that the font height is scaled by when determining the
* boundary of rotated text.
*/
double fontHeightMultiple = 2.0;
FontRenderContext frc = new FontRenderContext(null, true, true);
XSSFWorkbook wb = sheet.getWorkbook();
XSSFFont defaultFont = wb.getFontAt((short) 0);
str = new AttributedString("" + defaultChar);
copyAttributes(defaultFont, str, 0, 1);
layout = new TextLayout(str.getIterator(), frc);
int defaultCharWidth = (int)layout.getAdvance();
double width = -1;
rows:
for (Iterator it = sheet.rowIterator(); it.hasNext();) {
XSSFRow row = (XSSFRow) it.next();
XSSFCell cell = row.getCell(column);
if (cell == null) {
continue;
}
int colspan = 1;
for (int i = 0 ; i < sheet.getNumMergedRegions(); i++) {
CellRangeAddress region = sheet.getMergedRegion(i);
if (containsCell(region, row.getRowNum(), column)) {
if (!useMergedCells) {
// If we're not using merged cells, skip this one and move on to the next.
continue rows;
}
cell = row.getCell(region.getFirstColumn());
colspan = 1 + region.getLastColumn() - region.getFirstColumn();
}
}
XSSFCellStyle style = cell.getCellStyle();
XSSFFont font = wb.getFontAt(style.getFontIndex());
if (cell.getCellType() == XSSFCell.CELL_TYPE_STRING) {
XSSFRichTextString rt = cell.getRichStringCellValue();
String[] lines = rt.getString().split("\\n");
for (int i = 0; i < lines.length; i++) {
String txt = lines[i] + defaultChar;
str = new AttributedString(txt);
copyAttributes(font, str, 0, txt.length());
if (rt.numFormattingRuns() > 0) {
int pos = 0;
for (int j = 0; j < rt.numFormattingRuns(); j++) {
XSSFFont fnt = rt.getFontOfFormattingRun(j);
if (fnt != null) {
int len = rt.getLengthOfFormattingRun(j);
copyAttributes(fnt, str, pos, pos + len);
pos += len;
}
}
}
layout = new TextLayout(str.getIterator(), frc);
if(style.getRotation() != 0){
/*
* Transform the text using a scale so that it's height is increased by a multiple of the leading,
* and then rotate the text before computing the bounds. The scale results in some whitespace around
* the unrotated top and bottom of the text that normally wouldn't be present if unscaled, but
* is added by the standard Excel autosize.
*/
AffineTransform trans = new AffineTransform();
trans.concatenate(AffineTransform.getRotateInstance(style.getRotation()*2.0*Math.PI/360.0));
trans.concatenate(
AffineTransform.getScaleInstance(1, fontHeightMultiple)
);
width = Math.max(width, ((layout.getOutline(trans).getBounds().getWidth() / colspan) / defaultCharWidth) + cell.getCellStyle().getIndention());
} else {
width = Math.max(width, ((layout.getBounds().getWidth() / colspan) / defaultCharWidth) + cell.getCellStyle().getIndention());
}
}
} else {
String sval = null;
if (cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) {
String format = style.getDataFormatString().replaceAll("\"", "");
double value = cell.getNumericCellValue();
try {
NumberFormat fmt;
if ("General".equals(format))
sval = "" + value;
else
{
fmt = new DecimalFormat(format);
sval = fmt.format(value);
}
} catch (Exception e) {
sval = "" + value;
}
} else if (cell.getCellType() == XSSFCell.CELL_TYPE_BOOLEAN) {
sval = String.valueOf(cell.getBooleanCellValue());
}
if(sval != null) {
String txt = sval + defaultChar;
str = new AttributedString(txt);
copyAttributes(font, str, 0, txt.length());
layout = new TextLayout(str.getIterator(), frc);
if(style.getRotation() != 0){
/*
* Transform the text using a scale so that it's height is increased by a multiple of the leading,
* and then rotate the text before computing the bounds. The scale results in some whitespace around
* the unrotated top and bottom of the text that normally wouldn't be present if unscaled, but
* is added by the standard Excel autosize.
*/
AffineTransform trans = new AffineTransform();
trans.concatenate(AffineTransform.getRotateInstance(style.getRotation()*2.0*Math.PI/360.0));
trans.concatenate(
AffineTransform.getScaleInstance(1, fontHeightMultiple)
);
width = Math.max(width, ((layout.getOutline(trans).getBounds().getWidth() / colspan) / defaultCharWidth) + cell.getCellStyle().getIndention());
} else {
width = Math.max(width, ((layout.getBounds().getWidth() / colspan) / defaultCharWidth) + cell.getCellStyle().getIndention());
}
}
}
}
return width;
}
/**
* Copy text attributes from the supplied HSSFFont to Java2D AttributedString
*/
private static void copyAttributes(XSSFFont font, AttributedString str, int startIdx, int endIdx) {
str.addAttribute(TextAttribute.FAMILY, font.getFontName(), startIdx, endIdx);
str.addAttribute(TextAttribute.SIZE, new Float(font.getFontHeightInPoints()));
if (font.getBoldweight() == XSSFFont.BOLDWEIGHT_BOLD) str.addAttribute(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD, startIdx, endIdx);
if (font.getItalic() ) str.addAttribute(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE, startIdx, endIdx);
if (font.getUnderline() == XSSFFont.U_SINGLE ) str.addAttribute(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON, startIdx, endIdx);
}
private static boolean containsCell(CellRangeAddress cr, int rowIx, int colIx) {
if (cr.getFirstRow() <= rowIx && cr.getLastRow() >= rowIx
&& cr.getFirstColumn() <= colIx && cr.getLastColumn() >= colIx)
{
return true;
}
return false;
}
}

View File

@ -17,7 +17,6 @@
package org.apache.poi.xssf.usermodel;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
@ -28,21 +27,13 @@ import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Comment;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.DataFormat;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.XSSFTestDataSamples;
import org.apache.poi.xssf.model.CommentsTable;
import org.apache.poi.xssf.model.SharedStringSource;
import org.apache.poi.xssf.model.SharedStringsTable;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComments;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STCellType;
@ -291,7 +282,8 @@ public final class TestXSSFCell extends TestCase {
CellStyle cs = workbook.createCellStyle();
assertNotNull(cs);
assertTrue(cs.getIndex() > 0);
assertNotNull(creationHelper);
assertNotNull(creationHelper.createDataFormat());
@ -299,9 +291,10 @@ public final class TestXSSFCell extends TestCase {
creationHelper.createDataFormat().getFormat("yyyy/mm/dd")
);
Cell cell = sheet.createRow(0).createCell((short)0);
assertNotNull(cell.getCellStyle());
assertEquals(0, cell.getCellStyle().getIndex());
cell.setCellValue(new Date(654321));
assertNull(cell.getCellStyle());
cell.setCellStyle(cs);
assertEquals(new Date(654321), cell.getDateCellValue());

View File

@ -532,10 +532,10 @@ public class TestXSSFCellStyle extends TestCase {
public void testGetSetIndent() {
assertEquals((short)0, cellStyle.getIndention());
cellXf.getAlignment().setIndent(3);
cellStyle.setIndention((short)3);
assertEquals((short)3, cellStyle.getIndention());
cellStyle.setIndention((short) 13);
assertEquals((short)13, cellXf.getAlignment().getIndent());
assertEquals((short)13, cellStyle.getIndention());
}
public void testGetSetAlignement() {
@ -576,10 +576,10 @@ public class TestXSSFCellStyle extends TestCase {
public void testGetSetWrapText() {
assertFalse(cellStyle.getWrapText());
cellXf.getAlignment().setWrapText(true);
cellStyle.setWrapText(true);
assertTrue(cellStyle.getWrapText());
cellStyle.setWrapText(false);
assertFalse(cellXf.getAlignment().getWrapText());
assertFalse(cellStyle.getWrapText());
}
/**

View File

@ -36,9 +36,7 @@ public class TestXSSFDialogSheet extends TestCase {
public void testGetDialog() {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = (XSSFSheet) workbook.createSheet("Sheet 1");
assertFalse(sheet.getDialog());
XSSFSheet dialogsheet = (XSSFSheet) workbook.createDialogsheet("Dialogsheet 1", null);
XSSFDialogsheet dialogsheet = workbook.createDialogsheet("Dialogsheet 1", null);
assertTrue(dialogsheet.getDialog());
}

View File

@ -204,12 +204,12 @@ public final class TestXSSFRow extends TestCase {
assertEquals(Cell.CELL_TYPE_NUMERIC, row.getCell(5, XSSFRow.CREATE_NULL_AS_BLANK).getCellType());
// Check created ones get the right column
assertEquals((short)0, row.getCell(0, XSSFRow.CREATE_NULL_AS_BLANK).getCellNum());
assertEquals((short)1, row.getCell(1, XSSFRow.CREATE_NULL_AS_BLANK).getCellNum());
assertEquals((short)2, row.getCell(2, XSSFRow.CREATE_NULL_AS_BLANK).getCellNum());
assertEquals((short)3, row.getCell(3, XSSFRow.CREATE_NULL_AS_BLANK).getCellNum());
assertEquals((short)4, row.getCell(4, XSSFRow.CREATE_NULL_AS_BLANK).getCellNum());
assertEquals((short)5, row.getCell(5, XSSFRow.CREATE_NULL_AS_BLANK).getCellNum());
assertEquals((short)0, row.getCell(0, XSSFRow.CREATE_NULL_AS_BLANK).getColumnIndex());
assertEquals((short)1, row.getCell(1, XSSFRow.CREATE_NULL_AS_BLANK).getColumnIndex());
assertEquals((short)2, row.getCell(2, XSSFRow.CREATE_NULL_AS_BLANK).getColumnIndex());
assertEquals((short)3, row.getCell(3, XSSFRow.CREATE_NULL_AS_BLANK).getColumnIndex());
assertEquals((short)4, row.getCell(4, XSSFRow.CREATE_NULL_AS_BLANK).getColumnIndex());
assertEquals((short)5, row.getCell(5, XSSFRow.CREATE_NULL_AS_BLANK).getColumnIndex());
}
/**

View File

@ -32,7 +32,6 @@ import org.apache.poi.xssf.XSSFTestDataSamples;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCols;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComments;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRow;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet;
@ -305,7 +304,7 @@ public class TestXSSFSheet extends TestCase {
XSSFOddFooter ftr;
// Sheet 1 has a header with center and right text
XSSFSheet s1 = (XSSFSheet)workbook.getSheetAt(0);
XSSFSheet s1 = workbook.getSheetAt(0);
assertNotNull(s1.getHeader());
assertNotNull(s1.getFooter());
hdr = (XSSFOddHeader)s1.getHeader();
@ -324,7 +323,7 @@ public class TestXSSFSheet extends TestCase {
// Sheet 2 has a footer, but it's empty
XSSFSheet s2 = (XSSFSheet)workbook.getSheetAt(1);
XSSFSheet s2 = workbook.getSheetAt(1);
assertNotNull(s2.getHeader());
assertNotNull(s2.getFooter());
hdr = (XSSFOddHeader)s2.getHeader();
@ -359,7 +358,7 @@ public class TestXSSFSheet extends TestCase {
public void testGetAllHeadersFooters() {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = (XSSFSheet) workbook.createSheet("Sheet 1");
XSSFSheet sheet = workbook.createSheet("Sheet 1");
assertNotNull(sheet.getOddFooter());
assertNotNull(sheet.getEvenFooter());
assertNotNull(sheet.getFirstFooter());
@ -447,28 +446,19 @@ public class TestXSSFSheet extends TestCase {
public void testAutoSizeColumn() {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = (XSSFSheet) workbook.createSheet("Sheet 1");
XSSFSheet sheet = workbook.createSheet("Sheet 1");
sheet.createRow(0).createCell(13).setCellValue("test");
sheet.autoSizeColumn((short)13);
ColumnHelper columnHelper = sheet.getColumnHelper();
CTCol col = columnHelper.getColumn(13, false);
assertNull(col);
sheet.autoSizeColumn((short)13);
col = columnHelper.getColumn(13, false);
assertNotNull(col);
assertTrue(col.getBestFit());
}
public void testGetDialog() {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("Sheet 1");
assertFalse(sheet.getDialog());
XSSFSheet dialogsheet = workbook.createDialogsheet("Dialogsheet 1", null);
assertTrue(dialogsheet.getDialog());
assertTrue(col.getBestFit());
}
public void testGetSetHorizontallyCentered() {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = (XSSFSheet) workbook.createSheet("Sheet 1");
XSSFSheet sheet = workbook.createSheet("Sheet 1");
assertFalse(sheet.getHorizontallyCenter());
sheet.setHorizontallyCenter(true);
assertTrue(sheet.getHorizontallyCenter());
@ -478,7 +468,7 @@ public class TestXSSFSheet extends TestCase {
public void testGetSetVerticallyCentered() {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = (XSSFSheet) workbook.createSheet("Sheet 1");
XSSFSheet sheet = workbook.createSheet("Sheet 1");
assertFalse(sheet.getVerticallyCenter());
sheet.setVerticallyCenter(true);
assertTrue(sheet.getVerticallyCenter());
@ -488,7 +478,7 @@ public class TestXSSFSheet extends TestCase {
public void testIsSetPrintGridlines() {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = (XSSFSheet) workbook.createSheet("Sheet 1");
XSSFSheet sheet = workbook.createSheet("Sheet 1");
assertFalse(sheet.isPrintGridlines());
sheet.setPrintGridlines(true);
assertTrue(sheet.isPrintGridlines());
@ -496,7 +486,7 @@ public class TestXSSFSheet extends TestCase {
public void testIsSetDisplayFormulas() {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = (XSSFSheet) workbook.createSheet("Sheet 1");
XSSFSheet sheet = workbook.createSheet("Sheet 1");
assertFalse(sheet.isDisplayFormulas());
sheet.setDisplayFormulas(true);
assertTrue(sheet.isDisplayFormulas());
@ -504,7 +494,7 @@ public class TestXSSFSheet extends TestCase {
public void testIsSetDisplayGridLines() {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = (XSSFSheet) workbook.createSheet("Sheet 1");
XSSFSheet sheet = workbook.createSheet("Sheet 1");
assertTrue(sheet.isDisplayGridlines());
sheet.setDisplayGridlines(false);
assertFalse(sheet.isDisplayGridlines());
@ -512,7 +502,7 @@ public class TestXSSFSheet extends TestCase {
public void testIsSetDisplayGuts() {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = (XSSFSheet) workbook.createSheet("Sheet 1");
XSSFSheet sheet = workbook.createSheet("Sheet 1");
assertTrue(sheet.getDisplayGuts());
sheet.setDisplayGuts(false);
assertFalse(sheet.getDisplayGuts());
@ -520,7 +510,7 @@ public class TestXSSFSheet extends TestCase {
public void testIsSetDisplayRowColHeadings() {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = (XSSFSheet) workbook.createSheet("Sheet 1");
XSSFSheet sheet = workbook.createSheet("Sheet 1");
assertTrue(sheet.isDisplayRowColHeadings());
sheet.setDisplayRowColHeadings(false);
assertFalse(sheet.isDisplayRowColHeadings());
@ -528,7 +518,7 @@ public class TestXSSFSheet extends TestCase {
public void testGetScenarioProtect() {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = (XSSFSheet) workbook.createSheet("Sheet 1");
XSSFSheet sheet = workbook.createSheet("Sheet 1");
assertFalse(sheet.getScenarioProtect());
}
@ -720,8 +710,8 @@ public class TestXSSFSheet extends TestCase {
sheet.setDefaultColumnStyle((short) 3, cellStyle);
assertEquals(1, ctWorksheet.getColsArray(0).getColArray(0).getStyle());
XSSFRow row = (XSSFRow) sheet.createRow(0);
XSSFCell cell = (XSSFCell) sheet.getRow(0).createCell(3);
XSSFRow row = sheet.createRow(0);
XSSFCell cell = sheet.getRow(0).createCell(3);
}

View File

@ -322,8 +322,6 @@ public final class TestXSSFWorkbook extends TestCase {
((XSSFFont)font).setBold(true);
font.setUnderline(Font.U_DOUBLE);
StylesTable styleSource=new StylesTable();
long index=styleSource.putFont(font);
workbook.setStylesSource(styleSource);
fontFind=workbook.findFont(Font.BOLDWEIGHT_BOLD, IndexedColors.BLACK.getIndex(), (short)15, "Calibri", false, false, Font.SS_NONE, Font.U_DOUBLE);
assertNull(fontFind);
}
@ -399,7 +397,7 @@ public final class TestXSSFWorkbook extends TestCase {
public void testSetDisplayedTab(){
XSSFWorkbook workbook = new XSSFWorkbook();
workbook.setFirstVisibleTab(new Integer(1).shortValue());
workbook.setFirstVisibleTab(1);
short i = (short) workbook.getFirstVisibleTab();
//0 (defualt value) is not longer set
assertNotSame(0, i);