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 ...
随机推荐
- CSS3实现翻转菜单效果
演示地址 点击打开链接 注意:菜单翻转效果在搜狗浏览器上看不出来.推荐用FireFox <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1 ...
- 五种开源协议的比较(BSD,Apache,GPL,LGPL,MIT) – 整理
当Adobe.Microsoft.Sun等一系列巨头开始表现出对”开源”的青睐时,”开源”的时代即将到来! 最初来自:sinoprise.com/read.php?tid-662-page-e-fpa ...
- Apache POI 合并单元格
合并单元格所使用的方法: sheet.addMergedRegion( CellRangeAddress cellRangeAddress ); CellRangeAddress 对象的构造 ...
- Codeforces Round #327 (Div. 2) C. Median Smoothing 找规律
C. Median Smoothing Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/591/p ...
- C# 使用xsd文件验证XML 格式是否正确
C# 使用xsd文件验证XML 格式是否正确 核心示例代码: //创建xmlDocument XmlDocument doc = new XmlDocument(); //创建声明段 如<?xm ...
- CSS里的单位
CSS中预设了16种颜色以及16种颜色的衍生色,这16种颜色是CSS规范推荐的.并且一些主流的浏览器都可以识别.例如以下表所看到的: 十六进制颜色是最经常使用的定义方式.它的基本格式为#RRGGBB, ...
- 史上最浅显易懂的Git教程!
http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000
- 检测到有潜在危险的 Request.Form
今天在做一个.net的新闻发布器的时候. 遇到这样的一个问,在html编辑器里面加入图片提交的时候 就报一个 从客户端(content1="<img src="/web/ne ...
- oc-08-内存分析
说有对象公用类的一个方法
- Spark_Api_图解