diff options
author | Nick Burch <nick@apache.org> | 2008-04-03 00:23:59 +0000 |
---|---|---|
committer | Nick Burch <nick@apache.org> | 2008-04-03 00:23:59 +0000 |
commit | 21154b1bbfcbd21eccd8e7d2d3a11028b110789f (patch) | |
tree | b2619c87f144df183c56e8571f636a8ac9962823 /src/ooxml | |
parent | c496f727c95e6b9928063e7b4b9a17cbae577764 (diff) | |
download | poi-21154b1bbfcbd21eccd8e7d2d3a11028b110789f.tar.gz poi-21154b1bbfcbd21eccd8e7d2d3a11028b110789f.zip |
New ooxml4j, and finish getting xssf comments to be saved and re-loaded again
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@644118 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/ooxml')
3 files changed, 34 insertions, 3 deletions
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 ded600fc39..94b4f536e2 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java @@ -919,6 +919,13 @@ public class XSSFSheet implements Sheet { } return sheetComments; } + /** + * Returns the sheet's comments object if there is one, + * or null if not + */ + protected CommentsSource getCommentsSourceIfExists() { + return sheetComments; + } /** * Does this sheet have any comments on it? We need to know, 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 cbaa92b8c1..e308a7c1a0 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java @@ -133,6 +133,18 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { public String getContentType() { return TYPE; } public String getRelation() { return REL; } public String getDefaultFileName() { return DEFAULT_NAME; } + + /** + * Returns the filename for the nth one of these, + * eg /xl/comments4.xml + */ + public String getFileName(int index) { + if(DEFAULT_NAME.indexOf("#") == -1) { + // Generic filename in all cases + return getDefaultFileName(); + } + return DEFAULT_NAME.replace("#", Integer.toString(index)); + } /** * Fetches the InputStream to read the contents, based @@ -667,7 +679,8 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { for (int i=0 ; i < this.getNumberOfSheets(); i++) { int sheetNumber = (i+1); XSSFSheet sheet = (XSSFSheet) this.getSheetAt(i); - PackagePartName partName = PackagingURIHelper.createPartName("/xl/worksheets/sheet" + sheetNumber + ".xml"); + PackagePartName partName = PackagingURIHelper.createPartName( + WORKSHEET.getFileName(sheetNumber)); PackageRelationship rel = corePart.addRelationship(partName, TargetMode.INTERNAL, WORKSHEET.getRelation(), "rSheet" + sheetNumber); PackagePart part = pkg.createPart(partName, WORKSHEET.getContentType()); @@ -683,7 +696,17 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { workbook.getSheets().getSheetArray(i).setSheetId(sheetNumber); // If our sheet has comments, then write out those - // TODO + if(sheet.hasComments()) { + CommentsTable ct = (CommentsTable)sheet.getCommentsSourceIfExists(); + PackagePartName ctName = PackagingURIHelper.createPartName( + SHEET_COMMENTS.getFileName(sheetNumber)); + part.addRelationship(ctName, TargetMode.INTERNAL, SHEET_COMMENTS.getRelation(), "rComments"); + PackagePart ctPart = pkg.createPart(ctName, SHEET_COMMENTS.getContentType()); + + out = ctPart.getOutputStream(); + ct.writeTo(out); + out.close(); + } } // Write shared strings and styles diff --git a/src/ooxml/testcases/org/apache/poi/xssf/model/TestCommentsTable.java b/src/ooxml/testcases/org/apache/poi/xssf/model/TestCommentsTable.java index 268af09198..413c96b837 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/model/TestCommentsTable.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/model/TestCommentsTable.java @@ -20,6 +20,7 @@ package org.apache.poi.xssf.model; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; +import java.io.FileOutputStream; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Comment; @@ -128,7 +129,7 @@ public class TestCommentsTable extends TestCase { assertEquals(2, cc7.getColumn()); } - public void DISABLEDtestWriteRead() throws Exception { + public void testWriteRead() throws Exception { File xml = new File( System.getProperty("HSSF.testdata.path") + File.separator + "WithVariousData.xlsx" |