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.Hyperlink;
import org.apache.poi.ss.usermodel.RichTextString;
/**
*
* @param link hypelrink associated with this cell
*/
- public void setHyperlink(HSSFHyperlink link){
+ public void setHyperlink(Hyperlink hyperlink){
+ HSSFHyperlink link = (HSSFHyperlink)hyperlink;
+
link.setFirstRow(record.getRow());
link.setLastRow(record.getRow());
link.setFirstColumn(record.getColumn());
return dataFormat;
}
- public HSSFHyperlink createHyperlink(int type, Sheet sheetFor) {
- return createHyperlink(type);
- }
public HSSFHyperlink createHyperlink(int type) {
return new HSSFHyperlink(type);
}
*\r
* @return the 0-based column of the first cell that contains the hyperlink\r
*/\r
- public short getFirstColumn(){\r
+ public int getFirstColumn(){\r
return record.getFirstColumn();\r
}\r
\r
*\r
* @param col the 0-based column of the first cell that contains the hyperlink\r
*/\r
- public void setFirstColumn(short col){\r
- record.setFirstColumn(col);\r
+ public void setFirstColumn(int col){\r
+ record.setFirstColumn((short)col);\r
}\r
\r
/**\r
*\r
* @return the 0-based column of the last cell that contains the hyperlink\r
*/\r
- public short getLastColumn(){\r
+ public int getLastColumn(){\r
return record.getLastColumn();\r
}\r
\r
*\r
* @param col the 0-based column of the last cell that contains the hyperlink\r
*/\r
- public void setLastColumn(short col){\r
- record.setLastColumn(col);\r
+ public void setLastColumn(int col){\r
+ record.setLastColumn((short)col);\r
}\r
\r
/**\r
package org.apache.poi.ss.usermodel;
+import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
/**
byte getErrorCellValue();
String getCellFormula();
+ HSSFCellStyle getCellStyle();
+
boolean getBooleanCellValue();
double getNumericCellValue();
HSSFRichTextString getRichStringCellValue();
package org.apache.poi.ss.usermodel;
-public interface CellStyle {}
+public interface CellStyle {
+ short getDataFormat();
+ String getDataFormatString();
+}
import java.util.Calendar;
import java.util.Date;
-
public interface Cell {
/**
*/
Comment getCellComment();
+ /**
+ * Returns hyperlink associated with this cell
+ *
+ * @return hyperlink associated with this cell or null if not found
+ */
+ public Hyperlink getHyperlink();
+
+ /**
+ * Assign a hypelrink to this cell
+ *
+ * @param link hypelrink associated with this cell
+ */
+ public void setHyperlink(Hyperlink link);
}
\ No newline at end of file
DataFormat createDataFormat();
/**
- * Creates a new Hyperlink, of the given type,
- * for the given sheet
+ * Creates a new Hyperlink, of the given type
*/
- Hyperlink createHyperlink(int type, Sheet sheetFor);
+ Hyperlink createHyperlink(int type);
}
\ No newline at end of file
*
* @return the 0-based column of the first cell that contains the hyperlink
*/
- public short getFirstColumn();
+ public int getFirstColumn();
/**
* 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(short col);
+ public void setFirstColumn(int col);
/**
* 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 short getLastColumn();
+ public int getLastColumn();
/**
* 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(short col);
+ public void setLastColumn(int col);
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.Hyperlink;
import org.apache.poi.ss.usermodel.RichTextString;
import org.apache.poi.ss.usermodel.SharedStringSource;
import org.apache.poi.ss.usermodel.StylesSource;
public RichTextString createRichTextString(String text) {
return new XSSFRichTextString(text);
}
+
+ public Hyperlink getHyperlink() {
+ return row.getSheet().getHyperlink(row.getRowNum(), cellNum);
+ }
+ public void setHyperlink(Hyperlink hyperlink) {
+ row.getSheet().setCellHyperlink((XSSFHyperlink)hyperlink);
+ }
}
import org.apache.poi.ss.usermodel.DataFormat;
import org.apache.poi.ss.usermodel.Hyperlink;
import org.apache.poi.ss.usermodel.RichTextString;
-import org.apache.poi.ss.usermodel.Sheet;
public class XSSFCreationHelper implements CreationHelper {
private XSSFWorkbook workbook;
return dataFormat;
}
- public Hyperlink createHyperlink(int type, Sheet sheetFor) {
- return new XSSFHyperlink(type, (XSSFSheet)sheetFor);
+ public Hyperlink createHyperlink(int type) {
+ return new XSSFHyperlink(type);
}
}
==================================================================== */
package org.apache.poi.xssf.usermodel;
+import java.net.URI;
+
import org.apache.poi.ss.usermodel.Hyperlink;
+import org.apache.poi.ss.util.CellReference;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTHyperlink;
import org.openxml4j.opc.Package;
import org.openxml4j.opc.PackagePart;
+import org.openxml4j.opc.PackageRelationship;
+
/**
* XSSF Implementation of a Hyperlink.
*/
public class XSSFHyperlink implements Hyperlink {
private int type;
- private XSSFSheet sheet;
+ private PackageRelationship externalRel;
private CTHyperlink ctHyperlink;
+ private String location;
- protected XSSFHyperlink(int type, XSSFSheet sheet) {
+ protected XSSFHyperlink(int type) {
this.type = type;
- this.sheet = sheet;
this.ctHyperlink = CTHyperlink.Factory.newInstance();
}
- protected XSSFHyperlink(CTHyperlink ctHyperlink, XSSFSheet sheet) {
- this.sheet = sheet;
+ protected XSSFHyperlink(CTHyperlink ctHyperlink, PackageRelationship hyperlinkRel) {
this.ctHyperlink = ctHyperlink;
+ this.externalRel = hyperlinkRel;
// Figure out the Hyperlink type
- // TODO
+
+ // 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);
+ }
+ }
+ // TODO
+
+ //URI target = externalRel.getTargetURI();
+ //location = target.toString();
+ }
}
/**
* this hyperlink?
*/
public boolean needsRelationToo() {
- // TODO
- return false;
+ return (type != Hyperlink.LINK_DOCUMENT);
}
/**
* Generates the relation if required
*/
- protected void generateRelationIfNeeded(Package pkg, PackagePart sheetPart) {
- // TODO
+ protected void generateRelationIfNeeded(PackagePart sheetPart) {
+ if(needsRelationToo()) {
+ // TODO
+ }
}
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() {
- // TODO Auto-generated method stub
- return null;
+ return location;
}
public String getLabel() {
- // TODO Auto-generated method stub
- return null;
+ return ctHyperlink.getDisplay();
}
public void setLabel(String label) {
- // TODO Auto-generated method stub
+ ctHyperlink.setDisplay(label);
}
public void setAddress(String address) {
- // TODO Auto-generated method stub
-
+ location = address;
+ }
+
+ private CellReference buildCellReference() {
+ return new CellReference(ctHyperlink.getRef());
}
- public short getFirstColumn() {
- // TODO Auto-generated method stub
- return 0;
+ public int getFirstColumn() {
+ return buildCellReference().getCol();
}
- public int getFirstRow() {
- // TODO Auto-generated method stub
- return 0;
+ public int getLastColumn() {
+ return buildCellReference().getCol();
}
- public short getLastColumn() {
- // TODO Auto-generated method stub
- return 0;
+
+ public int getFirstRow() {
+ return buildCellReference().getRow();
}
public int getLastRow() {
- // TODO Auto-generated method stub
- return 0;
+ return buildCellReference().getRow();
}
- public void setFirstColumn(short col) {
- // TODO Auto-generated method stub
+ public void setFirstColumn(int col) {
+ ctHyperlink.setRef(
+ new CellReference(
+ getFirstRow(), col
+ ).formatAsString()
+ );
}
- public void setFirstRow(int row) {
- // TODO Auto-generated method stub
+ public void setLastColumn(int col) {
+ setFirstColumn(col);
}
- public void setLastColumn(short col) {
- // TODO Auto-generated method stub
+ public void setFirstRow(int row) {
+ ctHyperlink.setRef(
+ new CellReference(
+ row, getFirstColumn()
+ ).formatAsString()
+ );
}
public void setLastRow(int row) {
- // TODO Auto-generated method stub
+ setFirstRow(row);
}
}
import java.io.IOException;
import java.io.OutputStream;
+import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import org.apache.poi.ss.usermodel.CommentsSource;
import org.apache.poi.ss.usermodel.Footer;
import org.apache.poi.ss.usermodel.Header;
+import org.apache.poi.ss.usermodel.Hyperlink;
import org.apache.poi.ss.usermodel.Patriarch;
import org.apache.poi.ss.usermodel.PrintSetup;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.model.CommentsTable;
import org.apache.poi.xssf.usermodel.helpers.ColumnHelper;
import org.apache.xmlbeans.XmlOptions;
+import org.openxml4j.opc.PackagePart;
+import org.openxml4j.opc.PackageRelationship;
+import org.openxml4j.opc.PackageRelationshipCollection;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBreak;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCols;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDialogsheet;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTHeaderFooter;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTHyperlink;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPageBreak;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPageMargins;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPageSetUpPr;
protected CTWorksheet worksheet;
protected CTDialogsheet dialogsheet;
protected List<Row> rows;
+ protected List<XSSFHyperlink> hyperlinks;
protected ColumnHelper columnHelper;
protected XSSFWorkbook workbook;
protected CommentsSource sheetComments;
}
initRows(this.worksheet);
initColumns(this.worksheet);
+
+ hyperlinks = new ArrayList<XSSFHyperlink>();
}
public XSSFSheet(XSSFWorkbook workbook) {
this.workbook = workbook;
+
+ hyperlinks = new ArrayList<XSSFHyperlink>();
}
public XSSFWorkbook getWorkbook() {
* will accept without a massive huff, and write into
* the OutputStream supplied.
*/
- protected void save(OutputStream out, XmlOptions xmlOptions) throws IOException {
+ protected void save(PackagePart sheetPart, XmlOptions xmlOptions) throws IOException {
// Excel objects to <cols/>
if(worksheet.getColsArray().length == 1) {
CTCols col = worksheet.getColsArray(0);
worksheet.setColsArray(null);
}
}
+
+ // Now re-generate our CTHyperlinks, if needed
+ if(hyperlinks.size() > 0) {
+ if(worksheet.getHyperlinks() == null) {
+ worksheet.addNewHyperlinks();
+ }
+ CTHyperlink[] ctHls = new CTHyperlink[hyperlinks.size()];
+ for(int i=0; i<ctHls.length; i++) {
+ // If our sheet has hyperlinks, have them add
+ // any relationships that they might need
+ XSSFHyperlink hyperlink = hyperlinks.get(i);
+ hyperlink.generateRelationIfNeeded(sheetPart);
+ // Now grab their underling object
+ ctHls[i] = hyperlink.getCTHyperlink();
+ }
+ worksheet.getHyperlinks().setHyperlinkArray(ctHls);
+ }
// Save
+ OutputStream out = sheetPart.getOutputStream();
worksheet.save(out, xmlOptions);
+ out.close();
}
protected CTWorksheet getWorksheet() {
protected void initColumns(CTWorksheet worksheet) {
columnHelper = new ColumnHelper(worksheet);
}
-
+
+ protected void initHyperlinks(PackageRelationshipCollection hyperRels) {
+ if(worksheet.getHyperlinks() == null) return;
+
+ // Turn each one into a XSSFHyperlink
+ for(CTHyperlink hyperlink : worksheet.getHyperlinks().getHyperlinkArray()) {
+ PackageRelationship hyperRel = null;
+ if(hyperlink.getId() != null) {
+ hyperRel = hyperRels.getRelationshipByID(hyperlink.getId());
+ }
+
+ // TODO: fix openxml4j
+// hyperlinks.add(
+// new XSSFHyperlink(hyperlink, hyperRel)
+// );
+ }
+ }
+
protected CTSheet getSheet() {
return this.sheet;
}
public Comment getCellComment(int row, int column) {
return getComments().findCellComment(row, column);
}
+
+ public Hyperlink getHyperlink(int row, int column) {
+ String ref = new CellReference(row, column).formatAsString();
+ for(XSSFHyperlink hyperlink : hyperlinks) {
+ if(hyperlink.getCellRef().equals(ref)) {
+ return hyperlink;
+ }
+ }
+ return null;
+ }
public short[] getColumnBreaks() {
CTBreak[] brkArray = getSheetTypeColumnBreaks().getBrkArray();
// TODO Auto-generated method stub
return 0;
}
+
+ public int getNumHyperlinks() {
+ return hyperlinks.size();
+ }
public boolean getObjectProtect() {
// TODO Auto-generated method stub
getComments().setCellComment(cellRef, comment);
}
+ public void setCellHyperlink(XSSFHyperlink hyperlink) {
+ hyperlinks.add(hyperlink);
+ }
+
public String getActiveCell() {
return getSheetTypeSelection().getActiveCell();
}
WorksheetDocument worksheetDoc = WorksheetDocument.Factory.parse(part.getInputStream());
XSSFSheet sheet = new XSSFSheet(ctSheet, worksheetDoc.getWorksheet(), this, comments);
this.sheets.add(sheet);
+
+ // Process external hyperlinks for the sheet,
+ // if there are any
+ PackageRelationshipCollection hyperlinkRels =
+ part.getRelationshipsByType(SHEET_HYPERLINKS.REL);
+ sheet.initHyperlinks(hyperlinkRels);
}
} catch (XmlException e) {
throw new IOException(e.toString());
// XXX This should not be needed, but apparently the setSaveOuter call above does not work in XMLBeans 2.2
xmlOptions.setSaveSyntheticDocumentElement(new QName(CTWorksheet.type.getName().getNamespaceURI(), "worksheet"));
- out = part.getOutputStream();
- sheet.save(out, xmlOptions);
- out.close();
+ sheet.save(part, xmlOptions);
// Update our internal reference for the package part
workbook.getSheets().getSheetArray(i).setId(rel.getId());
--- /dev/null
+/* ====================================================================
+ 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;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+
+import junit.framework.TestCase;
+
+import org.openxml4j.opc.Package;
+
+public class TestXSSFHyperlink extends TestCase {
+ public TestXSSFHyperlink(String name) {
+ super(name);
+
+ // Use system out logger
+ System.setProperty(
+ "org.apache.poi.util.POILogger",
+ "org.apache.poi.util.SystemOutLogger"
+ );
+ }
+
+ public void testAddNew() throws Exception {
+
+ }
+
+ public void testLoadExisting() throws Exception {
+ File xml = new File(
+ System.getProperty("HSSF.testdata.path") +
+ File.separator + "WithMoreVariousData.xlsx"
+ );
+ assertTrue(xml.exists());
+
+ XSSFWorkbook workbook = new XSSFWorkbook(xml.toString());
+ assertEquals(3, workbook.getNumberOfSheets());
+
+ XSSFSheet sheet = (XSSFSheet)workbook.getSheetAt(0);
+
+ // TODO - check hyperlinks
+ //assertEquals(4, sheet.getNumHyperlinks());
+ }
+
+ public void testLoadSave() throws Exception {
+ File xml = new File(
+ System.getProperty("HSSF.testdata.path") +
+ File.separator + "WithMoreVariousData.xlsx"
+ );
+ assertTrue(xml.exists());
+
+ XSSFWorkbook workbook = new XSSFWorkbook(xml.toString());
+ assertEquals(3, workbook.getNumberOfSheets());
+
+ // TODO - check hyperlinks
+
+ // Write out, and check
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ workbook.write(baos);
+ ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
+
+ // Load up again, check all links still there
+ XSSFWorkbook wb2 = new XSSFWorkbook(Package.open(bais));
+ assertEquals(3, wb2.getNumberOfSheets());
+ assertNotNull(wb2.getSheetAt(0));
+ assertNotNull(wb2.getSheetAt(1));
+ assertNotNull(wb2.getSheetAt(2));
+
+ // TODO
+ }
+
+}