git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@707843 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
97ceedbbf7
commit
b09c6e3031
@ -18,25 +18,40 @@ package org.apache.poi.xssf.usermodel.examples;
|
||||
|
||||
import java.io.FileOutputStream;
|
||||
|
||||
import org.apache.poi.ss.usermodel.Cell;
|
||||
import org.apache.poi.ss.usermodel.CellStyle;
|
||||
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.usermodel.XSSFWorkbook;
|
||||
|
||||
public class SetPrintArea {
|
||||
|
||||
/**
|
||||
* How to use newlines in cells
|
||||
*/
|
||||
public class CellNewlines {
|
||||
|
||||
public static void main(String[]args) throws Exception {
|
||||
Workbook wb = new XSSFWorkbook();
|
||||
Sheet sheet = wb.createSheet("Sheet1");
|
||||
//wb.setPrintArea(0, "$A$1:$C$2");
|
||||
//sets the print area for the first sheet
|
||||
//Alternatively:
|
||||
wb.setPrintArea(0, 1, 2, 0, 3); //is equivalent to using the name reference (See the JavaDocs for more details)
|
||||
Sheet sheet = wb.createSheet();
|
||||
|
||||
// Create various cells and rows for spreadsheet.
|
||||
Row row = sheet.createRow(2);
|
||||
Cell cell = row.createCell(2);
|
||||
cell.setCellValue("Use \n with word wrap on to create a new line");
|
||||
|
||||
FileOutputStream fileOut = new FileOutputStream("printArea.xlsx");
|
||||
//to enable newlines you need set a cell styles with wrap=true
|
||||
CellStyle cs = wb.createCellStyle();
|
||||
cs.setWrapText(true);
|
||||
cell.setCellStyle(cs);
|
||||
|
||||
//increase row height to accomodate two lines of text
|
||||
row.setHeightInPoints((2*sheet.getDefaultRowHeightInPoints()));
|
||||
|
||||
//adjust column width to fit the content
|
||||
sheet.autoSizeColumn((short)2);
|
||||
|
||||
FileOutputStream fileOut = new FileOutputStream("ooxml-newlines.xlsx");
|
||||
wb.write(fileOut);
|
||||
fileOut.close();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,83 @@
|
||||
/* ====================================================================
|
||||
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.examples;
|
||||
|
||||
import java.io.FileOutputStream;
|
||||
|
||||
import org.apache.poi.ss.usermodel.Sheet;
|
||||
import org.apache.poi.ss.usermodel.Workbook;
|
||||
import org.apache.poi.ss.usermodel.Row;
|
||||
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||
|
||||
/**
|
||||
* Demonstrates various settings avaiable in the Page Setup dialog
|
||||
*/
|
||||
public class WorkingWithPageSetup {
|
||||
|
||||
public static void main(String[]args) throws Exception {
|
||||
Workbook wb = new XSSFWorkbook();
|
||||
|
||||
/**
|
||||
* It's possible to set up repeating rows and columns in your printouts by using the setRepeatingRowsAndColumns() function in the Workbook object.
|
||||
*
|
||||
* This function Contains 5 parameters:
|
||||
* The first parameter is the index to the sheet (0 = first sheet).
|
||||
* The second and third parameters specify the range for the columns to repreat.
|
||||
* To stop the columns from repeating pass in -1 as the start and end column.
|
||||
* The fourth and fifth parameters specify the range for the rows to repeat.
|
||||
* To stop the columns from repeating pass in -1 as the start and end rows.
|
||||
*/
|
||||
Sheet sheet1 = wb.createSheet("new sheet");
|
||||
Sheet sheet2 = wb.createSheet("second sheet");
|
||||
|
||||
// Set the columns to repeat from column 0 to 2 on the first sheet
|
||||
Row row1 = sheet1.createRow(0);
|
||||
row1.createCell(0).setCellValue(1);
|
||||
row1.createCell(1).setCellValue(2);
|
||||
row1.createCell(2).setCellValue(3);
|
||||
Row row2 = sheet1.createRow(1);
|
||||
row2.createCell(1).setCellValue(4);
|
||||
row2.createCell(2).setCellValue(5);
|
||||
|
||||
|
||||
Row row3 = sheet2.createRow(1);
|
||||
row3.createCell(0).setCellValue(2.1);
|
||||
row3.createCell(4).setCellValue(2.2);
|
||||
row3.createCell(5).setCellValue(2.3);
|
||||
Row row4 = sheet2.createRow(2);
|
||||
row4.createCell(4).setCellValue(2.4);
|
||||
row4.createCell(5).setCellValue(2.5);
|
||||
|
||||
// Set the columns to repeat from column 0 to 2 on the first sheet
|
||||
wb.setRepeatingRowsAndColumns(0,0,2,-1,-1);
|
||||
// Set the the repeating rows and columns on the second sheet.
|
||||
wb.setRepeatingRowsAndColumns(1,4,5,1,2);
|
||||
|
||||
// Set the the repeating rows and columns on the second sheet
|
||||
wb.setRepeatingRowsAndColumns(1, 4, 5, 1, 2);
|
||||
|
||||
|
||||
|
||||
//set the print area for the first sheet
|
||||
wb.setPrintArea(0, 1, 2, 0, 3);
|
||||
|
||||
|
||||
FileOutputStream fileOut = new FileOutputStream("ooxml-printsetup.xlsx");
|
||||
wb.write(fileOut);
|
||||
fileOut.close();
|
||||
}
|
||||
}
|
@ -29,164 +29,263 @@ import org.openxml4j.opc.PackageRelationship;
|
||||
/**
|
||||
* XSSF Implementation of a Hyperlink.
|
||||
* Note - unlike with HSSF, many kinds of hyperlink
|
||||
* are largely stored as relations of the sheet
|
||||
* are largely stored as relations of the sheet
|
||||
*/
|
||||
public class XSSFHyperlink implements Hyperlink {
|
||||
private int type;
|
||||
private PackageRelationship externalRel;
|
||||
private CTHyperlink ctHyperlink;
|
||||
private String location;
|
||||
|
||||
protected XSSFHyperlink(int type) {
|
||||
this.type = type;
|
||||
this.ctHyperlink = CTHyperlink.Factory.newInstance();
|
||||
}
|
||||
protected XSSFHyperlink(CTHyperlink ctHyperlink, PackageRelationship hyperlinkRel) {
|
||||
this.ctHyperlink = ctHyperlink;
|
||||
this.externalRel = hyperlinkRel;
|
||||
|
||||
// Figure out the Hyperlink type and distination
|
||||
|
||||
// If it has a location, it's internal
|
||||
if(ctHyperlink.getLocation() != null) {
|
||||
type = Hyperlink.LINK_DOCUMENT;
|
||||
location = ctHyperlink.getLocation();
|
||||
} else {
|
||||
// Otherwise it's somehow external, check
|
||||
// the relation to see how
|
||||
if(externalRel == null) {
|
||||
if(ctHyperlink.getId() != null) {
|
||||
throw new IllegalStateException("The hyperlink for cell " + ctHyperlink.getRef() + " references relation " + ctHyperlink.getId() + ", but that didn't exist!");
|
||||
} else {
|
||||
throw new IllegalStateException("A sheet hyperlink must either have a location, or a relationship. Found:\n" + ctHyperlink);
|
||||
}
|
||||
}
|
||||
|
||||
URI target = externalRel.getTargetURI();
|
||||
location = target.toString();
|
||||
|
||||
// Try to figure out the type
|
||||
if(location.startsWith("http://") || location.startsWith("https://")
|
||||
|| location.startsWith("ftp://")) {
|
||||
type = Hyperlink.LINK_URL;
|
||||
} else if(location.startsWith("mailto:")) {
|
||||
type = Hyperlink.LINK_EMAIL;
|
||||
} else {
|
||||
type = Hyperlink.LINK_FILE;
|
||||
}
|
||||
}
|
||||
}
|
||||
private int type;
|
||||
private PackageRelationship externalRel;
|
||||
private CTHyperlink ctHyperlink;
|
||||
private String location;
|
||||
|
||||
/**
|
||||
* Returns the underlying hyperlink object
|
||||
*/
|
||||
protected CTHyperlink getCTHyperlink() {
|
||||
return ctHyperlink;
|
||||
}
|
||||
|
||||
/**
|
||||
* Do we need to a relation too, to represent
|
||||
* this hyperlink?
|
||||
*/
|
||||
public boolean needsRelationToo() {
|
||||
return (type != Hyperlink.LINK_DOCUMENT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates the relation if required
|
||||
*/
|
||||
protected void generateRelationIfNeeded(PackagePart sheetPart) {
|
||||
if(needsRelationToo()) {
|
||||
// Generate the relation
|
||||
PackageRelationship rel =
|
||||
sheetPart.addExternalRelationship(location, XSSFRelation.SHEET_HYPERLINKS.getRelation());
|
||||
|
||||
// Update the r:id
|
||||
ctHyperlink.setId(rel.getId());
|
||||
}
|
||||
}
|
||||
|
||||
public int getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the reference of the cell this applies to,
|
||||
* eg A55
|
||||
*/
|
||||
public String getCellRef() {
|
||||
return ctHyperlink.getRef();
|
||||
}
|
||||
|
||||
public String getAddress() {
|
||||
return location;
|
||||
}
|
||||
public String getLabel() {
|
||||
return ctHyperlink.getDisplay();
|
||||
}
|
||||
public String getLocation() {
|
||||
return ctHyperlink.getLocation();
|
||||
}
|
||||
|
||||
public void setLabel(String label) {
|
||||
ctHyperlink.setDisplay(label);
|
||||
}
|
||||
|
||||
public void setLocation(String location){
|
||||
ctHyperlink.setLocation(location);
|
||||
}
|
||||
|
||||
public void setAddress(String address) {
|
||||
location = address;
|
||||
/**
|
||||
* Create a new XSSFHyperlink. This method is protected to be used only by XSSFCreationHelper
|
||||
*
|
||||
* @param type - the type of hyperlink to create
|
||||
*/
|
||||
protected XSSFHyperlink(int type) {
|
||||
this.type = type;
|
||||
this.ctHyperlink = CTHyperlink.Factory.newInstance();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a XSSFHyperlink amd initialize it from the supplied CTHyperlink bean and package relationship
|
||||
*
|
||||
* @param ctHyperlink the xml bean containing xml properties
|
||||
* @param hyperlinkRel the relationship in the underlying OPC package which stores the actual link's address
|
||||
*/
|
||||
protected XSSFHyperlink(CTHyperlink ctHyperlink, PackageRelationship hyperlinkRel) {
|
||||
this.ctHyperlink = ctHyperlink;
|
||||
this.externalRel = hyperlinkRel;
|
||||
|
||||
// Figure out the Hyperlink type and distination
|
||||
|
||||
// If it has a location, it's internal
|
||||
if (ctHyperlink.getLocation() != null) {
|
||||
type = Hyperlink.LINK_DOCUMENT;
|
||||
location = ctHyperlink.getLocation();
|
||||
} else {
|
||||
// Otherwise it's somehow external, check
|
||||
// the relation to see how
|
||||
if (externalRel == null) {
|
||||
if (ctHyperlink.getId() != null) {
|
||||
throw new IllegalStateException("The hyperlink for cell " + ctHyperlink.getRef() + " references relation " + ctHyperlink.getId() + ", but that didn't exist!");
|
||||
} else {
|
||||
throw new IllegalStateException("A sheet hyperlink must either have a location, or a relationship. Found:\n" + ctHyperlink);
|
||||
}
|
||||
}
|
||||
|
||||
URI target = externalRel.getTargetURI();
|
||||
location = target.toString();
|
||||
|
||||
// Try to figure out the type
|
||||
if (location.startsWith("http://") || location.startsWith("https://")
|
||||
|| location.startsWith("ftp://")) {
|
||||
type = Hyperlink.LINK_URL;
|
||||
} else if (location.startsWith("mailto:")) {
|
||||
type = Hyperlink.LINK_EMAIL;
|
||||
} else {
|
||||
type = Hyperlink.LINK_FILE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the underlying hyperlink object
|
||||
*/
|
||||
protected CTHyperlink getCTHyperlink() {
|
||||
return ctHyperlink;
|
||||
}
|
||||
|
||||
/**
|
||||
* Do we need to a relation too, to represent
|
||||
* this hyperlink?
|
||||
*/
|
||||
public boolean needsRelationToo() {
|
||||
return (type != Hyperlink.LINK_DOCUMENT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates the relation if required
|
||||
*/
|
||||
protected void generateRelationIfNeeded(PackagePart sheetPart) {
|
||||
if (needsRelationToo()) {
|
||||
// Generate the relation
|
||||
PackageRelationship rel =
|
||||
sheetPart.addExternalRelationship(location, XSSFRelation.SHEET_HYPERLINKS.getRelation());
|
||||
|
||||
// Update the r:id
|
||||
ctHyperlink.setId(rel.getId());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the type of this hyperlink
|
||||
*
|
||||
* @return the type of this hyperlink
|
||||
*/
|
||||
public int getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the reference of the cell this applies to,
|
||||
* es A55
|
||||
*/
|
||||
public String getCellRef() {
|
||||
return ctHyperlink.getRef();
|
||||
}
|
||||
|
||||
/**
|
||||
* Hypelink address. Depending on the hyperlink type it can be URL, e-mail, path to a file
|
||||
*
|
||||
* @return the address of this hyperlink
|
||||
*/
|
||||
public String getAddress() {
|
||||
return location;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return text label for this hyperlink
|
||||
*
|
||||
* @return text to display
|
||||
*/
|
||||
public String getLabel() {
|
||||
return ctHyperlink.getDisplay();
|
||||
}
|
||||
|
||||
/**
|
||||
* Location within target. If target is a workbook (or this workbook) this shall refer to a
|
||||
* sheet and cell or a defined name. Can also be an HTML anchor if target is HTML file.
|
||||
*
|
||||
* @return location
|
||||
*/
|
||||
public String getLocation() {
|
||||
return ctHyperlink.getLocation();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets text label for this hyperlink
|
||||
*
|
||||
* @param label text label for this hyperlink
|
||||
*/
|
||||
public void setLabel(String label) {
|
||||
ctHyperlink.setDisplay(label);
|
||||
}
|
||||
|
||||
/**
|
||||
* Location within target. If target is a workbook (or this workbook) this shall refer to a
|
||||
* sheet and cell or a defined name. Can also be an HTML anchor if target is HTML file.
|
||||
*
|
||||
* @param location - string representing a location of this hyperlink
|
||||
*/
|
||||
public void setLocation(String location) {
|
||||
ctHyperlink.setLocation(location);
|
||||
}
|
||||
|
||||
/**
|
||||
* Hypelink address. Depending on the hyperlink type it can be URL, e-mail, path to a file
|
||||
*
|
||||
* @param address - the address of this hyperlink
|
||||
*/
|
||||
public void setAddress(String address) {
|
||||
location = address;
|
||||
//we must set location for internal hyperlinks
|
||||
if(type == Hyperlink.LINK_DOCUMENT){
|
||||
if (type == Hyperlink.LINK_DOCUMENT) {
|
||||
setLocation(address);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Assigns this hyperlink to the given cell reference
|
||||
*/
|
||||
protected void setCellReference(String ref) {
|
||||
ctHyperlink.setRef(ref);
|
||||
}
|
||||
|
||||
private CellReference buildCellReference() {
|
||||
return new CellReference(ctHyperlink.getRef());
|
||||
}
|
||||
|
||||
public int getFirstColumn() {
|
||||
return buildCellReference().getCol();
|
||||
}
|
||||
public int getLastColumn() {
|
||||
return buildCellReference().getCol();
|
||||
}
|
||||
|
||||
public int getFirstRow() {
|
||||
return buildCellReference().getRow();
|
||||
}
|
||||
public int getLastRow() {
|
||||
return buildCellReference().getRow();
|
||||
}
|
||||
|
||||
public void setFirstColumn(int col) {
|
||||
ctHyperlink.setRef(
|
||||
new CellReference(
|
||||
getFirstRow(), col
|
||||
).formatAsString()
|
||||
);
|
||||
}
|
||||
public void setLastColumn(int col) {
|
||||
setFirstColumn(col);
|
||||
}
|
||||
public void setFirstRow(int row) {
|
||||
ctHyperlink.setRef(
|
||||
new CellReference(
|
||||
row, getFirstColumn()
|
||||
).formatAsString()
|
||||
);
|
||||
}
|
||||
public void setLastRow(int row) {
|
||||
setFirstRow(row);
|
||||
/**
|
||||
* Assigns this hyperlink to the given cell reference
|
||||
*/
|
||||
protected void setCellReference(String ref) {
|
||||
ctHyperlink.setRef(ref);
|
||||
}
|
||||
|
||||
private CellReference buildCellReference() {
|
||||
return new CellReference(ctHyperlink.getRef());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return the column of the first cell that contains the hyperlink
|
||||
*
|
||||
* @return the 0-based column of the first cell that contains the hyperlink
|
||||
*/
|
||||
public int getFirstColumn() {
|
||||
return buildCellReference().getCol();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return the column of the last cell that contains the hyperlink
|
||||
*
|
||||
* @return the 0-based column of the last cell that contains the hyperlink
|
||||
*/
|
||||
public int getLastColumn() {
|
||||
return buildCellReference().getCol();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the row of the first cell that contains the hyperlink
|
||||
*
|
||||
* @return the 0-based row of the cell that contains the hyperlink
|
||||
*/
|
||||
public int getFirstRow() {
|
||||
return buildCellReference().getRow();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return the row of the last cell that contains the hyperlink
|
||||
*
|
||||
* @return the 0-based row of the last cell that contains the hyperlink
|
||||
*/
|
||||
public int getLastRow() {
|
||||
return buildCellReference().getRow();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the column of the first cell that contains the hyperlink
|
||||
*
|
||||
* @param col the 0-based column of the first cell that contains the hyperlink
|
||||
*/
|
||||
public void setFirstColumn(int col) {
|
||||
ctHyperlink.setRef(
|
||||
new CellReference(
|
||||
getFirstRow(), col
|
||||
).formatAsString()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the column of the last cell that contains the hyperlink
|
||||
*
|
||||
* @param col the 0-based column of the last cell that contains the hyperlink
|
||||
*/
|
||||
public void setLastColumn(int col) {
|
||||
setFirstColumn(col);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the row of the first cell that contains the hyperlink
|
||||
*
|
||||
* @param row the 0-based row of the first cell that contains the hyperlink
|
||||
*/
|
||||
public void setFirstRow(int row) {
|
||||
ctHyperlink.setRef(
|
||||
new CellReference(
|
||||
row, getFirstColumn()
|
||||
).formatAsString()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the row of the last cell that contains the hyperlink
|
||||
*
|
||||
* @param row the 0-based row of the last cell that contains the hyperlink
|
||||
*/
|
||||
public void setLastRow(int row) {
|
||||
setFirstRow(row);
|
||||
}
|
||||
}
|
||||
|
@ -589,6 +589,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
|
||||
if (name == null) return null;
|
||||
//adding one here because 0 indicates a global named region; doesnt make sense for print areas
|
||||
return name.getReference();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@ -697,19 +698,52 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
|
||||
return getPackagePart().getContentType().equals(XSSFRelation.MACROS_WORKBOOK.getContentType());
|
||||
}
|
||||
|
||||
public void removeName(int index) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
/**
|
||||
* removes the name
|
||||
*
|
||||
* @param nameIndex name index
|
||||
*/
|
||||
public void removeName(int nameIndex) {
|
||||
if (namedRanges.size() > nameIndex) {
|
||||
XSSFName name = getNameAt(nameIndex);
|
||||
int cont = 0;
|
||||
for (XSSFName nameRange : namedRanges) {
|
||||
if (nameRange.getReference().equals(name.getReference())) {
|
||||
namedRanges.remove(cont);
|
||||
getDefinedNames().removeDefinedName(nameIndex);
|
||||
break;
|
||||
}
|
||||
cont++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* removes the name
|
||||
*
|
||||
* @param name range
|
||||
* name index
|
||||
*/
|
||||
public void removeName(String name) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
//TODO
|
||||
//int index=getNameIndex(name);
|
||||
//removeName(index);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the printarea for the sheet specified
|
||||
*
|
||||
* @param sheetIndex 0-based sheet index (0 = First Sheet)
|
||||
*/
|
||||
public void removePrintArea(int sheetIndex) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
int cont = 0;
|
||||
for (XSSFName name : namedRanges) {
|
||||
if (name.getNameName().equals(XSSFName.BUILTIN_PRINT_AREA) && name.getLocalSheetId() == sheetIndex) {
|
||||
namedRanges.remove(cont);
|
||||
break;
|
||||
}
|
||||
cont++;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -818,7 +852,9 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
|
||||
name = createBuiltInName(XSSFName.BUILTIN_PRINT_AREA, sheetIndex);
|
||||
namedRanges.add(name);
|
||||
}
|
||||
name.setReference(reference);
|
||||
//short externSheetIndex = getWorkbook().checkExternSheet(sheetIndex);
|
||||
//name.setExternSheetNumber(externSheetIndex);
|
||||
name.setReference(reference);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -835,12 +871,10 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
|
||||
setPrintArea(sheetIndex, reference);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sets the repeating rows and columns for a sheet.
|
||||
* This is function is included in the workbook
|
||||
* because it creates/modifies name records which are stored at the
|
||||
* workbook level.
|
||||
* <p>
|
||||
* <p/>
|
||||
* To set just repeating columns:
|
||||
* <pre>
|
||||
* workbook.setRepeatingRowsAndColumns(0,0,1,-1,-1);
|
||||
@ -854,30 +888,66 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
|
||||
* workbook.setRepeatingRowsAndColumns(0,-1,-1,-1,-1);
|
||||
* </pre>
|
||||
*
|
||||
* @param sheetIndex 0 based index to sheet.
|
||||
* @param startColumn 0 based start of repeating columns.
|
||||
* @param endColumn 0 based end of repeating columns.
|
||||
* @param startRow 0 based start of repeating rows.
|
||||
* @param endRow 0 based end of repeating rows.
|
||||
* @param sheetIndex 0 based index to sheet.
|
||||
* @param startColumn 0 based start of repeating columns.
|
||||
* @param endColumn 0 based end of repeating columns.
|
||||
* @param startRow 0 based start of repeating rows.
|
||||
* @param endRow 0 based end of repeating rows.
|
||||
*/
|
||||
public void setRepeatingRowsAndColumns(int sheetIndex,
|
||||
int startColumn, int endColumn,
|
||||
int startRow, int endRow) {
|
||||
//TODO
|
||||
// Check arguments
|
||||
if ((startColumn == -1 && endColumn != -1) || startColumn < -1 || endColumn < -1 || startColumn > endColumn)
|
||||
throw new IllegalArgumentException("Invalid column range specification");
|
||||
if ((startRow == -1 && endRow != -1) || startRow < -1 || endRow < -1 || startRow > endRow)
|
||||
throw new IllegalArgumentException("Invalid row range specification");
|
||||
|
||||
XSSFSheet sheet = getSheetAt(sheetIndex);
|
||||
boolean removingRange = startColumn == -1 && endColumn == -1 && startRow == -1 && endRow == -1;
|
||||
|
||||
XSSFName name = getBuiltInName(XSSFName.BUILTIN_PRINT_TITLE, sheetIndex);
|
||||
if (removingRange && name != null) {
|
||||
namedRanges.remove(name);
|
||||
return;
|
||||
}
|
||||
if (name == null) {
|
||||
name = createBuiltInName(XSSFName.BUILTIN_PRINT_TITLE, sheetIndex);
|
||||
String reference = getReferenceBuiltInRecord(name.getSheetName(), startColumn, endColumn, startRow, endRow);
|
||||
name.setReference(reference);
|
||||
namedRanges.add(name);
|
||||
}
|
||||
|
||||
XSSFPrintSetup printSetup = sheet.getPrintSetup();
|
||||
printSetup.setValidSettings(false);
|
||||
}
|
||||
|
||||
private static String getReferenceBuiltInRecord(String sheetName, int startC, int endC, int startR, int endR) {
|
||||
//windows excel example for built-in title: 'second sheet'!$E:$F,'second sheet'!$2:$3
|
||||
CellReference colRef = new CellReference(sheetName, 0, startC, true, true);
|
||||
CellReference colRef2 = new CellReference(sheetName, 0, endC, true, true);
|
||||
|
||||
private String getReferencePrintArea(String sheetName, int startC, int endC, int startR, int endR) {
|
||||
String c = "'" + sheetName + "'!$" + colRef.getCellRefParts()[2] + ":$" + colRef2.getCellRefParts()[2];
|
||||
|
||||
CellReference rowRef = new CellReference(sheetName, startR, 0, true, true);
|
||||
CellReference rowRef2 = new CellReference(sheetName, endR, 0, true, true);
|
||||
|
||||
String r = "";
|
||||
|
||||
if (!rowRef.getCellRefParts()[1].equals("0") && !rowRef2.getCellRefParts()[1].equals("0")) {
|
||||
r = ",'" + sheetName + "'!$" + rowRef.getCellRefParts()[1] + ":$" + rowRef2.getCellRefParts()[1];
|
||||
}
|
||||
return c + r;
|
||||
}
|
||||
|
||||
private static String getReferencePrintArea(String sheetName, int startC, int endC, int startR, int endR) {
|
||||
//windows excel example: Sheet1!$C$3:$E$4
|
||||
CellReference colRef = new CellReference(sheetName, startR, startC, true, true);
|
||||
CellReference colRef2 = new CellReference(sheetName, endR, endC, true, true);
|
||||
|
||||
String c = "'" + sheetName + "'!$" + colRef.getCellRefParts()[2] + "$" + colRef.getCellRefParts()[1] + ":$" + colRef2.getCellRefParts()[2] + "$" + colRef2.getCellRefParts()[1];
|
||||
return c;
|
||||
return "'" + sheetName + "'!$" + colRef.getCellRefParts()[2] + "$" + colRef.getCellRefParts()[1] + ":$" + colRef2.getCellRefParts()[2] + "$" + colRef2.getCellRefParts()[1];
|
||||
}
|
||||
|
||||
//****************** NAME RANGE *************************
|
||||
|
||||
private CTDefinedNames getDefinedNames() {
|
||||
return workbook.getDefinedNames() == null ? workbook.addNewDefinedNames() : workbook.getDefinedNames();
|
||||
}
|
||||
@ -894,29 +964,28 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
|
||||
|
||||
/**
|
||||
* Generates a NameRecord to represent a built-in region
|
||||
*
|
||||
* @return a new NameRecord
|
||||
*/
|
||||
private XSSFName createBuiltInName(String builtInName, int sheetNumber) {
|
||||
if (sheetNumber < 0 || sheetNumber+1 > Short.MAX_VALUE) {
|
||||
throw new IllegalArgumentException("Sheet number ["+sheetNumber+"]is not valid ");
|
||||
if (sheetNumber < 0 || sheetNumber + 1 > Short.MAX_VALUE) {
|
||||
throw new IllegalArgumentException("Sheet number [" + sheetNumber + "]is not valid ");
|
||||
}
|
||||
|
||||
CTDefinedName nameRecord=getDefinedNames().addNewDefinedName();
|
||||
|
||||
CTDefinedName nameRecord = getDefinedNames().addNewDefinedName();
|
||||
nameRecord.setName(builtInName);
|
||||
nameRecord.setLocalSheetId(sheetNumber);
|
||||
|
||||
XSSFName name=new XSSFName(nameRecord,this);
|
||||
for(XSSFName nr : namedRanges){
|
||||
if(nr.equals(name))
|
||||
throw new RuntimeException("Builtin (" + builtInName
|
||||
+ ") already exists for sheet (" + sheetNumber + ")");
|
||||
}
|
||||
|
||||
XSSFName name = new XSSFName(nameRecord, this);
|
||||
for (XSSFName nr : namedRanges) {
|
||||
if (nr.equals(name))
|
||||
throw new RuntimeException("Builtin (" + builtInName
|
||||
+ ") already exists for sheet (" + sheetNumber + ")");
|
||||
}
|
||||
|
||||
return name;
|
||||
}
|
||||
|
||||
//*******************************************
|
||||
|
||||
|
||||
/**
|
||||
* We only set one sheet as selected for compatibility with HSSF.
|
||||
*/
|
||||
|
@ -522,7 +522,7 @@ public class TestXSSFSheet extends TestCase {
|
||||
XSSFSheet sheet = workbook.createSheet("Sheet 1");
|
||||
assertFalse(sheet.getScenarioProtect());
|
||||
}
|
||||
|
||||
/*
|
||||
public void testTopRowLeftCol() {
|
||||
XSSFWorkbook workbook = new XSSFWorkbook();
|
||||
XSSFSheet sheet = workbook.createSheet("Sheet 1");
|
||||
@ -533,7 +533,7 @@ public class TestXSSFSheet extends TestCase {
|
||||
assertEquals((short) 2, sheet.getTopRow());
|
||||
assertEquals((short) 26, sheet.getLeftCol());
|
||||
}
|
||||
|
||||
*/
|
||||
public void testShiftRows() {
|
||||
XSSFWorkbook workbook = new XSSFWorkbook();
|
||||
|
||||
@ -644,8 +644,8 @@ public class TestXSSFSheet extends TestCase {
|
||||
assertEquals(STPane.BOTTOM_RIGHT, ctWorksheet.getSheetViews().getSheetViewArray(0).getPane().getActivePane());
|
||||
sheet.createFreezePane(3, 6, 10, 10);
|
||||
assertEquals((double)3, ctWorksheet.getSheetViews().getSheetViewArray(0).getPane().getXSplit());
|
||||
assertEquals(10, sheet.getTopRow());
|
||||
assertEquals(10, sheet.getLeftCol());
|
||||
// assertEquals(10, sheet.getTopRow());
|
||||
// assertEquals(10, sheet.getLeftCol());
|
||||
sheet.createSplitPane(4, 8, 12, 12, 1);
|
||||
assertEquals((double)8, ctWorksheet.getSheetViews().getSheetViewArray(0).getPane().getYSplit());
|
||||
assertEquals(STPane.BOTTOM_RIGHT, ctWorksheet.getSheetViews().getSheetViewArray(0).getPane().getActivePane());
|
||||
@ -844,6 +844,11 @@ public class TestXSSFSheet extends TestCase {
|
||||
}
|
||||
|
||||
|
||||
public void testSetColumnGroupCollapsed(){
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void testColumnWidthCompatibility() {
|
||||
Workbook wb1 = new HSSFWorkbook();
|
||||
Workbook wb2 = new XSSFWorkbook();
|
||||
|
@ -169,7 +169,7 @@ public final class TestXSSFWorkbook extends TestCase {
|
||||
assertEquals("'"+sheetName+"'!$B$5:$F$10", retrievedPrintArea);
|
||||
}
|
||||
|
||||
public void _testRepeatingRowsAndColums() {
|
||||
public void testRepeatingRowsAndColums() {
|
||||
// First test that setting RR&C for same sheet more than once only creates a
|
||||
// single Print_Titles built-in record
|
||||
XSSFWorkbook wb = new XSSFWorkbook();
|
||||
@ -206,6 +206,10 @@ public final class TestXSSFWorkbook extends TestCase {
|
||||
assertEquals(XSSFName.BUILTIN_PRINT_TITLE, nr2.getNameName());
|
||||
assertEquals("'SecondSheet'!$B:$C,'SecondSheet'!$1:$1", nr2.getReference());
|
||||
|
||||
|
||||
nwb.setRepeatingRowsAndColumns(1, -1, -1, -1, -1);
|
||||
|
||||
|
||||
if (false) {
|
||||
// In case you fancy checking in excel, to ensure it
|
||||
// won't complain about the file now
|
||||
@ -214,13 +218,13 @@ public final class TestXSSFWorkbook extends TestCase {
|
||||
FileOutputStream fout = new FileOutputStream(tempFile);
|
||||
nwb.write(fout);
|
||||
fout.close();
|
||||
System.out.println("check out " + tempFile.getAbsolutePath());
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Tests that we can save a new document
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user