Android 读Excel 精简函数封装利用了jxl.jar

有段时间做项目的时候遇到了需要在Android读取Excel的需求,于是选择了轻量级库jxl.jar,这个库的缺点是文档不是那么全,而且不能读offic2007版本以上格式的xlsx,但是用来你简单操作.xls后缀的问题不大,但是要注意这个库从Excel读出来时间格式是GMT时间所以要根据需要转换成北京时间,你懂的时区转化必须要注意的一点。下面贴出为封装的把Excel文件流转换成Object[][]的源代码


package com.third.pblib;

import java.io.*;

import jxl.*;

public class UtilsJxlExcel {

public static Object[][] ReadToEnd(InputStream inputStream) throws Exception {
	Workbook book = Workbook.getWorkbook(inputStream);
	Sheet sheet = book.getSheet(0);
	int Rows = sheet.getRows();
	int Cols = sheet.getColumns();

	Object[][] arr = new Object[Rows][Cols];

	for (int row = 0; row < Rows; row++) {
		for (int col = 0; col < Cols; col++) {
			Cell cell = sheet.getCell(col, row);
			CellType type = cell.getType();
			if (type == CellType.DATE) {
				DateCell dateCell = (DateCell) cell;
				arr[row][col] = UtilsTime.GMTToLocal(dateCell.getDate());//时间必须转换一下
			} else if (type == CellType.NUMBER) {
				NumberCell numberCell = (NumberCell) cell;
				arr[row][col] = numberCell.getValue();
			} else {
				arr[row][col] = cell.getContents();
			}
		}
	}
	return arr;
}
}
其中UtilsTime.GMTToLocal是一个GMT时间转北京时间的转换函数

public static java.util.Date GMTToLocal(java.util.Date gmtDate) throws Exception {

	if (gmtDate == null)
		return null;
	DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
	dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
	String str = dateFormat.format(gmtDate);
	dateFormat.setTimeZone(TimeZone.getDefault());
	return dateFormat.parse(str);
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值