java读取xls

利用jsl.jar这个包,可以很容易的读取xls文件,包在下面的附件中

Java代码 复制代码 收藏代码
  1. import java.io.File;
  2. import java.io.IOException;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import jxl.Sheet;
  6. import jxl.Workbook;
  7. import jxl.read.biff.BiffException;
  8. public class ParseExcel {
  9. static List<String[]> parse(File file) {
  10. List<String[]> excelValueList = new ArrayList<String[]>();
  11. if (file.exists() && file.canRead()
  12. && (file.getName().lastIndexOf(".xls") >= 1)) {
  13. Workbook workbook = null;
  14. try {
  15. workbook = Workbook.getWorkbook(file);
  16. Sheet sheet = workbook.getSheet(0);
  17. int row = sheet.getRows();
  18. int col = sheet.getColumns();
  19. for (int r = 0; r < row; r++) {
  20. String[] rowValue = new String[col];
  21. for (int c = 0; c < col; c++) {
  22. rowValue[c] = sheet.getCell(c, r).getContents() != null ? sheet
  23. .getCell(c, r).getContents()
  24. : "";
  25. }
  26. excelValueList.add(rowValue);
  27. }
  28. } catch (BiffException e) {
  29. // TODO Auto-generated catch block
  30. e.printStackTrace();
  31. } catch (IOException e) {
  32. // TODO Auto-generated catch block
  33. e.printStackTrace();
  34. } finally {
  35. if (workbook != null) {
  36. workbook.close();
  37. }
  38. }
  39. }
  40. return excelValueList;
  41. }
  42. public static void main(String[] args) {
  43. String fname = "E:\\1\\高新技术(处理后).xls";
  44. File file = new File(fname);
  45. List<String[]> excelValueList = new ArrayList<String[]>();
  46. excelValueList = parse(file);
  47. for(String[] sa:excelValueList){
  48. for(String s:sa){
  49. System.out.print(s+"----");
  50. }
  51. System.out.println();
  52. }
  53. }
  54. }
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

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

public class ParseExcel {

	static List<String[]> parse(File file) {
		List<String[]> excelValueList = new ArrayList<String[]>();
		if (file.exists() && file.canRead()
				&& (file.getName().lastIndexOf(".xls") >= 1)) {
			Workbook workbook = null;
			try {
				workbook = Workbook.getWorkbook(file);
				Sheet sheet = workbook.getSheet(0);
				int row = sheet.getRows();
				int col = sheet.getColumns();
				for (int r = 0; r < row; r++) {
					String[] rowValue = new String[col];
					for (int c = 0; c < col; c++) {
						rowValue[c] = sheet.getCell(c, r).getContents() != null ? sheet
								.getCell(c, r).getContents()
								: "";
					}
					excelValueList.add(rowValue);
				}
			} catch (BiffException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				if (workbook != null) {
					workbook.close();
				}
			}
		}
		return excelValueList;
	}
	public static void main(String[] args) {
		String fname = "E:\\1\\高新技术(处理后).xls";
		File file = new File(fname);
		List<String[]> excelValueList = new ArrayList<String[]>();
		excelValueList = parse(file);
		for(String[] sa:excelValueList){
			for(String s:sa){
				System.out.print(s+"----");
			}
			System.out.println();
		}
	}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值