Excel Operation
在日常工作中, 常常需要收集统计一些数据, 然后整理到excel, 这种重复性的操作可以自己写个工具来实现。 采用HtmlUnitDriver 访问页面, 抓取数据, 再把数据列表通过调用POI放到excel。 这里先把操作excel 操作部分抽取出来, 拿到数据后, 可以直接调用该类实现存取操作。
package com.rc.qa.base.utils;
import java.io.FileOutputStream;
import java.util.Calendar;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.Hyperlink; /**
* @author jennifer.huang
* @date 2014/10/23
*
*/
public class ExcelOperation { private Workbook wb;
private CreationHelper createHelper;
private Sheet sheet;
private CellStyle titleCellStyle,dataCellStyle,linkCellStyle;
private int rowHeight; public ExcelOperation() {
init();
} public ExcelOperation(String []columns){
init();
this.createTitleRow(columns);
} public ExcelOperation(String []columns,CellStyle titleCellStyle){
init();
this.createTitleRow(columns, titleCellStyle);
} private void init(){
wb = new XSSFWorkbook();
createHelper = wb.getCreationHelper();
sheet = wb.createSheet();
this.createTitleStyle();
this.createDataStyle();
this.createLinkStyle();
rowHeight = 400;
} /**
* createTitleRow
* row: excel first rowId=0. createRow((short)0)
* @param columns
*/
public void createTitleRow(String []columns){
createTitleRow(columns, titleCellStyle);
}
public void createTitleRow(String []columns,CellStyle titleCellStyle){
sheet.createFreezePane( 0, 1, 0, 1 );
setColumnWidth(columns);
Row row = sheet.createRow((short)0);
setRowHeight(row,rowHeight);
for(int i=0;i<columns.length;i++){
createCell(row, i, columns[i], titleCellStyle);
}
} /**
* createDataRow
* @param rowId (can start from 1)
* @param columns
*/
public void createDataRow(int rowId,String []columns){
createDataRow(rowId,columns,dataCellStyle);
} public void createDataRow(int rowId, String []columns,CellStyle dataCellStyle){
setColumnWidth(columns);
Row tmpRow = sheet.createRow((short)rowId);
setRowHeight(tmpRow,rowHeight);
for(int i=0;i<columns.length;i++){
createCell(tmpRow, i, columns[i], dataCellStyle);
}
} /**
* setHylinkForCell
* @param rowId
* @param columnId
* @param link
*/
public void setHylinkForCell(int rowId, int columnId,Hyperlink link){
setHylinkForCell(rowId, columnId, link, linkCellStyle); }
public void setHylinkForCell(int rowId, int columnId,Hyperlink link,CellStyle linkCellStyle){
Row row = sheet.getRow(rowId);
Cell cell = row.getCell(columnId);
cell.setHyperlink(link);
cell.setCellStyle(linkCellStyle);
} /**
* saveToExcel
* @param savePath
*/
public void saveToExcel(String savePath){
Calendar c = Calendar.getInstance();
FileOutputStream fileOut;
try {
fileOut = new FileOutputStream(String.format(savePath+"\\Tasks_%d-%d-%d-%d-%d.xlsx", c.get(Calendar.YEAR), c.get(Calendar.MONTH)+1, c.get(Calendar.DATE), c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE)));
wb.write(fileOut);
fileOut.close();
} catch (Exception e) {
e.printStackTrace();
}
} private void setColumnWidth(String []columns){
for(int i=0;i<columns.length;i++){
sheet.autoSizeColumn((short)i);
}
} private void setRowHeight(Row row, int height){
row.setHeight((short)height);
} private CellStyle createTitleStyle(){
titleCellStyle = wb.createCellStyle();
Font titleFont = wb.createFont();
titleFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
titleFont.setFontHeightInPoints((short)12);
titleCellStyle.setFillForegroundColor(IndexedColors.SEA_GREEN.getIndex());
titleCellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
titleCellStyle.setBorderBottom(CellStyle.BORDER_THIN);
titleCellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
titleCellStyle.setBorderLeft(CellStyle.BORDER_THIN);
titleCellStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
titleCellStyle.setBorderRight(CellStyle.BORDER_THIN);
titleCellStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());
titleCellStyle.setBorderTop(CellStyle.BORDER_THIN);
titleCellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
titleCellStyle.setAlignment(CellStyle.VERTICAL_CENTER);
titleCellStyle.setFont(titleFont);
return titleCellStyle;
} private CellStyle createDataStyle(){
dataCellStyle = wb.createCellStyle();
dataCellStyle.setBorderBottom(CellStyle.BORDER_THIN);
dataCellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
dataCellStyle.setBorderLeft(CellStyle.BORDER_THIN);
dataCellStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
dataCellStyle.setBorderRight(CellStyle.BORDER_THIN);
dataCellStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());
dataCellStyle.setBorderTop(CellStyle.BORDER_THIN);
dataCellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
dataCellStyle.setAlignment(CellStyle.VERTICAL_CENTER);
return dataCellStyle;
} private CellStyle createLinkStyle(){
linkCellStyle = wb.createCellStyle();
linkCellStyle.cloneStyleFrom(dataCellStyle);
Font hlink_font = wb.createFont();
hlink_font.setUnderline(Font.U_SINGLE);
hlink_font.setColor(IndexedColors.BLUE.getIndex());
linkCellStyle.setFont(hlink_font);
return linkCellStyle;
} private Cell createCell(Row row, int cellId, String cellValue, CellStyle cellStyle){
Cell cell = row.createCell((short)cellId);
cell.setCellValue(cellValue);
cell.setCellStyle(cellStyle);
return cell;
} public Workbook getWb() {
return wb;
} public void setWb(Workbook wb) {
this.wb = wb;
} public CreationHelper getCreateHelper() {
return createHelper;
} public void setCreateHelper(CreationHelper createHelper) {
this.createHelper = createHelper;
} public Sheet getSheet() {
return sheet;
} public void setSheet(Sheet sheet) {
this.sheet = sheet;
} public CellStyle getTitleCellStyle() {
return titleCellStyle;
} public void setTitleCellStyle(CellStyle titleCellStyle) {
this.titleCellStyle = titleCellStyle;
} public CellStyle getDataCellStyle() {
return dataCellStyle;
} public void setDataCellStyle(CellStyle dataCellStyle) {
this.dataCellStyle = dataCellStyle;
} public CellStyle getLinkCellStyle() {
return linkCellStyle;
} public void setLinkCellStyle(CellStyle linkCellStyle) {
this.linkCellStyle = linkCellStyle;
} public int getRowHeight() {
return rowHeight;
} public void setRowHeight(int rowHeight) {
this.rowHeight = rowHeight;
} }
上面已经默认对excel 风格做了初始化, 可以直接调用做保存操作:
String []columns=new String[]{"Backend User Story","Test Case Key","User Stories","Site","Priority","Automation Keyword"};
ExcelOperation excelOperation = new ExcelOperation(columns);
public void saveToExcel(String savePath,List<Task> tasks){
Hyperlink link = excelOperation.getCreateHelper().createHyperlink(Hyperlink.LINK_URL);
int i=1;
for(Task task:tasks){
String []dataColumns=new String[]{task.getMainKeyword(),task.getTestCaseKey(),task.getUserStories(), task.getSite(),task.getPriority(),task.getAutomationStatus()};
link.setAddress(String.format(testCaseURL, task.getTestCaseId()));
excelOperation.createDataRow(i, dataColumns);
excelOperation.setHylinkForCell(i, 1, link);
i++;
}
excelOperation.saveToExcel(savePath);
}
POI需要的jar包:
POI3.7
poi-3.7-20101029.jar
poi-examples-3.7-20101029.jar
poi-ooxml-3.7-20101029.jar
poi-ooxml-schemas-3.7-20101029.jar
poi-scratchpad-3.7-20101029.jar
Excel Operation的更多相关文章
- excel copy cell & batch operation & checkbox
excel copy cell & batch operation & checkbox excel 右下角,下拉/双击 (复制 cell) 注意: 不是选择列
- 定时从多个Excel导入数据到SQL数据库
Scheduling Data Imports in SQL Server Importing data into a SQL Server database isn't really that tr ...
- Use Excel Pivot Table as a BI tool
Normally, we have created a table, view in database or cube in SSAS, user can use Excel as a BI tool ...
- Microsoft Office Excel 不能访问文件 的解决办法
Microsoft Office Excel 不能访问文件"a.xls". 可能的原因有: ? 文件名称或路径不存在. ? 文件正被其他程序使用. ? 您正要保存的工作簿与当前 ...
- Tutorial: Analyzing sales data from Excel and an OData feed
With Power BI Desktop, you can connect to all sorts of different data sources, then combine and shap ...
- C#与excel互操作的错误无法将类型为“Excel.ApplicationClass”的COM 对象强制转换为接口类型“Excel._Application”
如果您使用的电脑要操作的是office2003而之前使用过office2007使用此方法可解决您的问题 无法将类型为“Microsoft.Office.Interop.Excel.Applicatio ...
- excel中VBA对多个文件的操作
添加引用 "Scripting.FileSystemObject" (Microsoft Scripting Runtime) '用于操作文件.目录 Sub 数据整理部分() ' ...
- asp.net mvc4 easyui datagrid 增删改查分页 导出 先上传后导入 NPOI批量导入 导出EXCEL
效果图 数据库代码 create database CardManage use CardManage create table CardManage ( ID ,) primary key, use ...
- [转]Win7、Windows Server 2008下无法在Windows Service中打开一个已经存在的Excel 2007文件问题的解决方案
昨天,组里一个小朋友告诉我,他写的报表生成服务中无法打开一个已经存在的Excel 2007文件,他的开发环境是Win7.Visual Studio .Net 2008(Windows Server 2 ...
随机推荐
- Android Layout_Gravity和Gravity
简单来说layout_gravity表示子控件在父容器的位置,gravity表示控件内容在控件内的位置. 上面图片的xml代码 <?xml version="1.0" enc ...
- 一,彻底理解第一个C语言程序 Hello World
对于初学者来说,第一个程序一般都是hello world,而且是照着书上一点一点敲的.所以,在初学者眼中,敲出来的第一个程序代码不过是一堆看不懂的英语.而事实上,C语言作为一门语言,是有语法的.所以这 ...
- CustomerSOList
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- 在linux下编译netcat并且反弹cmdshell(转载)
本地Windows监听 nc -vv -l -p 1234 首先从sf上get一个tar的压缩包 wget http://sourceforge.net/projects/netcat/files ...
- BZOJ 3931: [CQOI2015]网络吞吐量 最大流
3931: [CQOI2015]网络吞吐量 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/p ...
- 实现经常使用的配置文件/初始化文件读取的一个C程序
在编程中,我们常常会遇到一些配置文件或初始化文件. 这些文件通常后缀名为.ini或者.conf.能够直接用记事本打开.里面会存储一些程序參数,在程序中直接读取使用.比如,计算机与server通信.se ...
- [Angular 2] Inject Service with "Providers"
In this lesson, we’re going to take a look at how add a class to the providers property of a compone ...
- INNOBACKUPEX热备MYSQL数据
http://www.databaseclub.com/2014/11/innobackupex/ 1)对MySQL进行全备份1.备份数据 1 innobackupex --user=userna ...
- oracle_partition sample_simple
一:范围分区 就是根据数据库表中某一字段的值的范围来划分分区,例如: create table graderecord ( sno varchar2(10), sname varchar2(20), ...
- Android Service与Activity之间通信的几种方式
在Android中,Activity主要负责前台页面的展示,Service主要负责需要长期运行的任务,所以在我们实际开发中,就会常常遇到Activity与Service之间的通信,我们一般在Activ ...