diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java index 9ffa352eb..9c2762142 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java @@ -86,6 +86,10 @@ public class XSSFSheet implements Sheet { hf.setOddFooter("&C&\"Arial\"&10Page &P"); } + protected CTSheet getSheet() { + return this.sheet; + } + protected CTWorksheet getWorksheet() { return this.worksheet; } @@ -601,4 +605,9 @@ public class XSSFSheet implements Sheet { } return views.getSheetViewArray(views.getSheetViewArray().length - 1); } + + protected XSSFSheet cloneSheet() { + return new XSSFSheet((CTSheet) sheet.copy()); + } + } diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java index 20f604be7..a7af4ade3 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java @@ -79,7 +79,34 @@ public class XSSFWorkbook implements Workbook { } 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; } @@ -246,8 +273,7 @@ public class XSSFWorkbook implements Workbook { } public String getSheetName(int sheet) { - // TODO Auto-generated method stub - return null; + return this.workbook.getSheets().getSheetArray(sheet).getName(); } public void insertChartRecord() { @@ -311,13 +337,11 @@ public class XSSFWorkbook implements Workbook { } 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) { - // TODO Auto-generated method stub - + this.workbook.getSheets().getSheetArray(sheet).setName(name); } public void setSheetOrder(String sheetname, int pos) { diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java index bf2c218cb..e739b5449 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java @@ -70,4 +70,24 @@ public class TestXSSFWorkbook extends TestCase { workbook.setSelectedTab((short) 1); 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)); + } }