【问题标题】:How to add a hyperlink in XLS cell using JAVA如何使用 JAVA 在 XLS 单元格中添加超链接
【发布时间】:2014-04-05 14:50:42
【问题描述】:

我需要在 XLS 单元格中添加一个超链接,该超链接应该使用 Java 链接到我本地驱动器中的文件。这是我的代码。

我需要将本地文件夹中的相应文件链接到 XLs 中的相应单元格。

我尝试添加超链接,但我只能在本地磁盘中的文件中添加 URL。请帮帮我

public boolean to_write_xls( int max, List <String> temp_1,List <String> temp_2,List <String> temp_3,List <String> temp_4,List <String> temp_5 ) {

HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Analyzed Result");
HSSFRow rowhead = sheet.createRow((short) 0);
CellStyle style = workbook.createCellStyle();
style.setFillForegroundColor(HSSFColor.ORANGE.index);
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
style.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM);
style.setBorderTop(HSSFCellStyle.BORDER_THICK);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);

    rowhead.createCell((short) 0).setCellValue("Passed TC's     ");
    rowhead.createCell((short) 1).setCellValue("CRC:Failure     ");
    rowhead.createCell((short) 2).setCellValue("unexpected RRC PDU");
    rowhead.createCell((short) 3).setCellValue("PCallback Error ");
    rowhead.createCell((short) 4).setCellValue("Piggybacked NAS PDU");

/*    for (int i=0; i<5; i++){
       // sheet.setColumnWidth(i,4000);
        sheet.autoSizeColumn((short)i);


    }*/
    Iterator<Cell> ct = rowhead.iterator();
    int i=0;
    while(ct.hasNext()){
        Cell cell = (Cell) ct.next();
        cell.setCellStyle(style);
        sheet.autoSizeColumn((short)i);
        i ++;
    }
    CellStyle style_r = workbook.createCellStyle();
    style_r.setBorderBottom(HSSFCellStyle.BORDER_THIN);
    style_r.setBorderTop(HSSFCellStyle.BORDER_THIN);
    style_r.setBorderRight(HSSFCellStyle.BORDER_THIN);
    style_r.setBorderLeft(HSSFCellStyle.BORDER_THIN);

    i=0;
    while (i < max ) {

        HSSFRow row = sheet.createRow((short) i+2);

        row.createCell((short) 0).setCellValue(temp_1.get(i));
        row.createCell((short) 1).setCellValue(temp_2.get(i));
        row.createCell((short) 2).setCellValue(temp_3.get(i));
        row.createCell((short) 3).setCellValue(temp_4.get(i));
        row.createCell((short) 4).setCellValue(temp_5.get(i));

        Iterator<Cell> rw = row.iterator();
         while(rw.hasNext()){
            Cell cell = (Cell) rw.next();
            cell.setCellStyle(style_r);
         }
        i++;
        }      




    try {

         FileOutputStream Fout =
     new FileOutputStream(new File(fin+"\\Result.xls"));
     workbook.write(Fout);
     Fout.close();
     //System.out.println("Excel written successfully..with the file name directory-----> D:\\_Analyzed_Result\\Result.xls");
         Runtime.getRuntime().exec("cmd /c start "+fin+"\\Result.xls"); 
     } catch (Exception e) {
     e.printStackTrace();

     return false;
 }


return true;

}

【问题讨论】:

  • 您确实需要提供更多信息。你试过什么?你用什么框架来生成 xls 文件? ...

标签: java excel hyperlink xls


【解决方案1】:

以上代码不工作。所以我发现其他一些工作正常的代码,用于在单元格中添加超链接。

CellStyle hlink_style = workbook.createCellStyle();
Font hlink_font = workbook.createFont();
hlink_font.setUnderline(Font.U_SINGLE);
hlink_font.setColor(Font.COLOR_RED);
hlink_style.setFont(hlink_font);
Hyperlink link = createHelper.createHyperlink(Hyperlink.LINK_FILE);
Cell cell = null;     
cell=row.createCell((short) 1);
cell.setCellValue("Go to Result");
path_f="D://Result.xls";
link.setAddress(path_f);        
cell.setHyperlink(link);    
cell.setCellStyle(hlink_style); 

100% 没问题!!

【讨论】:

  • 在 Apache POI 4.1.1 中,可以按如下方式创建超链接:Hyperlink link = workbook.getCreationHelper().createHyperlink(HyperlinkType.URL); 并且可以使用hlink_font.setColor(HSSFColor.HSSFColorPredefined.BLUE.getIndex()); 设置更多颜色
【解决方案2】:

开发者可以通过调用 Hyperlinks 集合的 Add 方法来添加外部 Excel 文件的超链接。 Add 方法采用以下参数:

Cell Name ,表示要添加超链接的单元格名称

