1、话不多说,直接上代码,可以看注释
public static void buildEXCELDATA(OutputStream output, String json, String[] properties) throws JSONException {
buildEXCELDATA(output, json, properties, properties);
}
public static void buildEXCELDATA(OutputStream output, String json, String[] properties, String[] columnsNames) throws JSONException {
HSSFWorkbook libro = new HSSFWorkbook();
HSSFSheet ssf= libro.createSheet();
HSSFRow header = ssf.createRow(0);
JSONArray array = new JSONArray(json);
int e;
for (e = 0; e < columnsNames.length; ++e) {
String object = columnsNames[e];
HSSFCell row = header.createCell(e);
HSSFRichTextString j = new HSSFRichTextString(object);
row.setCellValue(j);
}
for (e = 0; e < array.length(); ++e) {
JSONObject var16 = array.getJSONObject(e);
HSSFRow var17 = ssf.createRow(e + 1);
for (int var18 = 0; var18 < properties.length; ++var18) {
String string = properties[var18];
HSSFCell cell = var17.createCell(var18);
try {
HSSFRichTextString text = new HSSFRichTextString(object .get(string).toString());
System.out.println("===="+text);
cell.setCellValue(text);
} catch (Exception var15) {
var15.printStackTrace();
}
}
}
try {
libro.write(output);
} catch (Exception var15) {
var15.printStackTrace();
}
}
public static void main(String[] args) {
try {
//文件位置
FileInputStream fis = new FileInputStream("xx\\file-utf8.json");
InputStreamReader isr = new InputStreamReader(fis,"UTF-8");
BufferedReader bufferedReader = new BufferedReader(isr);
String line = null;
StringBuffer stringBuffer = new StringBuffer();
while ((line = bufferedReader.readLine()) != null) {
stringBuffer.append(line);
}
//要存储的文件位置
FileOutputStream xlsStream = new FileOutputStream("xx\\7777.xls");
//需要存储的字段
FileJsonToExcel.buildEXCELDATA(xlsStream, stringBuffer.toString(), new String[]{"id", "createTime", "name"});
xlsStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
json文件格式
[
{
"id": "438",
"name": "小明",
"createTime": 1594112146000
}
]
遇到的问题:
- 导出的excel 如果有中文乱码,我尝试了使用 InputStreamReader isr = new InputStreamReader(fis,“UTF-8”)也没有用,建议更改一下文件格式,如图