Fix implementation of XSSFWorkbook#cloneSheet and provide unit test.
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@614571 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
9db93d7661
commit
936e16ac26
@ -86,6 +86,10 @@ public class XSSFSheet implements Sheet {
|
|||||||
hf.setOddFooter("&C&\"Arial\"&10Page &P");
|
hf.setOddFooter("&C&\"Arial\"&10Page &P");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected CTSheet getSheet() {
|
||||||
|
return this.sheet;
|
||||||
|
}
|
||||||
|
|
||||||
protected CTWorksheet getWorksheet() {
|
protected CTWorksheet getWorksheet() {
|
||||||
return this.worksheet;
|
return this.worksheet;
|
||||||
}
|
}
|
||||||
@ -601,4 +605,9 @@ public class XSSFSheet implements Sheet {
|
|||||||
}
|
}
|
||||||
return views.getSheetViewArray(views.getSheetViewArray().length - 1);
|
return views.getSheetViewArray(views.getSheetViewArray().length - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected XSSFSheet cloneSheet() {
|
||||||
|
return new XSSFSheet((CTSheet) sheet.copy());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -79,7 +79,34 @@ public class XSSFWorkbook implements Workbook {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Sheet cloneSheet(int sheetNum) {
|
public Sheet cloneSheet(int sheetNum) {
|
||||||
// TODO Auto-generated method stub
|
XSSFSheet srcSheet = sheets.get(sheetNum);
|
||||||
|
String srcName = getSheetName(sheetNum);
|
||||||
|
if (srcSheet != null) {
|
||||||
|
XSSFSheet clonedSheet = srcSheet.cloneSheet();
|
||||||
|
|
||||||
|
sheets.add(clonedSheet);
|
||||||
|
CTSheet newcts = this.workbook.getSheets().addNewSheet();
|
||||||
|
newcts.set(clonedSheet.getSheet());
|
||||||
|
|
||||||
|
int i = 1;
|
||||||
|
while (true) {
|
||||||
|
//Try and find the next sheet name that is unique
|
||||||
|
String name = srcName;
|
||||||
|
String index = Integer.toString(i++);
|
||||||
|
if (name.length() + index.length() + 2 < 31) {
|
||||||
|
name = name + "("+index+")";
|
||||||
|
} else {
|
||||||
|
name = name.substring(0, 31 - index.length() - 2) + "(" +index + ")";
|
||||||
|
}
|
||||||
|
|
||||||
|
//If the sheet name is unique, then set it otherwise move on to the next number.
|
||||||
|
if (getSheetIndex(name) == -1) {
|
||||||
|
setSheetName(sheets.size() - 1, name);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return clonedSheet;
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -246,8 +273,7 @@ public class XSSFWorkbook implements Workbook {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public String getSheetName(int sheet) {
|
public String getSheetName(int sheet) {
|
||||||
// TODO Auto-generated method stub
|
return this.workbook.getSheets().getSheetArray(sheet).getName();
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void insertChartRecord() {
|
public void insertChartRecord() {
|
||||||
@ -311,13 +337,11 @@ public class XSSFWorkbook implements Workbook {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setSheetName(int sheet, String name) {
|
public void setSheetName(int sheet, String name) {
|
||||||
// TODO Auto-generated method stub
|
this.workbook.getSheets().getSheetArray(sheet).setName(name);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setSheetName(int sheet, String name, short encoding) {
|
public void setSheetName(int sheet, String name, short encoding) {
|
||||||
// TODO Auto-generated method stub
|
this.workbook.getSheets().getSheetArray(sheet).setName(name);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setSheetOrder(String sheetname, int pos) {
|
public void setSheetOrder(String sheetname, int pos) {
|
||||||
|
@ -70,4 +70,24 @@ public class TestXSSFWorkbook extends TestCase {
|
|||||||
workbook.setSelectedTab((short) 1);
|
workbook.setSelectedTab((short) 1);
|
||||||
assertEquals(1, workbook.getSelectedTab());
|
assertEquals(1, workbook.getSelectedTab());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testSetSheetName() throws Exception {
|
||||||
|
XSSFWorkbook workbook = new XSSFWorkbook();
|
||||||
|
Sheet sheet1 = workbook.createSheet("sheet1");
|
||||||
|
assertEquals("sheet1", workbook.getSheetName(0));
|
||||||
|
workbook.setSheetName(0, "sheet2");
|
||||||
|
assertEquals("sheet2", workbook.getSheetName(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testCloneSheet() throws Exception {
|
||||||
|
XSSFWorkbook workbook = new XSSFWorkbook();
|
||||||
|
Sheet sheet1 = workbook.createSheet("sheet");
|
||||||
|
Sheet sheet2 = workbook.cloneSheet(0);
|
||||||
|
assertEquals(2, workbook.getNumberOfSheets());
|
||||||
|
assertEquals("sheet(1)", workbook.getSheetName(1));
|
||||||
|
workbook.setSheetName(1, "clonedsheet");
|
||||||
|
Sheet sheet3 = workbook.cloneSheet(1);
|
||||||
|
assertEquals(3, workbook.getNumberOfSheets());
|
||||||
|
assertEquals("clonedsheet(1)", workbook.getSheetName(2));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user