如何使用JAVA在XLS单元格中添加超链接

如何使用JAVA在XLS单元格中添加超链接,java,excel,hyperlink,xls,Java,Excel,Hyperlink,Xls,我需要在XLS单元中添加一个超链接,该超链接应使用Java链接到本地驱动器中的文件。这是我的密码 我需要将本地文件夹中的相应文件链接到XLs中的相应单元格 我试图添加超链接,但我只能在中添加URL,而不能从本地磁盘添加文件。请帮帮我 public boolean to_write_xls( int max, List <String> temp_1,List <String> temp_2,List <String> temp_3,List <Strin

我需要在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;
public boolean to_write_xls(int max,List temp_1,List temp_2,List temp_3,List temp_4,List temp_5){
HSSFWorkbook=新的HSSFWorkbook();
HSSFSheet sheet=workbook.createSheet(“分析结果”);
HSSFRow rowhead=sheet.createRow((短)0);
CellStyle style=workbook.createCellStyle();
风格:setFillForegroundColor(HSSFColor.ORANGE.index);
style.setFillPattern(CellStyle.SOLID\u前景);
style.setBorderBottom(HSSFCellStyle.BORDER_-MEDIUM);
style.setBorderTop(HSSFCellStyle.BORDER_-THICK);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
rowhead.createCell((短)0.setCellValue(“传递的TC”);
rowhead.createCell((short)1.setCellValue(“CRC:Failure”);
rowhead.createCell((短)2.setCellValue(“意外的RRC PDU”);
rowhead.createCell((short)3.setCellValue(“pcCallback错误”);
createCell((短)4.setCellValue(“背驮式NAS PDU”);

/*对于(int i=0;i开发人员可以通过调用hyperlinks集合的add方法向外部Excel文件添加超链接。add方法采用以下参数:

单元名称,表示将在其中添加超链接的单元名称

行数,表示此超链接范围中的行数

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

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

[爪哇]

//实例化工作簿对象

Workbook workbook = new Workbook();
//获取第一个工作表的引用

WorksheetCollection worksheets = workbook.getWorksheets();
Worksheet sheet = worksheets.get(0);
//设置“A1”单元格的值

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

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");

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

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%好

您可以使用以下java代码在excel工作表中创建超链接

File veri1=新文件(“您的文件路径”);
FileInputStream inputStream_ED1=新的FileInputStream(veri1);
HSSF工作簿_ED1=新的HSSF工作簿(输入流_ED1);
HSSFSheet sheet_ED1=工作簿_ED1.getSheet(“结果”);
CreationHelper createHelper=工作簿_ED1.getCreationHelper();
HSSFCellStyle hlinkstyle=工作簿_ED1.createCellStyle();
hssfont hlinkfont=工作簿_ED1.createFont();
hlinkfont.setUnderline(hssfont.U_SINGLE);
hlinkfont.setColor(HSSFColor.BLUE.index);
hlinkstyle.setFont(hlinkfont);
迭代器riterator_ED1=sheet_ED1.Iterator();
Row Row_ED1=sheet_ED1.createRow(sheet_ED1.getLastRowNum()+1);
如果(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=行_ED1.createCell(2);
SubModule1.setCellValue(SubModule.toString());
Cell ScenarioID1=行_ED1.createCell(3);
ScenarioID.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=行_ED1.createCell(7);
ActualResult1.setCellValue(ActualResult.toString());
Cell Status1=行\单位ED1.createCell(8);
Status1.setCellValue(Status.toString());
单元时间=行×ED1.createCell(9);
time.setCellValue(time.toString());
Cell ExecutionDate1=row_ED1.createCell(10);
ExecutionDate1.setCellValue(ExecutionDate.toString());
HSSFHyperlink link=(HSSFHyperlink)createHelper.createHyperlink(Hyperlink.link\uURL);
Cell ss=行_ED1.createCell((短)11);
ss.setCellValue(sspath.toString());
link=(HSSFHyperlink)createHelper.createHyperlink(Hyperlink.link\u文件);
link.setAddress(sspath.toString());
ss.setHyperlink(链接);
ss.setCellStyle(hlinkstyle);
文件输出流
os_ED1=新文件输出流(veri1);
练习册1.write(操作系统1);
os_ED1.close();
工作簿_ED1.close();
inputStream_ED1.close();

您确实需要提供更多信息。您尝试过什么?您使用什么框架生成xls文件?…这是一个古老且aleady回答过的问题,请您也解释一下您发布的代码,或者在代码中添加一些基本的注释,这样这个答案可以提供更多的价值?在Apache POI 4.1.1中,hype可以按如下方式创建链接:
Hyperlink link=workbook.getCreationHelper().createHyperlink(HyperlinkType.URL);
可以使用
hlink\u font.setColor(HSSFColor.HSSFColorPredefined.BLUE.getIndex())设置更多颜色;
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); 
 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();