]> source.dussan.org Git - poi.git/commitdiff
addHyperlink method on SXSSFSheet
authorPJ Fanning <fanningpj@apache.org>
Tue, 29 Mar 2022 23:30:26 +0000 (23:30 +0000)
committerPJ Fanning <fanningpj@apache.org>
Tue, 29 Mar 2022 23:30:26 +0000 (23:30 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1899367 13f79535-47bb-0310-9956-ffa450edef68

poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/SXSSFSheet.java
poi-ooxml/src/test/java/org/apache/poi/xssf/streaming/TestSXSSFWorkbook.java

index 38ff3d3e09558ea9ae58a8c8c7c00642f8aefdbb..8b6ca89ce6e58b7ede5095a22b2dc2c425799213 100644 (file)
@@ -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
      *
index 089bbfb1b9a602ac35bf6875ff9cddacc4ec68b7..579ea290f2dfd19fe51678e80920792d30e1fd5f 100644 (file)
@@ -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() {