java 读取excel文件转换成json格式的实例代码
发布于 2020-7-1|
摘记: 需要读取excel数据转换成json数据,写了个测试功能,转换正常:JSON转换:org.json.jar 测试类: importFile.java:
```java
package com.siemens.util;
import java.util.ArrayList;
import java.util ..
需要读取excel数据转换成json数据,写了个测试功能,转换正常:JSON转换:org.json.jar 测试类: importFile.java:
```java
package com.siemens.util;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONException;
import org.json.JSONObject;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
//import com.siemens.entity.master;
//import com.siemens.service.masterService;
//import com.siemens.serviceImpl.masterServiceImpl;
//import com.siemens.serviceImpl.webServiceImpl;
public class importFile {
public static void main(String[] args) throws JSONException{
// master masters = new master();
// ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
// masterService ms = (masterService)ac.getBean("masterservice");
Workbook wb =null;
Sheet sheet = null;
Row row = null;
String cellData = null;
//文件路径,
String filePath = "f:/haoxy/Worktable222.xls";
wb =EXCELBean.readExcel(filePath);
if(wb != null){
//用来存放表中数据
List listMap = new ArrayList();
//获取第一个sheet
sheet = wb.getSheetAt(0);
//获取最大行数
int rownum = sheet.getPhysicalNumberOfRows();
//获取第一行
row = sheet.getRow(0);
//获取最大列数
int colnum = row.getPhysicalNumberOfCells();
//这里创建json对象,实测用map的话,json数据会有问题
JSONObject jsonMap = new JSONObject();
//循环行
for (int i = 1; i list = new ArrayList();
//循环列
for (int j=0;j list3 = new ArrayList();
list3.add("non-empty-placeholder");
jsonObject2.put("children",list3);
}
listMap.add(jsonObject2);
}else{
break;
}
}// end for row
//最外层加个key-gridData
jsonMap.put("gridData", listMap);
System.out.println(jsonMap);
}
}
}
```
读取excel 工具类,看到网友的读取方法引用一下:EXCELBean.java:
```java
package com.siemens.util;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class EXCELBean {
//读取excel
public static Workbook readExcel(String filePath){
Workbook wb = null;
if(filePath==null){
return null;
}
String extString = filePath.substring(filePath.lastIndexOf("."));
InputStream is = null;
try {
is = new FileInputStream(filePath);
if(".xls".equals(extString)){
return wb = new HSSFWorkbook(is);
}else if(".xlsx".equals(extString)){
return wb = new XSSFWorkbook(is);
}else{
return wb = null;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return wb;
}
public static Object getCellFormatValue(Cell cell){
Object cellValue = null;
if(cell!=null){
//判断cell类型
switch(cell.getCellType()){
case Cell.CELL_TYPE_NUMERIC:{
cellValue = String.valueOf(cell.getNumericCellValue());
break;
}
case Cell.CELL_TYPE_FORMULA:{
//判断cell是否为日期格式
if(DateUtil.isCellDateFormatted(cell)){
//转换为日期格式YYYY-mm-dd
cellValue = cell.getDateCellValue();
}else{
//数字
cellValue = String.valueOf(cell.getNumericCellValue());
}
break;
}
case Cell.CELL_TYPE_STRING:{
cellValue = cell.getRichStringCellValue().getString();
break;
}
default:
cellValue = "";
}
}else{
cellValue = "";
}
return cellValue;
}
}
```
原文地址: https://blog.csdn.net/qq_27985863/article/details/79917636