Cách thêm Hyperlink vào file excel trong Apache POI

Trong bài viết này chúng ta sẽ cùng nhau tìm cách thêm Hyperlink vào một ô trong file excel sử dụng Apache POI. Thông thường hyperlink được dùng để liên kết một đoạn văn bản đến một địa chỉ trang web, một email hay dẫn đến một file nội bộ trong máy.

Dependency

Đầu tiên, chúng ta cần thêm phụ thuộc POI vào tệp pom.xml của dự án. Để làm việc với sổ làm việc Excel 2007+, chúng ta nên sử dụng poi-ooxml:

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>4.1.2</version>
</dependency>

Lưu ý rằng đối với các phiên bản Excel trước, chúng ta nên sử dụng phụ thuộc poi để thay thế.

Ví dụ tạo các hyperlink liên kết đến URL web, một file nội bộ và một email.

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.common.usermodel.Hyperlink;
import org.apache.poi.common.usermodel.HyperlinkType;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Color;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFHyperlink;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class Main {

    public static void main(String args[]) throws IOException {
        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet spreadsheet = workbook.createSheet("Hyperlinks");
        XSSFCell cell;
        CreationHelper createHelper = workbook.getCreationHelper();
        XSSFCellStyle hlinkstyle = workbook.createCellStyle();
        XSSFFont hlinkfont = workbook.createFont();
        hlinkfont.setUnderline(XSSFFont.U_SINGLE);
        hlinkstyle.setFont(hlinkfont);

        //URL Link
        cell = spreadsheet.createRow(1).createCell((short) 1);
        cell.setCellValue("URL Link");
        XSSFHyperlink link = (XSSFHyperlink)createHelper.createHyperlink(HyperlinkType.URL);
        link.setAddress("https://shareprogramming.net/");
        cell.setHyperlink((XSSFHyperlink) link);
        cell.setCellStyle(hlinkstyle);

        //Hyperlink to a file in the current directory
        cell = spreadsheet.createRow(2).createCell((short) 1);
        cell.setCellValue("File Link");
        XSSFHyperlink fileLink = (XSSFHyperlink)createHelper.createHyperlink(HyperlinkType.FILE);
        fileLink.setAddress("cellstyle.xlsx");
        cell.setHyperlink(fileLink);
        cell.setCellStyle(hlinkstyle);

        //e-mail link
        cell = spreadsheet.createRow(3).createCell((short) 1);
        cell.setCellValue("Email Link");
        XSSFHyperlink emailLink = (XSSFHyperlink)createHelper.createHyperlink(HyperlinkType.EMAIL);
        emailLink.setAddress("mailto:[email protected]");
        cell.setHyperlink(emailLink);
        cell.setCellStyle(hlinkstyle);

        FileOutputStream out = new FileOutputStream(new File("hyperlink.xlsx"));
        workbook.write(out);
        out.close();
        System.out.println("hyperlink.xlsx written successfully");
    }
}

Output

apache-poi-hyperlink

Nguồn

https://www.tutorialspoint.com/apache_poi/apache_poi_hyperlink.htm

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x