一段小程序,用java读取Excel数据的。
- Java code
package com.inAndOut;import java.io.BufferedReader;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;import java.io.Serializable;import java.util.ArrayList;import java.util.List;class ReadIN { private List<Integer> list = new ArrayList<Integer>();// 存放整数 public List<Integer> input() throws IOException { BufferedReader in = null; String str = null; try { in = new BufferedReader(new FileReader("./你好.xls")); } catch (FileNotFoundException e) { e.printStackTrace(); System.out.println("这是打开文件错误!"); } str = in.readLine();// 抛弃第一行 while ((str = in.readLine()) != null) { String[] str2 = str.trim().split("\\s+");//通过空格切分 // 程序运行时出现错误 主要错误 for (int i = 1; i < str2.length; i++) {// 从第二个单元格开始 list.add(Integer.parseInt(str2[i])); } } in.close(); return list; }}public class Yunxing{ public static void main(String[] args){ ReadIN ri=new ReadIN(); List<Integer> list = new ArrayList<Integer>(); list=ri.input(); System.out.println(list);//出现错误}}
你好.xls文件如下(星号中的内容为你好.xls表格中的内容):
*****************
行和列 一列 二列 三列 四列
一行 123 456 789 12
二行 45 25 56 32
三行 121 26 56 89
***********
表中的数字都是以数值的形式(注意是数值)存在的。
输出结果错误
------解决方案--------------------
这倒少见,一个直接输入流操作。。
通常用POI和jxl,真的没自己写过。
------解决方案--------------------
Reader和BufferReade是用来读取文本文件的
而excel存储的是二进制文件
java解析excel还是用第三方的jar包吧
就像上面提到的JXL、POI、JACOB等
------解决方案--------------------