//读取表格数据
XSSFWorkbook xssfWorkbook = new XSSFWorkbook(new FileInputStream("D:/数据收集规范表格.xlsx"));
XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(0);
for (int i = 1; i <= xssfSheet.getLastRowNum(); i++) {
XSSFRow xssfRow = xssfSheet.getRow(i);//获取每一行的数据
int minCell = xssfRow.getFirstCellNum();
int maxCell = xssfRow.getLastCellNum();
String parent = String.valueOf((xssfRow.getCell(0)).getStringCellValue());//String类型获取数据
xssfRow.getCell(5).setCellType(CellType.STRING);//先转换类型,否则报错
Double.parseDouble(xssfRow.getCell(5).getStringCellValue())//double 类型获取数据
Integer.valueOf((xssfRow.getCell(9)).getStringCellValue());int类型获取数据
// 对于不好处理的数据,比如:
// [
// {url: 'nanhai/a.geojson'},
// {"type":"geojson","url":"/GeoVis/data/Competition/product07/01-金寨抽水蓄能联动区周边景点.geojson"},
// {"type":"geojson","layers":"icenter:颖上水域专题图-PNG-4326"}
// ]
// 我的处理方法:
String data = (xssfRow.getCell(12)).getStringCellValue();//获取数据
data = "{data:"+data+"}";//拼接数据为json类型
JSONObject panelJsonObject = new JSONObject(data);//解析
JSONArray contents = panelJsonObject.getJSONArray("data");
for(int k = 0;k<contents.length();k++){
JSONObject datas = contents.getJSONObject(k);
AssociatedGeojson geojson = new AssociatedGeojson();
//datas.has("url")?(String)datas.get("url"):""
//判断是否存在数据url,如果存在获取数据,不存在显示空
geojson.setUrl(datas.has("url")?(String)datas.get("url"):"");
geojson.setType(datas.has("type")?(String)datas.get("type"):"");
geojson.setTypeLabel(datas.has("layers")?(String)datas.get("layers"):"");
this.insertAssiciatedDao.save(geojson);
}
}
pom.xml文件可能使用的包:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>RELEASE</version>
</dependency>
springboot处理xlsx或xls表格文件
最新推荐文章于 2024-01-08 17:10:13 发布