做junit时,直接读excel中数据

数据驱动: 数据写入excel中,支持2003之前的excel。调用jar包jxl。

以下是代码

package com.pax.tms.demo;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import org.apache.poi.ss.formula.SheetNameFormatter;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

public class xls2String {

	
	/**
	 * 
	 * @param file 想要读取的文件对象
	 * @param i 想要读取的sheet
	 * @return 返回sheet的总行数
	 * @throws BiffException
	 * @throws IOException
	 */
	public int getRows(File file,int i) throws BiffException, IOException {
		int rows = 0;
		FileInputStream fis = new FileInputStream(file);   
        jxl.Workbook rwb = Workbook.getWorkbook(fis);   
            Sheet rs = rwb.getSheet(i);   
            rows = rs.getRows();
            String SheetName = rs.getName();
            System.out.println("--------工作表的名称是:" + SheetName + "-----------");
            return rows;
	}

	  /**
     * 读取xls文件内容
     * @param file 想要读取的文件对象
     * @param i 想要读取的sheet
     * @param j 想要读取的行
     * @param k 想要读取的列
     * @return result 返回对应行列的数据
     * @throws IOException 
     * @throws BiffException 
     */
	public String getData (File file,int i, int j,int k){
    	 String result = "";

    	try{
            FileInputStream fis = new FileInputStream(file);   
            jxl.Workbook rwb = Workbook.getWorkbook(fis);   
                Sheet rs = rwb.getSheet(i);   
                Cell[] cells = rs.getRow(j);   
                result = cells[k].getContents().toString();
 
            fis.close();   
            
        }catch(Exception e){
            e.printStackTrace();
        }
    	
		return result;
    }
    
	
	/**
	 * 
	 * @param a  Long 类型的数组
	 * @param b  String类型的数组
	 * @param c  String类型的字符串
	 * @return   返回Long类型的数组
	 * @category 功能介绍:c变量中的字符串以逗号分隔开,然后分别存入b数组中,再转回成Long类型的数组。
	 */
	
	public Long[] String2Long(String c) {
		 String[] b = c.split(","); 
		 Long[] a = new Long[b.length];
	     for (int idx = 0; idx < b.length; idx++) {
	        a[idx] = Long.parseLong(b[idx]);
//	        	System.out.println(a[idx].toString());
	        }
	
		return a;
	}
    
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值