How to create hyperlink to a filter on other sheet using Apache poi in Java?

*爱你&永不变心* 提交于 2019-12-08 04:28:15

问题


I want to create a hyperlink from a field 'Name' on Sheet1 (Summary) to the AutoFilter on column 'Name' on Sheet2 (Details), in order to display the details of that particular name only on the Sheet2.

I have imported :

import org.apache.poi.ss.usermodel.Hyperlink;
import org.apache.poi.ss.usermodel.CreationHelper;

Hyperlink to filter on other sheet.

I have done this using VB macro, but want to implement this using Java POI.


回答1:


I had troubles with Hyperlinks some time ago, and the quickest way to do that (I was in a hurry too!) is the following:

row.createCell(cellIdx, HSSFCell.CELL_TYPE_FORMULA).setCellFormula(String.format("HYPERLINK(%s; \"%s\")", sheetAndCellLocation, friendlyText));



回答2:


To insert hyperlink into cell in Excel sheet using Apache POI we need CreationHelper, we need to get CreationHelper from WorkBook (like XSSFWorkBook). Code snippet:

CreationHelper creationHelper = getWorkbook().getCreationHelper();
Hyperlink link = creationHelper.createHyperlink(Hyperlink.LINK_URL);
link.setAddress("www.google.com");

There are other types of HyperLink available in Apache POI one of them is LINK_URL. also we can apply style to hyperlink created by default it will be blue and underlined.

XSSFCellStyle hLinkStyle = getWorkbook().createCellStyle();
Font hLinkFont = getWorkbook().createFont();
hLinkFont.setFontName("Ariel");
hLinkFont.setUnderline(Font.U_SINGLE);
hLinkFont.setColor(IndexedColors.BLUE.getIndex() );
hLinkStyle.setFont(hLinkFont);



回答3:


Not supported with JAVA POI - Till what I found..



来源:https://stackoverflow.com/questions/6728945/how-to-create-hyperlink-to-a-filter-on-other-sheet-using-apache-poi-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!