使用APACHE POI XSSF添加链接文件不接受目录地址,并显示java.net.URISyntaxExceptio



我试图在任何。xlsx文件的单元格内容超链接。png文件。下面是代码的一部分,它显示java.net.URISyntaxException异常(似乎是因为地址中使用了斜杠)。然而,改变link.setAddress("test.png")没有显示任何错误,但它没有解决我的目的。请帮帮我。

    public static void main(String[]args) throws Exception{
       XSSFWorkbook wb = new XSSFWorkbook();
       CreationHelper createHelper = wb.getCreationHelper();
       CellStyle hlink_style = wb.createCellStyle();
       Font hlink_font = wb.createFont();
       hlink_font.setUnderline(Font.U_SINGLE);
       hlink_font.setColor(IndexedColors.BLUE.getIndex());
       hlink_style.setFont(hlink_font);
       XSSFSheet sheet = wb.createSheet("Hyperlinks");
        XSSFCell cell = sheet.createRow(1).createCell((short)0);
       cell.setCellValue("File Link");
       Hyperlink link = createHelper.createHyperlink(Hyperlink.LINK_FILE);
       link.setAddress("H:\Selenium\XL\src\santosh\xlwork\test.png");
       cell.setHyperlink(link);
       cell.setCellStyle(hlink_style);
       FileOutputStream out = new FileOutputStream("hyperlinks.xlsx");
       wb.write(out);
       out.close();
    }

最终我需要做的是超链接截图与任何单元格。截图目录将在eclipse工作空间以外的任何地方。

最后我做到了…感谢Gagravarr…给了我一个好主意。

boolean isDirCreated = false;//to create Screenshot directory just once
//Create Screenshot Directory.
public static void createDir(String ScreenshotDirAddress){
    if(!isDirCreated){
       File file= new File(ScreenshotDirAddress);
       if (!file.exists())
            file.mkdirs();
    isDirCreated=true;
    }
}

//hyperlink screenshot
public static void hyperlinkScreenshot(XSSFCell cell, String FileAddress){
    XSSFWorkbook wb=cell.getRow().getSheet().getWorkbook();
    CreationHelper createHelper = wb.getCreationHelper();
    CellStyle hlink_style = wb.createCellStyle();
    Font hlink_font = wb.createFont();
    hlink_font.setUnderline(Font.U_SINGLE);
    hlink_font.setColor(IndexedColors.BLUE.getIndex());
    hlink_style.setFont(hlink_font);
    Hyperlink hp = createHelper.createHyperlink(Hyperlink.LINK_FILE);
    FileAddress=FileAddress.replace("\", "/");
    hp.setAddress(FileAddress);
    cell.setHyperlink(hp);
    cell.setCellStyle(hlink_style);
}
//take screenshot
public static void takeScreenShot(WebDriver driver, String screenshotName, XSSFCell cell){
    createDir();
    File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
    try {
        String FullAddress=System.getProperty("user.dir")+"/"+ScreenshotDirAddress+"/"+screenshotName+".png";
        FileUtils.copyFile(scrFile, new File(FullAddress));
        hyperlinkScreenshot(cell, FullAddress);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

我所面临的主要问题与URL有关。如果我使用"",它会显示illegalArgumentException,而将其替换为"/"则可以正常工作。我不知道确切的原因,但是如果

        FileAddress=FileAddress.replace("\", "/");

评论……它显示了非法参数异常。如果URL是

D: eclipse workspace TestApp 截图 TestResult Test.png

将显示illegalArgumentException。但是,如果将""替换为"/"以使其 D:/eclipse/工作区/TestApp/截图/TestResult/Test.png

工作正常。URL都是正确的,在浏览器中打开相同的文件,甚至在excel文件中手动使用超链接相同的文件,其工作良好。我不知道为什么它在Apache POI中显示异常,可能是一个可能的Bug。

public static String takeScreenShot(XSSFRow wrow, XSSFCell cell,String fileName)
    {
        try {
            DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm:ss");
            LocalDateTime now = LocalDateTime.now();    
            fileName = fileName + dtf.format(now);
            fileName=fileName.replace(":","");
            File src = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
            String screen_path;
            ReadConfigProperty file = new ReadConfigProperty();
            FileUtils.copyFile(src, new File("Screenshot\" + fileName + ".jpg"));
            screen_path= file.reportFilePath() + fileName + ".jpg";

            screen_path= "file:///"+screen_path;
            cell = wrow.createCell(7);
            //cell.setCellValue(screen_path);

            XSSFWorkbook wb=cell.getRow().getSheet().getWorkbook();
            CreationHelper createHelper = wb.getCreationHelper();
            XSSFCellStyle hlink_style = wb.createCellStyle();
            XSSFFont hlink_font = wb.createFont();
            hlink_font.setUnderline(Font.U_SINGLE);
            hlink_font.setColor(IndexedColors.BLUE.getIndex());
            hlink_style.setFont(hlink_font);
            Hyperlink hp = createHelper.createHyperlink(Hyperlink.LINK_FILE);
            screen_path=screen_path.replace("\", "/");
            hp.setAddress(screen_path);
            cell.setHyperlink((org.apache.poi.ss.usermodel.Hyperlink) hp);
            cell.setCellStyle(hlink_style);
            return screen_path;
        }catch (Exception e) {
            e.printStackTrace();
        }
        return fileName;
    }

最新更新