我需要在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;
}
答案 0 :(得分:2)
以上代码无效。所以我找到了其他一些工作正常的代码,用于在单元格中添加超链接。
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%罚款!!
答案 1 :(得分:0)
开发人员可以通过调用Hyperlinks集合的Add方法向外部Excel文件添加超链接。 Add方法采用以下参数:
单元格名称,表示将添加超链接的单元名称
行数,表示此超链接范围内的行数
列数,表示此超链接范围的列数
URL,表示将用作超链接的外部Excel文件的地址
[爪哇]
//实例化工作簿对象
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");
答案 2 :(得分:0)
您可以使用此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();