Number of Rows ,表示此超链接范围内的行数

列数,表示此超链接范围的列数

URL ,表示将用作超链接的外部 Excel 文件的地址

[Java]

//实例化工作簿对象

Workbook workbook = new Workbook();

//获取第一个工作表的引用。

WorksheetCollection worksheets = workbook.getWorksheets();
Worksheet sheet = worksheets.get(0);

//给“A1”单元格设置一个值

Cells cells = sheet.getCells();
Cell cell = cells.get("A1");
cell.setValue("Visit Aspose");

//设置单元格的字体颜色为蓝色

Style style = cell.getStyle();
style.getFont().setColor(Color.getBlue());

//设置单元格字体为单下划线

style.getFont().setUnderline(FontUnderlineType.SINGLE);
cell.setStyle(style);

HyperlinkCollection hyperlinks = sheet.getHyperlinks();

//添加外部文件的链接

hyperlinks.add("A5", 1, 1, "C:\\book1.xls");

//保存Excel文件

workbook.save("c:\\book2.xls");

【讨论】:

    【解决方案3】:

    您可以使用此 java 代码在 Excel 表中创建超链接

     File veri1 = new File("your_ file_path");
                        FileInputStream inputStream_ED1 = new FileInputStream(veri1);
                        HSSFWorkbook workbook_ED1 = new HSSFWorkbook(inputStream_ED1);
                        HSSFSheet sheet_ED1 = workbook_ED1.getSheet("Result");
                        CreationHelper createHelper = workbook_ED1.getCreationHelper();
                        HSSFCellStyle hlinkstyle = workbook_ED1.createCellStyle();
                        HSSFFont hlinkfont = workbook_ED1.createFont();
                        hlinkfont.setUnderline(HSSFFont.U_SINGLE);
                        hlinkfont.setColor(HSSFColor.BLUE.index);
                        hlinkstyle.setFont(hlinkfont);
                        Iterator<Row> riterator_ED1 = sheet_ED1.iterator();
                        Row row_ED1 = sheet_ED1.createRow(sheet_ED1.getLastRowNum()+1);
                        if(sheet_ED1.getLastRowNum()==0){
    
                        }
    
    
                     Cell DeviceName = row_ED1.createCell(0);
                     DeviceName.setCellValue(DeviceID.toString());   
    
                     Cell Module = row_ED1.createCell(1);
                     Module.setCellValue(module.toString());
    
                     Cell SubModule1 = row_ED1.createCell(2);
                     SubModule1.setCellValue(SubModule.toString());
    
    
                     Cell ScenarioID1 = row_ED1.createCell(3);
                     ScenarioID1.setCellValue(ScenarioID.toString());
    
                     Cell TestcaseID = row_ED1.createCell(4);
                     TestcaseID.setCellValue(TestCaseID.toString());
    
                     Cell TCDescription = row_ED1.createCell(5);
                     TCDescription.setCellValue(testcasedis.toString());
    
                     Cell ExpectedResult1 = row_ED1.createCell(6);
                     ExpectedResult1.setCellValue(ExpectedResult.toString());
    
                     Cell ActualResult1 = row_ED1.createCell(7);
                     ActualResult1.setCellValue(ActualResult.toString());
    
                     Cell Status1 = row_ED1.createCell(8);
                     Status1.setCellValue(Status.toString());
    
                     Cell time = row_ED1.createCell(9);
                     time.setCellValue(Time.toString());
    
                     Cell ExecutionDate1 = row_ED1.createCell(10);
                     ExecutionDate1.setCellValue(ExecutionDate.toString());
    
                     HSSFHyperlink link = (HSSFHyperlink)createHelper.createHyperlink(Hyperlink.LINK_URL);
                     Cell ss = row_ED1.createCell((short) 11);
                     ss.setCellValue(sspath.toString());
                     link = (HSSFHyperlink)createHelper.createHyperlink(Hyperlink.LINK_FILE);
                     link.setAddress(sspath.toString());
                     ss.setHyperlink(link);
                     ss.setCellStyle(hlinkstyle);
    
                     FileOutputStream 
    
    
        os_ED1 = new FileOutputStream(veri1);
                     workbook_ED1.write(os_ED1);
    
                     os_ED1.close();
                     workbook_ED1.close();
                     inputStream_ED1.close();
    

    【讨论】:

    • 这是一个古老的问题,您能否解释一下您发布的代码,或者在代码中添加一些基本的 cmets,所以这个答案可以提供更多的价值?
    猜你喜欢
    • 2014-12-03
    • 1970-01-01
    • 2017-05-02
    • 1970-01-01
    • 1970-01-01
    • 2018-10-20
    • 2016-12-28
    • 2019-06-02
    • 2015-02-11
    相关资源
    最近更新 更多