java使用jxl解析Excel

废话不多说,直接扔代码。

//使用jxl需要导入的类
import jxl.Cell;
import jxl.CellType;
import jxl.Sheet;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.write.Label;
import jxl.write.NumberFormats;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableWorkbook;

public List<SLSpeed> getImportInfoFromExcel(String filePath) throws Exception {
	result = new ArrayList<SLSpeed>();//记录集合
	SLSpeed slSpeed = null;//一行记录
	String errInfo = "";
	// 读取数据流并取得相应的Sheet
	/*InputStream is = new FileInputStream(filePath);*/
	File file = new File(filePath);
//		Workbook rwb = Workbook.getWorkbook(is);
	Workbook rwb = Workbook.getWorkbook(file);
	Sheet sheet = rwb.getSheet(0);

	try {
		int rows = sheet.getRows();// 获取当前sheet页中的总行数
		if (rows == 0) {
			throw new CTSIException("此sheet页无数据.");
		} else {
			for (int i = 1; i < rows; i++) {
				Cell[] row = sheet.getRow(i);//获得行
				errInfo = "";
				if(row == null){
					continue;
				}
				slSpeed = new SLSpeed();
				String temp = "";
				if(row.length<1 || row[0].getType() == CellType.EMPTY){
					temp = "";
				}else{
					temp = row[0].getContents()==null?"":row[0].getContents();// 获取每个单元格的值
				}
				slSpeed.setImCustAccountNum(temp);
				slSpeed.setCustAccountNum(getImCustAccountNum(temp));
				
				if(row.length < 2 || row[1].getType() == CellType.EMPTY){
					temp = "";
				}else{
					temp = row[1].getContents()==null?"":row[1].getContents();// 获取每个单元格的值
				}
				slSpeed.setImpSpeed(temp.trim());
				if(!slSpeed.getImCustAccountNum().equals("") || !slSpeed.getImpSpeed().equals("")){
					if(!result.contains(slSpeed)){
						result.add(slSpeed);
					}
				}
			}
		}
	} catch (Exception e) {
		e.printStackTrace();
	}finally{
		rwb.close();
//			is.close();
	}
	return result;
}

这个的Excel模板和上一篇文章 http://blog.csdn.net/xiakepan/article/details/39159375 的Excel模板为同一个,这里我闲着没事分别用了poi和jxl实现的解析。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值