调用jxl包实现Excel表格数据的读取,代码如下:

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap; import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException; /**
* @Description: Excel数据处理(使用中)
* @author
* @date 创建时间:2016年10月11日下午12:49:46
* @version 1.0
*/
public class ExcelData {
private Workbook workbook;
private Sheet sheet;
private int rows;
private int columns;
private String fileName;
private String caseName;
private ArrayList<String> arrkey = new ArrayList<String>();
private String sourceFile; private Log log = new Log(this.getClass());
/**
* @param fileName excel文件名
* @param caseName sheet名
*/
public ExcelData(String fileName, String caseName) {
super();
this.fileName = fileName;
this.caseName = caseName;
} /**
* 获得excel表中的数据
*/
public Object[][] getExcelData() throws BiffException, IOException {
workbook = Workbook.getWorkbook(new File(getPath()));
sheet = workbook.getSheet(caseName);
rows = sheet.getRows();
columns = sheet.getColumns();
// 为了返回值是Object[][],定义一个多行单列的二维数组
HashMap<String, String>[][] arrmap = new HashMap[rows - 1][1];
// 对数组中所有元素hashmap进行初始化
if (rows > 1) {
for (int i = 0; i < rows - 1; i++) {
arrmap[i][0] = new HashMap<String , String>();
}
} else {
log.error("excel中没有数据");
} // 获得首行的列名,作为hashmap的key值
for (int c = 0; c < columns; c++) {
String cellvalue = sheet.getCell(c, 0).getContents();
arrkey.add(cellvalue);
}
// 遍历所有的单元格的值添加到hashmap中
for (int r = 1; r < rows; r++) {
for (int c = 0; c < columns; c++) {
String cellvalue = sheet.getCell(c, r).getContents();
arrmap[r - 1][0].put(arrkey.get(c), cellvalue);
}
}
return arrmap;
} /**
* 获得excel文件的路径
* @return
* @throws java.io.IOException
*/ public String getPath() throws IOException {
sourceFile = "test-data/" + fileName + ".xls";
log.info("sourceFile = "+sourceFile);
return sourceFile;
} }

调用方式如下:

    /**
* 从对应Excel表格读取数据
*
* @param
* @return
* @throws Exception
*/
@DataProvider(name = "componentMessageQueryCase")
private Object[][] QueryComponentMessageNumbers() throws Exception {
// 获取Excel数据,得到一个map,依次传给test
ExcelData e = new ExcelData("ComponentMessageTestCase", "componentMessageQueryCase");
log.info("componentMessageQueryCase e.getExcelData() = " + e.getExcelData());
return e.getExcelData();
}
/**
* 测试配件管理页面的查询功能
*
* @param data 从对应Excel获取的测试数据
* @return
* @throws Exception
*/
@Test(dataProvider = "componentMessageQueryCase")
private void componentMessageQueryCase(HashMap<String, String> data) throws Exception{
log.info("测试配件查询功能"); String OEMCode = data.get("OEMCode");
String brandName = data.get("brandName");
String seriesId = data.get("seriesId");
String modelId = data.get("modelId");
String componentNo = data.get("componentNo");
String componentName = data.get("componentName");
ComponentManageQuery.componentMessageQuery( OEMCode, brandName, seriesId, modelId, componentNo, componentName);
}

