基于Java實(shí)現(xiàn)Json文件轉(zhuǎn)換為Excel文件
一. 思路
今天接到個(gè)小任務(wù),讓把json文件轉(zhuǎn)換成excel文件,按照列展開.
思路:既然json已經(jīng)都已經(jīng)是現(xiàn)成的,那直接將json文件做讀操作,在通過(guò)不同的key,找到對(duì)應(yīng)的信息,在存到單元格中,在寫操作,生成excel文檔
二.jar包
涉及到的jar包,阿里的fastjson和poi的jar包
三.代碼
我的json文檔里數(shù)據(jù)的格式是這樣的
[
{
"total": 1,
"name": "規(guī)則限制:XXXX",
"timeStr": 1619242800000,
"message": "XXX",
"hehe": ""
},
{
"total": 2,
"name": "服務(wù)異常:XXXX",
"timeStr": 1619240400000,
"message": "XXX!",
"hehe": ""
}
]1.先對(duì)json文件進(jìn)行讀操作,提取String對(duì)象,在將String對(duì)象轉(zhuǎn)換為JsonArray
public static String readJsonFile(String path) {
String jsonString = "";
try {
File file = new File(path);
FileReader fileReader = new FileReader(file);
Reader reader = new InputStreamReader(new FileInputStream(file),"utf-8");
int ch = 0;
StringBuffer sb = new StringBuffer();
while ((ch = reader.read()) != -1) {
sb.append((char) ch);
}
fileReader.close();
reader.close();
jsonString = sb.toString();
return jsonString;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}我試過(guò)直接讀文件,出現(xiàn)中文亂碼,所以記得用UTF-8編碼,否則會(huì)是亂碼
2.文件內(nèi)容以String的形式獲取到,這時(shí)創(chuàng)建excel文件,在將String轉(zhuǎn)換為jsonArray形式遍歷,分別插入到excel文件的單元格cell中,在做寫操作
public static void main(String[] args) {
String json = ToJson.readJsonFile("C:\\Users\\yu\\Desktop\\new.json");
//System.out.println(json);
//JSONObject object = JSON.parseObject(json);
try {
//生成excel文件存放的地址
String uploadFile = "D:/test.xlsx";
OutputStream excel = new FileOutputStream(uploadFile);
XSSFWorkbook workBook = new XSSFWorkbook();
XSSFSheet sheet = workBook.createSheet();
XSSFRow row = null;//行
XSSFCell cell = null;//單元格
row = sheet.createRow(0);
//這是創(chuàng)建excel上邊的標(biāo)題頭
String[] names = { "total", "異常", "頁(yè)面名稱", "信息","時(shí)間","工號(hào)"};
for (int index = 0; index < 5; index++) {
cell = row.createCell(index);
cell.setCellValue(names[index]);
}
int count = 1;
JSONArray dataArray = JSONArray.parseArray(json);
for(int i = 0; i < dataArray.size();i++){
JSONObject dataObj = dataArray.getJSONObject(i);
//獲取不同key中的值
String total = dataObj.getString("total");
String name = dataObj.getString("name");
String[] nameArray = name.split(":");//這個(gè)是通過(guò)分號(hào)獲得兩個(gè)值,分別寫在excel中
String name1 = nameArray[0];
String name2 = nameArray[1];
String timeStr = dataObj.getString("timeStr");
String time = ToJson.stampToTime(timeStr);//這個(gè)根據(jù)時(shí)間戳轉(zhuǎn)換為正常年月日,時(shí)分秒
String message = dataObj.getString("message");
String staffId = dataObj.getString("hehe");
row = sheet.createRow(count);
cell = row.createCell(0);
cell.setCellValue(total);
cell = row.createCell(1);
cell.setCellValue(name1);
cell = row.createCell(2);
cell.setCellValue(name2);
cell = row.createCell(3);
cell.setCellValue(message);
cell = row.createCell(4);
cell.setCellValue(time);
cell = row.createCell(5);
cell.setCellValue(staffId);
count++;
}
workBook.write(excel);
} catch (Exception e) {
e.printStackTrace();
}
}時(shí)間戳的轉(zhuǎn)換方法:
public static String stampToTime(String stamp) {
String sd = "";
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sd = sdf.format(new Date(Long.parseLong(stamp))); // 時(shí)間戳轉(zhuǎn)換日期
return sd;
}運(yùn)行即可獲得excel文件
全部代碼:
package com.china.excelToJson;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ToJson {
public static void main(String[] args) {
String json = ToJson.readJsonFile("C:\\Users\\yu\\Desktop\\new.json");
//System.out.println(json);
//JSONObject object = JSON.parseObject(json);
try {
//生成excel文件存放的地址
String uploadFile = "D:/test.xlsx";
OutputStream excel = new FileOutputStream(uploadFile);
XSSFWorkbook workBook = new XSSFWorkbook();
XSSFSheet sheet = workBook.createSheet();
XSSFRow row = null;//行
XSSFCell cell = null;//單元格
row = sheet.createRow(0);
//這是創(chuàng)建excel上邊的標(biāo)題頭
String[] names = { "total", "異常", "頁(yè)面名稱", "信息","時(shí)間","工號(hào)"};
for (int index = 0; index < 5; index++) {
cell = row.createCell(index);
cell.setCellValue(names[index]);
}
int count = 1;
JSONArray dataArray = JSONArray.parseArray(json);
for(int i = 0; i < dataArray.size();i++){
JSONObject dataObj = dataArray.getJSONObject(i);
//獲取不同key中的值
String total = dataObj.getString("total");
String name = dataObj.getString("name");
String[] nameArray = name.split(":");//這個(gè)是通過(guò)分號(hào)獲得兩個(gè)值,分別寫在excel中
String name1 = nameArray[0];
String name2 = nameArray[1];
String timeStr = dataObj.getString("timeStr");
String time = ToJson.stampToTime(timeStr);//這個(gè)根據(jù)時(shí)間戳轉(zhuǎn)換為正常年月日,時(shí)分秒
String message = dataObj.getString("message");
String staffId = dataObj.getString("hehe");
row = sheet.createRow(count);
cell = row.createCell(0);
cell.setCellValue(total);
cell = row.createCell(1);
cell.setCellValue(name1);
cell = row.createCell(2);
cell.setCellValue(name2);
cell = row.createCell(3);
cell.setCellValue(message);
cell = row.createCell(4);
cell.setCellValue(time);
cell = row.createCell(5);
cell.setCellValue(staffId);
count++;
}
workBook.write(excel);
} catch (Exception e) {
e.printStackTrace();
}
}
public static String stampToTime(String stamp) {
String sd = "";
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sd = sdf.format(new Date(Long.parseLong(stamp))); // 時(shí)間戳轉(zhuǎn)換日期
return sd;
}
public static String readJsonFile(String fileName) {
String jsonStr = "";
try {
File jsonFile = new File(fileName);
FileReader fileReader = new FileReader(jsonFile);
Reader reader = new InputStreamReader(new FileInputStream(jsonFile),"utf-8");
int ch = 0;
StringBuffer sb = new StringBuffer();
while ((ch = reader.read()) != -1) {
sb.append((char) ch);
}
fileReader.close();
reader.close();
jsonStr = sb.toString();
return jsonStr;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}到此這篇關(guān)于基于Java實(shí)現(xiàn)Json文件轉(zhuǎn)換為Excel文件的文章就介紹到這了,更多相關(guān)Java Json轉(zhuǎn)Excel內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
socket編程時(shí)的發(fā)送與接收數(shù)據(jù)時(shí)的問(wèn)題解析
這篇文章主要為大家介紹了socket編程時(shí)的發(fā)送與接收數(shù)據(jù)時(shí)的問(wèn)題解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
spring學(xué)習(xí)之@SessionAttributes實(shí)例解析
這篇文章主要介紹了spring學(xué)習(xí)之@SessionAttributes實(shí)例解析,分享了相關(guān)代碼示例,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-02-02

