From: PJ Fanning Date: Tue, 29 Mar 2022 23:30:26 +0000 (+0000) Subject: addHyperlink method on SXSSFSheet X-Git-Tag: REL_5_2_3~387 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=a7e1909c21d3d6550b6da5c83999140812bf6815;p=poi.git addHyperlink method on SXSSFSheet git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1899367 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/SXSSFSheet.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/SXSSFSheet.java index 38ff3d3e09..8b6ca89ce6 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/SXSSFSheet.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/SXSSFSheet.java @@ -28,20 +28,7 @@ import java.util.Spliterator; import java.util.TreeMap; import org.apache.poi.ss.SpreadsheetVersion; -import org.apache.poi.ss.usermodel.AutoFilter; -import org.apache.poi.ss.usermodel.Cell; -import org.apache.poi.ss.usermodel.CellRange; -import org.apache.poi.ss.usermodel.CellStyle; -import org.apache.poi.ss.usermodel.DataValidation; -import org.apache.poi.ss.usermodel.DataValidationHelper; -import org.apache.poi.ss.usermodel.Footer; -import org.apache.poi.ss.usermodel.Header; -import org.apache.poi.ss.usermodel.IndexedColors; -import org.apache.poi.ss.usermodel.PrintSetup; -import org.apache.poi.ss.usermodel.Row; -import org.apache.poi.ss.usermodel.Sheet; -import org.apache.poi.ss.usermodel.SheetConditionalFormatting; -import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.util.CellAddress; import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.ss.util.PaneInformation; @@ -1585,6 +1572,20 @@ public class SXSSFSheet implements Sheet, OoxmlSheetExtensions { return _sh.getHyperlink(addr); } + /** + * Register a hyperlink in the collection of hyperlinks on this sheet. + * Use {@link SXSSFCell#setHyperlink(Hyperlink)} if the hyperlink is just for that one cell. + * Use this method if you want to add a Hyperlink that covers a range of sells. If you use + * this method, you will need to call {@link XSSFHyperlink#setCellReference(String)} to + * explicitly cell the value, eg B2 or B2:C3 (the 4 cells with B2 at top left and C3 at bottom right) + * + * @param hyperlink the link to add + * @since POI 5.2.3 + */ + public void addHyperlink(XSSFHyperlink hyperlink) { + _sh.addHyperlink(hyperlink); + } + /** * Get a list of Hyperlinks in this sheet * diff --git a/poi-ooxml/src/test/java/org/apache/poi/xssf/streaming/TestSXSSFWorkbook.java b/poi-ooxml/src/test/java/org/apache/poi/xssf/streaming/TestSXSSFWorkbook.java index 089bbfb1b9..579ea290f2 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/xssf/streaming/TestSXSSFWorkbook.java +++ b/poi-ooxml/src/test/java/org/apache/poi/xssf/streaming/TestSXSSFWorkbook.java @@ -38,11 +38,13 @@ import java.util.Arrays; import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream; import org.apache.poi.POIDataSamples; +import org.apache.poi.common.usermodel.HyperlinkType; import org.apache.poi.openxml4j.opc.OPCPackage; import org.apache.poi.openxml4j.opc.PackageAccess; import org.apache.poi.ss.tests.usermodel.BaseTestXWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellType; +import org.apache.poi.ss.usermodel.Hyperlink; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; @@ -52,9 +54,12 @@ import org.apache.poi.xssf.SXSSFITestDataProvider; import org.apache.poi.xssf.XSSFTestDataSamples; import org.apache.poi.xssf.model.SharedStringsTable; import org.apache.poi.xssf.usermodel.DefaultIndexedColorMap; +import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFColor; import org.apache.poi.xssf.usermodel.XSSFFont; +import org.apache.poi.xssf.usermodel.XSSFHyperlink; import org.apache.poi.xssf.usermodel.XSSFRichTextString; +import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Disabled; @@ -530,6 +535,32 @@ public final class TestSXSSFWorkbook extends BaseTestXWorkbook { } } + @Test + void addHyperlink() throws Exception { + try ( + SXSSFWorkbook wb = new SXSSFWorkbook(); + UnsynchronizedByteArrayOutputStream bos = new UnsynchronizedByteArrayOutputStream() + ) { + SXSSFSheet sheet = wb.createSheet("s1"); + SXSSFRow row = sheet.createRow(0); + SXSSFCell cell = row.createCell(0); + cell.setCellValue("Example Website"); + XSSFHyperlink hyperlink = (XSSFHyperlink)wb.getCreationHelper().createHyperlink(HyperlinkType.URL); + hyperlink.setAddress("http://example.com"); + hyperlink.setCellReference("A1"); + sheet.addHyperlink(hyperlink); + wb.write(bos); + + try (XSSFWorkbook xssfWorkbook = new XSSFWorkbook(bos.toInputStream())) { + XSSFSheet xssfSheet = xssfWorkbook.getSheet(sheet.getSheetName()); + XSSFCell xssfCell = xssfSheet.getRow(0).getCell(0); + assertEquals("Example Website", xssfCell.getStringCellValue()); + XSSFHyperlink xssfHyperlink = xssfCell.getHyperlink(); + assertEquals(hyperlink.getAddress(), xssfHyperlink.getAddress()); + } + } + } + @Override @Disabled("not implemented") protected void changeSheetNameWithSharedFormulas() {