excel⽂件中的数据转化为json格式并输出json⽂件1:需求:将excel中的数据获取出来,转化为json格式,之后输出到.json⽂件中。
2:步骤:(1):将excel中的数据获取出来,使⽤jsonObject转化为json格式字符串
    (2):使⽤输出流将json字符串输出到json⽂件中
3:需要的依赖:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.10-FINAL</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
并输出<artifactId>poi-ooxml</artifactId>
<version>3.10-FINAL</version>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<classifier>jdk15</classifier>
<version>2.2.3</version>
</dependency>
4:代码
public class ExcelTojson {
/
/json的key
private static String[] titleArray = { "userName", "phoneNum"};
public static void main(String[] args) {
InputStream is = null;
String excelToJson = excelToJson(is);
try {
createJsonFile(excelToJson);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static String excelToJson(InputStream is) {
JSONObject jsonRow = new JSONObject();
try {
// 获取⽂件
is = new FileInputStream("C:\\Users\\Administrator\\Desktop\\新建 XLSX ⼯作表.xlsx");
// 创建excel⼯作空间
Workbook workbook = ate(is);
// 获取sheet页的数量
int sheetCount = NumberOfSheets();
// 遍历每个sheet页
for (int i = 0; i < sheetCount; i++) {
// 获取每个sheet页
Sheet sheet = SheetAt(i);
// 获取sheet页的总⾏数
int rowCount = PhysicalNumberOfRows();
// 遍历每⼀⾏
for (int j = 0; j < rowCount; j++) {
//记录每⼀⾏的⼀个唯⼀标识作为json的key
String key="";
String key="";
// 获取列对象
Row row = Row(j);
// 获取总列数
int cellCount = PhysicalNumberOfCells();
Map<String, String> map = new HashMap<String, String>();
//除去第⼀⾏的标题
if (j > 0) {
// 遍历所有数据
for (int n = 0; n < cellCount; n++) {
// 获取每⼀个单元格
Cell cell = Cell(n);
// 在读取单元格内容前,设置所有单元格中内容都是字符串类型
cell.setCellType(Cell.CELL_TYPE_STRING);
// 按照字符串类型读取单元格内数据
String cellValue = StringCellValue();
if(n==1) {
key=cellValue;
}
map.put(titleArray[n], cellValue);
}
}
if (map.size() != 0) {
jsonRow.put(key,map);
}
}
}
System.out.String());
} catch (Exception e) {
}
String();
}
public static void createJsonFile(String jsonData) throws IOException { File file = new File("C:\\Users\\Administrator\\Desktop\\⽤户.json");
//连接流
FileOutputStream fos=null;
//链接流
BufferedOutputStream bos =null;
try {
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
bos.Bytes());;
bos.flush();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(null!=bos) {
bos.close();
}
if(null!=fos) {
fos.close();
}
}
}
}
4:效果
      excel⽂件数据:
      json⽂件
{"20161031993":{"phoneNum":"20161031993","userName":"⼩明"},"20161031974":{"phoneNum":"20161031974","userName":"⼩红"},"20161031847":{"phoneNum":"20161031847","userName":"⼩张"},"20161031893":{"phoneNum":"20161031893","userName":"⼩李"},"20161032008":{"phoneNum":"20161032008","user
Name":"⼩王"},"20161031811":{"phoneNum":"20161031811","userName":"⼩⽜"},"20161032052":{"phoneNum":"20161032052","userName":"⼩宋"},"20161032042":{"phoneNum":"20161032042","userName":"⼩⾦"}}
5:改进
    可以查java代码,将输出的json格式化,使其更加美观。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。