数据驱动: 数据写入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;
}
}