您的位置:首页 > 编程语言 > Java开发

[Java]通过Poi包读取Excel表格

2017-05-18 14:38 411 查看
public List<EntBusinessCard> readCardExcel() throws IOException {
XSSFWorkbook hssfWorkbook = new XSSFWorkbook(new FileInputStream("D:\\buyal\\名片总和.xlsx"));

EntBusinessCard card = null;
List<EntBusinessCard> list = new ArrayList<EntBusinessCard>();

for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++){
XSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);

if(hssfSheet == null){
continue;
}

for(int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++){
XSSFRow hssfRow = hssfSheet.getRow(rowNum);

if(hssfRow != null){
card = new EntBusinessCard();
card.setProperty(hssfRow.getCell(0) == null ?"":getValue(hssfRow.getCell(0)));
card.setRole(hssfRow.getCell(1) == null ?"":getValue(hssfRow.getCell(1)));
card.setName(hssfRow.getCell(2) == null ?"":getValue(hssfRow.getCell(2)));
card.setDirect(hssfRow.getCell(3) == null ?"":getValue(hssfRow.getCell(3)));
card.setPostcode(hssfRow.getCell(4) == null ?"":getValue(hssfRow.getCell(4)));
card.setRealaddress(hssfRow.getCell(5) == null ?"":getValue(hssfRow.getCell(5)));
card.setEnttype(hssfRow.getCell(6) == null ?"":getValue(hssfRow.getCell(6)));
card.setLine(hssfRow.getCell(7) == null ?"":getValue(hssfRow.getCell(7)));
card.setMainproduct(hssfRow.getCell(8) == null ?"":getValue(hssfRow.getCell(8)));
card.setMainproductpic(hssfRow.getCell(9) == null ?"":getValue(hssfRow.getCell(9)));
card.setScale(hssfRow.getCell(10) == null ?"":getValue(hssfRow.getCell(10)));
card.setUrl(hssfRow.getCell(11) == null ?"":getValue(hssfRow.getCell(11)));
card.setFax(hssfRow.getCell(12) == null ?"":getValue(hssfRow.getCell(12)));
card.setWorkshopstatus(hssfRow.getCell(13) == null ?"":getValue(hssfRow.getCell(13)));
card.setCapacity(hssfRow.getCell(14) == null ?"":getValue(hssfRow.getCell(14)));
card.setContract(hssfRow.getCell(15) == null ?"":getValue(hssfRow.getCell(15)));
card.setTele(hssfRow.getCell(17) == null ?"":getValue(hssfRow.getCell(17)));
card.setEmail(hssfRow.getCell(18) == null ?"":getValue(hssfRow.getCell(18)));
card.setIntroduce(hssfRow.getCell(19) == null ?"":getValue(hssfRow.getCell(19)));

list.add(card);
}
}

}
return list;
}


getValue()

public String getValue(XSSFCell hssfCell){
if (hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN) {
// 返回布尔类型的值
return String.valueOf(hssfCell.getBooleanCellValue());
} else if (hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC) {
// 返回数值类型的值
return String.valueOf(hssfCell.getNumericCellValue());
} else {
// 返回字符串类型的值
return String.valueOf(hssfCell.getStringCellValue());
}
}


EntBusinessCard 类

public class EntBusinessCard {
private String property;
private String role;
private String name;
private String direct;
private String postcode;
private String realaddress;
private String enttype;
private String line;
private String mainproduct;
private String mainproductpic;
private String scale;
private String url;
private String fax;
private String workshopstatus;
private String capacity;
private String contract;
private String tele;
private String email;
private String introduce;

getter()。。。
setter()。。。
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: