【发布时间】:2016-03-27 18:39:14
【问题描述】:
我的超链接文件位于 Whois 目录中,并且目录和 Excel 文件都在同一个父目录中。由于相对路径,我无法通过超链接访问我的文件。我需要将此 Excel 文件发送给多个收件人,而不更改他们的选项。我试过 getPath()、getCanonicalPath() 和 getAbsolutePath() 无济于事。这是我的代码:
public void writeTheFile() throws Exception {
int rowNum=-1;
String line = "";
XSSFWorkbook workBook = new XSSFWorkbook();
XSSFSheet sheet = workBook.createSheet("Contact & Whois");
XSSFCreationHelper helper = workBook.getCreationHelper();
BufferedReader br = new BufferedReader(new FileReader(INPUT_FILE));
while((line = br.readLine()) != null) {
rowNum++;
XSSFRow currentRow=sheet.createRow(rowNum);
currentRow.createCell(0).setCellValue(line.trim());
currentRow.createCell(1);
XSSFHyperlink file_link_downloads = helper.createHyperlink(Hyperlink.LINK_FILE);
Cell cell = sheet.getRow(rowNum).getCell(1);
try {
File f = new File("Whois/" + line + ".whois.txt");
if(f.exists()) {
cell.setCellValue("[Whois]");
String path = f.getPath();
file_link_downloads.setAddress(path);
cell.setHyperlink((org.apache.poi.ss.usermodel.Hyperlink) file_link_downloads);
} else {
cell.setCellValue("-NA-");
}
} catch (Exception e) {
System.out.println("Error in setting up download link");
e.printStackTrace();
}
}
Windows 路径正在逐字保存在另一台计算机上。
【问题讨论】:
标签: java excel hyperlink apache-poi