木头人
2016-11-11 17:24:39
apache poi框架jxl对excel进行数据读取
只知道现在流行的Apache的poi和jxl对excel的读写操作,今天顺手写了一下代码,操作了一番。jxl具体的步骤:1.创建/获取工作簿;2.创建/获取表;3.读取/添加数据。
public static List<User> getDataFromFile(File file){
List<User> users=new ArrayList<User>();
Workbook workBook=null;
WorkbookSettings workbookSettings=new WorkbookSettings();
workbookSettings.setEncoding("utf-8");
//创建工作簿
try {
workBook=Workbook.getWorkbook(new FileInputStream(file), workbookSettings);
//获取第一张表
Sheet sheet=workBook.getSheet(0);
User user=null;
int count=sheet.getRows();//获取总的记录数
for(int row=1;row<count;row++)
{
String name=sheet.getCell(0,row).getContents().trim();
String sex=sheet.getCell(1,row).getContents().trim();
int age=Integer.parseInt(sheet.getCell(2,row).getContents().trim());
String email=sheet.getCell(3,row).getContents().trim();
user=new User(name, sex, age, email);
users.add(user);
}
} catch (BiffException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return users;
}
public static void main(String[] args) {
File file=new File("D:\\template\\用户.xls");
List<User> users=TestJxl.getDataFromFile(file);
System.out.println(users);
}
excel中的数据:
控制台输出的信息:
[User [name=jack, sex=male, age=18, email=xy123@qq.com], User [name=mary, sex=female, age=20, email=dshdsjh@163.com], User [name=Peter, sex=male, age=19, email=hsdjhhcvh@sian.com]]
评论




最近浏览
