2016-09-20 4 views
0

웹 페이지에서 요소의 스크린 샷을 캡처하려고하는데 캡처 된 스크린 샷을 Excel 시트의 셀에 쓰려고합니다.셀렌을 사용하여 Excel 시트의 셀에 이미지 추가

아래 코드를 찾으십시오. 당신은 "4 그 두 개의 매개 변수를 교체해야

// **Label lab = new Label(0, 0,);** 
    // wsheet.addCell(lab); 

    WritableImage image = new WritableImage(0, 0, 4, 6, screenshotLocation); 
    wsheet.addImage(image); 

    // Parameters: 
    // x - the column number at which to position the image 
    // y - the row number at which to position the image 
    // width - the number of columns cells which the image spans 
    // height - the number of rows which the image spans 
    // image - the source image file 

: 나는 당신이 아래의 방법을 사용해야

+0

링크를 확인하십시오. http://stackoverflow.com/questions/28238078/insert-image-in-column-to-excel-using-apache-poi 이 정보는 도움이 될 것이라고 생각합니다. –

답변

1

라벨

WebDriver driver; 
String baseUrl = "http://www.google.com"; 

@Test 
public void test() throws IOException, BiffException, RowsExceededException, WriteException { 
    WebElement ele = driver.findElement(By.id("hplogo")); 

    // Get entire page screenshot 
    File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); 
    BufferedImage fullImg = ImageIO.read(screenshot); 

    // Get the location of element on the page 
    Point point = ele.getLocation(); 

    // Get width and height of the element 
    int eleWidth = ele.getSize().getWidth(); 
    int eleHeight = ele.getSize().getHeight(); 

    // Crop the entire page screenshot to get only element screenshot 
    BufferedImage eleScreenshot = fullImg.getSubimage(point.getX(), point.getY(), eleWidth, eleHeight); 
    ImageIO.write(eleScreenshot, "png", screenshot); 

    // Copy the element screenshot to disk 
    File screenshotLocation = new File("D:\\GoogleLogo_screenshot.png"); 
    FileUtils.copyFile(screenshot, screenshotLocation); 

    String excelPath = "D:\\" + appName + ".xls"; 
    File file = new File(excelPath); 
    WritableWorkbook wbook = Workbook.createWorkbook(file); 
    WritableSheet wsheet = wbook.createSheet("Seeds", 0); 

    **Label lab = new Label(0, 0,);** 

    wsheet.addCell(lab); 
    wbook.write(); 
    wbook.close(); 

} 

}의 세 번째 매개 변수에 전달하는 결정할 수 없어요 , 실제 크기는 6 "입니다.

관련 문제