解析excel文档LZ用的是poi-3.10-FINAL.jar这个工具包
以上是LZ用的excel表的格式
public class Student {
private String name;
private String address;
private Date birthday;
private String classes;
private int gender;
private String mobileNumber;
private String nationId;
private String schools;
@Override
public String toString() {
return "Student [name=" + name + ", address=" + address
+ ", birthday=" + birthday + ", classes=" + classes
+ ", gender=" + gender + ", mobileNumber=" + mobileNumber
+ ", nationId=" + nationId + ", schools=" + schools + "]";
}
//get set 省略....
以上是用户类,这里用的是一个student类
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
public class ExcelClass{
public static void main(String[] args) throws Exception {
List list = new ArrayList();
File file1 = new File("E:"+File.separator+"1.xls");
FileInputStream fis = new FileInputStream(file1);
POIFSFileSystem fs = new POIFSFileSystem(fis);
HSSFWorkbook hwb = new HSSFWorkbook(fs);
HSSFSheet sheet = hwb.getSheet("学生");
if(sheet==null){
System.out.println("没有学生表");
return;
}
System.out.println(sheet.getFirstRowNum());//行和列都是从0开始计数 此处应该为1
HSSFRow titleRow = sheet.getRow(sheet.getFirstRowNum());//获取标题行
Map map = new HashMap();//此处将标题与列数放到map集合中,保证以后excel列位置即使改变也能同样解析
System.out.println(titleRow.getLastCellNum());//9 (列数的最后一列+1)
for(int i=titleRow.getFirstCellNum();i
以上是解析excel的解析类
结果:
[Student [name=abcde, address=abcvfg, birthday=Wed Oct 02 00:00:00 CST 1991, classes=5, gender=1, mobileNumber=13145678911, nationId=12345678912345301, schools=zxwe111],
Student [name=abcde1, address=abcvfg1, birthday=Sat Oct 03 00:00:00 CST 1992, classes=6, gender=2, mobileNumber=13145678912, nationId=12345678912345302, schools=zxwe112],
Student [name=abcde2, address=abcvfg2, birthday=Mon Oct 04 00:00:00 CST 1993, classes=7, gender=1, mobileNumber=13145678913, nationId=12345678912345303, schools=zxwe113],
Student [name=abcde3, address=abcvfg3, birthday=Wed Oct 05 00:00:00 CST 1994, classes=8, gender=2, mobileNumber=13145678914, nationId=12345678912345304, schools=zxwe114],
Student [name=abcde4, address=abcvfg4, birthday=Fri Oct 06 00:00:00 CST 1995, classes=9, gender=1, mobileNumber=13145678915, nationId=12345678912345305, schools=zxwe115]]
注意:1,使用getLastCellNum方法获得的值是最后一列的索引值(这里excel表最后一列的索引值是从左到右从0开始数一直到8)
加1
2,请看下用户班级和手机号码,这里的用户班级在poi解析时是当数字解析的,而手机号码是当字符串解析的,如果你想让它
当字符串解析,把excel格子中的数字开头加’ 即可!