【java 菜鸟自动化实践之二】采用PIO将图片存入指定的excel单元格中(支持excel2003和2007)
2017-12-05 14:54
681 查看
实现目的:selenium实时截取浏览器图,并采用PIO将图片存入指定的excel单元格中(支持excel2003和2007)
参考地址:http://silencelyn.iteye.com/blog/2170520
package common;
import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFDrawing;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
/**
* User: XZee Date:
* 2011-10-10 Time: 16:10:29
*/
public class write_excel {
private static Logger log = Logger.getLogger(write_excel.class);
/** 总行数 */
private int totalRows = 0;
/** 总列数 */
private int totalCells = 0;
/** 错误信息 */
private static String errorInfo;
private static XSSFWorkbook workbook;
private static HSSFWorkbook workbook2;
private static WebDriver driver;
/**
* 构造方法
*
* @return
*/
public int getTotalRows() {
return totalRows;
}
public int getTotalCells() {
return totalCells;
}
public String getErrorInfo() {
return errorInfo;
}
public static boolean validateExcel(String filePath) {
/** 检查文件名是否为空或者是否是Excel格式的文件 */
if (filePath == null
|| !(isExcel2003(filePath) || isExcel2007(filePath))) {
errorInfo = "文件名不是excel格式";
return false;
}
/** 检查文件是否存在 */
File file = new File(filePath);
if (file == null || !file.exists()) {
errorInfo = "文件不存在";
return false;
}
return true;
}
public static boolean isExcel2003(String filePath) {
return filePath.matches("^.+\\.(?i)(xls)$");
}
public static boolean isExcel2007(String filePath) {
return filePath.matches("^.+\\.(?i)(xlsx)$");
}
public static void update_Excel_Screenshot(String filePath, int sheetIndex, int row, int col, File value) {
BufferedImage bufferImage = null;
FileOutputStream fileOutputStream = null;
try {
FileInputStream fis = new FileInputStream(filePath);
// 先把读入的图片放到第一个 ByteArrayOutputStream 中,用于产生ByteArray
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bufferImage = ImageIO.read(value);
ImageIO.write(bufferImage, "JPEG", byteArrayOutputStream);
if (!validateExcel(filePath)) {
log.error(errorInfo);
}
if (isExcel2007(filePath)) {
workbook = new XSSFWorkbook(fis);
XSSFSheet sheet = workbook.getSheetAt(sheetIndex);
// 准备插入图片
XSSFDrawing patriarch = sheet.createDrawingPatriarch();
int cols = col + 1;
int rows = row - 1;
XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, 100, 100, (short) col, rows, (short) cols, row);
// anchor.setAnchorType(3);
// 准备插入图片
byte[] pictureData = byteArrayOutputStream.toByteArray();
int pictureFormat = XSSFWorkbook.PICTURE_TYPE_JPEG;
int pictureIndex = workbook.addPicture(pictureData,pictureFormat);
patriarch.createPicture(anchor, pictureIndex);
} else if (isExcel2003(filePath)) {
workbook2 = new HSSFWorkbook(fis);
HSSFSheet sheet = workbook2.getSheetAt(sheetIndex);
HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
int cols = col + 1;
int rows = row - 1;
// 准备插入图片
HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 100, 100, (short) col, rows, (short) cols, row);
byte[] pictureData = byteArrayOutputStream.toByteArray();
int pictureFormat = HSSFWorkbook.PICTURE_TYPE_JPEG;
int pictureIndex = workbook.addPicture(pictureData, pictureFormat);
patriarch.createPicture(anchor, pictureIndex);
}
fileOutputStream = new FileOutputStream(filePath);
// 写入excel
workbook.write(fileOutputStream);
byteArrayOutputStream.close();
fileOutputStream.close();
fis.close();
} catch (Exception e) {
log.info(e.getMessage());
} finally {
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException io) {
log.info(io.getMessage());
}
}
}
}
// 实时截取页面图,并存入到excel中
public static void write_excel_Screenshot(String filePath, int i) throws Exception {
try {
// 截图
File screenShotFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
// 将图片存入第1行的第3列的单元格中
update_Excel_Screenshot(filePath, 0, i, 3, screenShotFile);
log.info("用例执行失败后,成功保存当前操作失败的图片!");
} catch (Exception e) {
log.error(e.getMessage());
}
}
public static void main(String[] args) throws Exception {
File file_chrome = new File("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");
System.setProperty("webdriver.chrome.driver", file_chrome.getAbsolutePath());
ChromeOptions options = new ChromeOptions();
options.addArguments("disable-infobars");
// 设置成用户自己的数据目录
options.addArguments("user-data-dir=C:\\Users\\Administrator\\AppData\\Local\\Google\\Chrome\\User Data");
// 打开chrome浏览器
WebDriver driver = new ChromeDriver(options);
System.out.println("End chrome browser...");
driver.get("http://www.baidu.com");
int row = 1; // 行
String filePath =".\\xxxxx\\xxxx.xlsx";
write_excel_Screenshot(filePath, row);
}
}
参考地址:http://silencelyn.iteye.com/blog/2170520
package common;
import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFDrawing;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
/**
* User: XZee Date:
* 2011-10-10 Time: 16:10:29
*/
public class write_excel {
private static Logger log = Logger.getLogger(write_excel.class);
/** 总行数 */
private int totalRows = 0;
/** 总列数 */
private int totalCells = 0;
/** 错误信息 */
private static String errorInfo;
private static XSSFWorkbook workbook;
private static HSSFWorkbook workbook2;
private static WebDriver driver;
/**
* 构造方法
*
* @return
*/
public int getTotalRows() {
return totalRows;
}
public int getTotalCells() {
return totalCells;
}
public String getErrorInfo() {
return errorInfo;
}
public static boolean validateExcel(String filePath) {
/** 检查文件名是否为空或者是否是Excel格式的文件 */
if (filePath == null
|| !(isExcel2003(filePath) || isExcel2007(filePath))) {
errorInfo = "文件名不是excel格式";
return false;
}
/** 检查文件是否存在 */
File file = new File(filePath);
if (file == null || !file.exists()) {
errorInfo = "文件不存在";
return false;
}
return true;
}
public static boolean isExcel2003(String filePath) {
return filePath.matches("^.+\\.(?i)(xls)$");
}
public static boolean isExcel2007(String filePath) {
return filePath.matches("^.+\\.(?i)(xlsx)$");
}
public static void update_Excel_Screenshot(String filePath, int sheetIndex, int row, int col, File value) {
BufferedImage bufferImage = null;
FileOutputStream fileOutputStream = null;
try {
FileInputStream fis = new FileInputStream(filePath);
// 先把读入的图片放到第一个 ByteArrayOutputStream 中,用于产生ByteArray
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bufferImage = ImageIO.read(value);
ImageIO.write(bufferImage, "JPEG", byteArrayOutputStream);
if (!validateExcel(filePath)) {
log.error(errorInfo);
}
if (isExcel2007(filePath)) {
workbook = new XSSFWorkbook(fis);
XSSFSheet sheet = workbook.getSheetAt(sheetIndex);
// 准备插入图片
XSSFDrawing patriarch = sheet.createDrawingPatriarch();
int cols = col + 1;
int rows = row - 1;
XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, 100, 100, (short) col, rows, (short) cols, row);
// anchor.setAnchorType(3);
// 准备插入图片
byte[] pictureData = byteArrayOutputStream.toByteArray();
int pictureFormat = XSSFWorkbook.PICTURE_TYPE_JPEG;
int pictureIndex = workbook.addPicture(pictureData,pictureFormat);
patriarch.createPicture(anchor, pictureIndex);
} else if (isExcel2003(filePath)) {
workbook2 = new HSSFWorkbook(fis);
HSSFSheet sheet = workbook2.getSheetAt(sheetIndex);
HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
int cols = col + 1;
int rows = row - 1;
// 准备插入图片
HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 100, 100, (short) col, rows, (short) cols, row);
byte[] pictureData = byteArrayOutputStream.toByteArray();
int pictureFormat = HSSFWorkbook.PICTURE_TYPE_JPEG;
int pictureIndex = workbook.addPicture(pictureData, pictureFormat);
patriarch.createPicture(anchor, pictureIndex);
}
fileOutputStream = new FileOutputStream(filePath);
// 写入excel
workbook.write(fileOutputStream);
byteArrayOutputStream.close();
fileOutputStream.close();
fis.close();
} catch (Exception e) {
log.info(e.getMessage());
} finally {
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException io) {
log.info(io.getMessage());
}
}
}
}
// 实时截取页面图,并存入到excel中
public static void write_excel_Screenshot(String filePath, int i) throws Exception {
try {
// 截图
File screenShotFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
// 将图片存入第1行的第3列的单元格中
update_Excel_Screenshot(filePath, 0, i, 3, screenShotFile);
log.info("用例执行失败后,成功保存当前操作失败的图片!");
} catch (Exception e) {
log.error(e.getMessage());
}
}
public static void main(String[] args) throws Exception {
File file_chrome = new File("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");
System.setProperty("webdriver.chrome.driver", file_chrome.getAbsolutePath());
ChromeOptions options = new ChromeOptions();
options.addArguments("disable-infobars");
// 设置成用户自己的数据目录
options.addArguments("user-data-dir=C:\\Users\\Administrator\\AppData\\Local\\Google\\Chrome\\User Data");
// 打开chrome浏览器
WebDriver driver = new ChromeDriver(options);
System.out.println("End chrome browser...");
driver.get("http://www.baidu.com");
int row = 1; // 行
String filePath =".\\xxxxx\\xxxx.xlsx";
write_excel_Screenshot(filePath, row);
}
}
相关文章推荐
- 【java 菜鸟自动化实践之一】实现PIO excel更新某个单元格数据(支持excel2003和2007)
- 【java 菜鸟实践<一>】实现PIO excel更新某个单元格数据(支持excel2003和2007)
- MIME生成EXCEL 包括图片的写入 支持EXCEL2003 2007 草稿
- 【JMeter 菜鸟实操之二】linux下采用Jenkins、Ant实施自动化测试集成方案(结合网上资料,纠正部分操作)
- 【java 菜鸟自动化实践之三】net.sf.json json递归 删除key和key值
- java 将pdf多页转换成一张图片,支持指定页数
- 【java 菜鸟自动化实践之四】将数据库查询数据,转为TestNG适用的对象二维数组数据
- Java 得到指定数据Excel2003 与2007需要注意的地方
- java将图片存入数据库
- 采用过滤器处理指定盘符下的图片显示
- java 压缩图片至指定大小
- JAVA实践生成验证码图片
- CKEditor 3.x 在Java中配置、包括图片上传、支持FTP、图片压缩
- python脚本爬取百度贴吧里的图片并下载到指定路径下,支持多页同时下载
- Java 解析excel2003和2007区别和兼容性问题(POI操作)
- jax-rs(Java API for RESTful Web Services)实践教程 之二 —— get/post/put/delete/head
- java学习从菜鸟到笨鸟之二——关于类的继承
- Java与编码问题串讲之二–如何理解java采用Unicode编码
- java 读取world的图片 并把图片路径存入数据库
- linux下采用shell脚本实现批量为指定文件夹下图片添加水印的方法