jxl读取Excel表格数据的更多相关文章

  1. 利用 pandas库读取excel表格数据

    利用 pandas库读取excel表格数据 初入IT行业,愿与大家一起学习,共同进步,有问题请指出!! 还在为数据读取而头疼呢,请看下方简洁介绍: 数据来源为国家统计局网站下载: 具体方法 代码: i ...

  2. go读取excel表格数据

    go读取excel表格数据 使用工具 github.com/Luxurioust/excelize 百度到的都是使用这个 实际上已经改名了 github.com/360EntSecGroup-Skyl ...

  3. C#调用NPOI组件读取excel表格数据转为datatable写入word表格中并向word中插入图片/文字/书签 获得书签列表

    调用word的com组件将400条数据导入word表格中耗时10分钟简直不能忍受,使用NPOI组件耗时4秒钟.但是NPOI中替换书签内容的功能不知道是不支持还是没找到. 辅助类 Excel表格数据与D ...

  4. C#读取Excel表格数据到DataGridView中和导出DataGridView中的数据到Excel

    其实想在datagridview中显示excel表格中的数据跟读取数据库中的数据没什么差别,只不过是创建数据库连接的时候连接字段稍有差别. private void btnShow_Click(obj ...

  5. jxl读取excel浮点数据时,小数点后三位截取问题

    今天导入Excel数据时,发现很多浮点数据被自动四舍五入只保留了三位,原来是jxl里对getContents()进行了封装,对数值型数据作了该处理.一般我们会对读取excel的一整套流程作为工具类,那 ...

  6. [转] Windows下使用Python读取Excel表格数据

    http://www.python-excel.org/这个网站罗列了很多关于在Python下操作Excel文件的信息,这里选择了其介绍的第一个模块xlrd . xlrd 0.9.2版本跨平台同时支持 ...

  7. python自动化,使用unittest对界面操作,读取excel表格数据输入到页面查询结果,在把结果保存到另外一张excel中

    # -*- coding: utf-8 -*-from selenium import webdriverfrom selenium.webdriver.common.by import Byfrom ...

  8. Java操作Jxl实现导出数据生成Excel表格数据文件

    实现:前台用的框架是Easyui+Bootstrap结合使用,需要引入相应的Js.Css文件.页面:Jsp.拦截请求:Servlet.逻辑处理:ClassBean.数据库:SQLserver. 注意: ...

  9. java中使用jxl读取excel中的数据

    package bboss; import java.io.File; import java.io.FileInputStream; import java.io.IOException; impo ...

随机推荐

  1. CentOS防火墙iptables的配置方法详解

    CentOS系统也是基于linux中的它的防火墙其实就是iptables了,下面我来介绍在CentOS防火墙iptables的配置教程,希望此教程对各位朋友会有所帮助. iptables是与Linux ...

  2. java基本算法之快速排序

    快速排序:是找出一个元素(理论上可以随便找一个)作为基准(pivot),然后对数组进行分区操作,使基准左边元素的值都不大于基准值,基准右边的元素值 都不小于基准值,如此作为基准的元素调整到排序后的正确 ...

  3. ubuntu下安装chrome

    首先.题主在试过直接ubuntu终端命令安装chrome失败. 把经历过的错误稍微提一下: 在终端输入 下载安装包 sudo wget https://dl.google.com/linux/dire ...

  4. 【python】isinstance可以接收多个类型,hasattr,getattr,setattr

    来源:廖雪峰 可以判断一个变量是否是某些类型中的一种,比如下面的代码就可以判断是否是str或者unicode: >>> isinstance('a', (str, unicode)) ...

  5. Linux常用命令组合

    1.删除除某个文件或文件夹外的所有的内容 ll |grep -v test |xargs rm -rf find . -maxdepth 1 -type d|grep -v test|xargs rm ...

  6. request getParameter getAttribute

    在浏览器地址输入,表示传入一个参数test,值为123 http://localhost:8888/Test/index.jsp?test=123 在index.jsp中尝试使用EL表达式取出,代码如 ...

  7. Blender使用基础

    快捷键 -- 鼠标放在“大纲视图” -- .(小键盘) 定位到所选对象(该键的英文为period) -- 鼠标放在“3D视图” -- n  打开3D视图右面属性面板(把鼠标放在面板上,按F5可以左右切 ...

  8. [LeetCode] Reverse Linked List

    Reverse a singly linked list. 这题因为palindrome linked list 的时候需要就顺便做了一下.利用三个指针:prev, now, next 相互倒腾就行. ...

  9. [MongoDB]Mongo基本使用:

    汇总: 1. [MongoDB]安装MongoDB2. [MongoDB]Mongo基本使用:3. [MongoDB]MongoDB的优缺点及与关系型数据库的比较4. [MongoDB]MongoDB ...

  10. shell example01

    条件判断 if [[ -e ${1} ]]; then echo "$(tput setaf 2) found ${1} $(tput sgr0)" cat ${1} else e ...