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

java学习(四) excel读取

2014-07-23 16:49 302 查看
private static void readExcel() {
String filePath = "C:/Standardzid.xls";
File file = new File(filePath);
List<String> citys = new ArrayList<String>();
List<LandPropertyType> cityList = null;
List<List<LandPropertyType>> propertys = null;
try {
POIFSFileSystem readPoiFileSystem = new POIFSFileSystem(new FileInputStream(file));
HSSFWorkbook workbook = new HSSFWorkbook(readPoiFileSystem);
HSSFSheet sheet = workbook.getSheetAt(0);
boolean flag = true;
for (Row row : sheet) {
if (flag) {
for (Cell cell : row) {
String cityName = cell.getStringCellValue();
if (cityName != null && cityName.length() > 0) {
citys.add(cityName);
}
propertys = new ArrayList<List<LandPropertyType>>();
for (int i = 0; i < citys.size(); i++) {
cityList = new ArrayList<LandPropertyType>();
propertys.add(cityList);
}
}
flag = false;
} else {
String propertyType = row.getCell(0).getStringCellValue();
int lastCell = row.getLastCellNum();
for (int i = 2; i < lastCell; i++) {
Cell cell=row.getCell(i);
if(cell!=null){
String propertyName = row.getCell(i).getStringCellValue();
if (propertyName != null && propertyName.length() > 0) {
String[] propertyNames = propertyName.split(",");
for (String name : propertyNames) {
LandPropertyType landPropertyType = new LandPropertyType(name,LandProperty.valueOf(propertyType));
propertys.get(i - 2).add(landPropertyType);
}
}
}
}
}
}
for (int i = 0; i < citys.size(); i++) {
String city = citys.get(i);
LandPropertyType[] newLandPropertys = new LandPropertyType[propertys.size()];
propertyMap.put(city, propertys.get(i).toArray(newLandPropertys));
}

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