Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
805 views
in Technique[技术] by (71.8m points)

selenium - Adding Link file using APACHE POI XSSF is not accepting directory address and showing java.net.URISyntaxException

I am trying to hyperlink a .png file in a cell content of any .xlsx file. Following is the part of code and it is showing java.net.URISyntaxException exception (seems because of slash used in the address). However changing link.setAddress("test.png") is not showing any error, but it is not resolving my purpose. Please help me.

    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();
    }

Ultimately what I need to do is to hyperlink a screenshot with any cell. The screenshot directory will be anywhere other than eclipse workspace.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Finally I have done this ...Thanks to Gagravarr... Given me a good Idea.

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();
    }
}

The Main problem that I have faced was related to the URL. If I used "" it is showing illegalArgumentException, while replacing it with "/" is working fine. I don't know the exact reason but if

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

is commented... it is showing illegal argument exception. So if the URL is

D:eclipseworkspaceTestAppScreenshotTestResultTest.png

will show the illegalArgumentException. However if "" is replaced by "/" to make it D:/eclipse/workspace/TestApp/Screenshot/TestResult/Test.png

it is working properly. Both the URL is correct and opening the same file in browser or even using manually in excel file for hyperlink the same file its working fine. I don't know why it is showing exception in Apache POI, may be a possible Bug.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...