简体   繁体   English

JXL使用Java从Excel读取超链接单元格

[英]JXL read Hyperlink cell from Excel using Java

I am trying to read a hyperlink cell from my Excel spreadsheet but I'm unable to do so. 我正在尝试从Excel电子表格中读取超链接单元格,但无法这样做。 When I go into the spreadsheet and remove the hyperlink it reads just fine. 当我进入电子表格并删除超链接时,它看起来就很好。 I did come across a solution below in another question ( How to get hyperlink address from a cell in excel by using java? ) but the getHyperlink method only works with the sheet and not the cell which is confusing me. 我确实在下面的另一个问题中遇到了一个解决方案( 如何通过使用Java从e​​xcel中的单元格中获取超链接地址? ),但是getHyperlink方法仅适用于工作表,而不适用于使我感到困惑的单元格。

Workbook wb = WorkbookFactory.create(new File("test.xls"));
Sheet s = wb.getSheetAt(0);
Row r2 = s.getRow(1); // Rows in POI are 0 based
Cell cB2 = r2.getCell(1); // Cells are 0 based

Hyperlink h = cB2.getHyperlink();
if (h == null) {
   System.err.println("Cell B2 didn't have a hyperlink!");
} else {
   System.out.println("B2 : " + h.getLabel() + " -> " + h.getAddress());
}

This is my code just now 这是我的代码

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

public String[] readUsernameFromExcel() {
        File src = new File("C:/filepath.xls");
        String ex[] = new String[10];

        try {

            Workbook wb = Workbook.getWorkbook(src);
            Sheet sh1 = wb.getSheet(0);

            Cell a3 = sh1.getCell(0, 2);
            Cell b3 = sh1.getCell(1,2);


            Cell c2 = sh1.getCell(2,1); //this is the cell I want to read the hyperlink from

            ex[0] = a3.getContents().trim();
            ex[1] = b3.getContents().trim();
            ex[2] = c2.getContents().trim();

            System.out.println(ex[0]);
            System.out.println(ex[1]);
            System.out.println(ex[2]);

So what I have tried to do is 所以我试图做的是

Hyperlink h = c2.getHyperlink(); 

But I getHyperlink does not work when used with a cell. 但是我getHyperlink与单元格一起使用时不起作用。

在此处输入图片说明

And I dont have the option to add getHyperlink() method 而且我没有选择添加getHyperlink()方法

在此处输入图片说明

but when I use the sheet it does appear, although appears as hyperlinks and is an array. 但是当我使用工作表时,它确实出现了,尽管显示为超链接并且是一个数组。

在此处输入图片说明

I feel as though I'm so close but I just cant figure out what I'm missing or doing wrong so any help to get me over the link is greatly appreciated, thanks in advance! 我感觉好像我已经很接近了,但是我无法弄清我丢失了什么或做错了什么,所以非常感谢您帮助我通过链接,谢谢您!

I just decided to use the Apache POI instead as that's where the getHyperlink method is. 我刚决定使用Apache POI,因为那是getHyperlink方法所在的地方。 And it works like a charm. 它就像一种魅力。 I've posted my code below in the hope someone finds it useful if they find themselves in the same situation in trying to change from JXL to Apache POI! 我在下面发布了我的代码,希望有人在尝试从JXL更改为Apache POI时处于相同的情况下,会发现它很有用! Also if you're using .xslx then you need to use XSSF instead of HSSF ( http://www.codejava.net/coding/how-to-read-excel-files-in-java-using-apache-poi ) 另外,如果您使用的是.xslx,则需要使用XSSF而不是HSSF( http://www.codejava.net/coding/how-to-read-excel-files-in-java-using-apache-poi

 public String[] readUsernameFromExcel() {
        String ex[] = new String[10];
        try {

            FileInputStream fileInputStream = new FileInputStream("filepath.xls");
            HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);
            HSSFSheet worksheet = workbook.getSheetAt(0);
            HSSFRow row1 = worksheet.getRow(2);
            HSSFCell cellA3 = row1.getCell(0);
            ex[0] = String.valueOf(cellA3.getNumericCellValue()).trim();

            HSSFRow row2 = worksheet.getRow(2);
            HSSFCell cellB3 = row2.getCell(1);
            ex[1] = cellB3.getStringCellValue();

            HSSFRow row3 = worksheet.getRow(1);

            HSSFCell cellC2 = row3.getCell(2);
            Hyperlink h = cellC2.getHyperlink();
            ex[2] = h.getAddress();

            System.out.println("A3: " + ex[0]);
            System.out.println("B3: " + ex[1]);
            System.out.println("C2: " + ex[2]);

        }catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();}


return ex;

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM