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;
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
*
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;
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;
}
}
+ @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() {