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 ...
随机推荐
- ADO.NET 快速入门(十三):使用 OLE DB 检索数据
OleDbDataReader 类提供了一种从数据源读取数据记录只进流的方法.如果想使用 SQL Server 7.0 或者更高版本,请参考文章:使用 SQL Server 检索数据. OleDb ...
- 从hello-world 开始 <contiki学习之四>
按照contiki 官方给出的example下的例子之hello world来说,所有的工程里面都有一个唯一的Makefile.然后这个Makefile会去调用其他makefile文件.于是,一切就从 ...
- PC/UVa 题号: 110105/10267 Graphical Editor (图形化编辑器)题解
#include<cstdio> #include<iostream> #include<string> #include<algorithm> #in ...
- MySQL常用查询语句集合《转》
一查询数值型数据: SELECT * FROM tb_name WHERE sum > 100; 查询谓词:>,=,<,<>,!=,!>,!<,=>,= ...
- 【转】linux中的常见目录及文件
1. /proc目录 Linux 内核提供了一种通过 /proc 文件系统,在运行时访问内核内部数据结构.改变内核设置的机制.proc文件系统是一个伪文件系统,它只存在内存当中,而不占用外存空间.它以 ...
- TListView列表拒绝添加重复信息
//TListView列表拒绝添加重复信息 procedure TForm1.Button1Click(Sender: TObject);var i: Integer;begin if (Tr ...
- [MODX] 2. Chunks $
Chunk in Modx can cut your template into samll pieces to make code reuseable. [[$chunk_name]] For ex ...
- PHP实现Web Service(转)
1.OAuth OAUTH协议为用户资源的授权提供了一个安全的.开放而又简易的标准.与以往的授权方式不同之处是OAUTH的授权不会使第三方触及到用户的帐号信息(如用户名与密码) 2.SCA 服务组件 ...
- mysql的两阶段提交协议
http://www.cnblogs.com/hustcat/p/3577584.html 前两天和百度的一个同学聊MySQL两阶段提交,当时自信满满的说了一堆,后来发现还是有些问题的理解还是比较 ...
- 琐碎-关于hadoop2.X那些端口
此文转载http://www.aboutyun.com/thread-7513-1-1.html Hadoop集群的各部分一般都会使用到多个端口,有些是daemon之间进行交互之用,有些是用于RPC访 ...