用poi技术把EXCEL(.xls)文件读取到java对象中

本文提供了一个使用Apache POI库将Excel(.xls)文件内容读取到Java对象的工具类示例。通过遍历Excel工作表的行和列,根据单元格类型提取数据,并根据列索引将数据分配到对应的对象属性中。测试代码展示了如何调用工具类并获取对象集合,便于进一步处理或存储到数据库。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.直接贴代码,写一个工具类
public class PoiUtils {




public static List<Automat> getAutomatsByXls(String fileName) {
List<Automat> autoList = new ArrayList<Automat>();//那些一个集合存入对象
Automat automat = new Automat();//初始化一个对象。好放对应xls的值。
try {
InputStream input = new FileInputStream(fileName); // 建立输入流


HSSFWorkbook wb = new HSSFWorkbook(input);


Sheet sheet = wb.getSheetAt(0); // 获得第一个表单
Iterator<Row> rows = sheet.rowIterator(); // 获得第一个表单的迭代器
while (rows.hasNext()) {//


Row row = rows.next(); // 获得行数据
// System.out.println("Row #" + row.getRowNum()); // 获得行号从0开始
Iterator<Cell> cells = row.cellIterator(); // 获得第一行的迭代器
Automat automat1 = new Automat();
while (cells.hasNext()) {


Cell cell = cells.next();//


String temcell;// 临时


String cell1 = cell.getColumnIndex() + "";
String cell2 = cell1.substring(0, 1);


switch (cell.getCellType()) { // 根据cell中的类型来输出数据


case HSSFCell.CELL_TYPE_NUMERIC:
if (HSSFDateUtil.isCellDateFormatted(cell)) {
Date date1 = cell.getDateCellValue();


temcell = DateFormatUtils.format(date1,
"yyyy-MM-dd") + "";
break;
} else {


temcell = cell.getNumericCellValue() + "";
break;
}


case HSSFCell.CELL_TYPE_STRING:


temcell = cell.getStringCellValue() + "";
break;
case HSSFCell.CELL_TYPE_BOOLEAN:


temcell = cell.getBooleanCellValue() + "";
break;
case HSSFCell.CELL_TYPE_FORMULA:


temcell = cell.getCellFormula() + "";
break;
default:
temcell = null;
break;
}
//************************************给对象赋值
if (cell1.equals("0")) {
automat1.setMeansclass(temcell);
temcell = null;
} else {


}
if (cell1.equals("1")) {
automat1.setMeansname(temcell);
temcell = null;
} else {


}
if (cell1.equals("2")) {
automat1.setClassify(temcell);
temcell = null;
} else {


}
if (cell1.equals("3")) {
automat1.setModels(temcell);
temcell = null;
} else {


}
if (cell1.equals("4")) {
automat1.setMeansNo(temcell);
temcell = null;


} else {


}
if (cell1.equals("5")) {
automat1.setPropertyNo(temcell);


temcell = null;
} else {
}
if (cell1.equals("6")) {
automat1.setModelNo(temcell);
temcell = null;


} else {


}
if (cell1.equals("7")) {
automat1.setKeeping(temcell);
temcell = null;


} else {


}
if (cell1.equals("8")) {


automat1.setGaindate(temcell);
temcell = null;


} else {


}
if (cell1.equals("9")) {


automat1.setCost(temcell);
temcell = null;


} else {


}
if (cell1.equals("10")) {


if (cell1.length() < 1) {


} else {
automat1.setUse(temcell);
}


temcell = null;
} else {


}


}


autoList.add(automat1);// 增加到LIST


}
return autoList;


} catch (IOException ex) {
ex.printStackTrace();
return null;
}


}

2.进行测试,把要读取的XLS格式文件路径当参数传进去,就可以得到一个对象集合,有了集合,再想放到数据库存就简单了。
@Test
public void test() {
String fileName1 = "D:/poiTest/mlist1.xls";
List<Automat> sa = getAutomatsByXls(fileName1);


for (Automat a : sa) {
System.out.println(a);
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值