JSON数据导出Excel表
应老板要求写了一个实用的小功能,需求是将api接口返回的JSON数据导出Excel表,过程中改了两稿,一是前端js直接导出一个表,另一是分门别类在java中同时导出多个表(例:如输入某公司,将各部门各人员的具体信息导出,分多张表,不同部门存放不同表):
- JS写法
- JAVA写法
JS写法
话不多说,直接上代码!
代码块
json数据转excel函数:
//json数据转excel
function JSONToExcelConvertor(JSONData, FileName) {
//先转化json
var arrData = typeof JSONData != 'object' ? JSON.parse(JSONData) : JSONData;
var excel = '<table>';
var row = "<tr>";
//设置表头
var keys = Object.keys(JSONData[0]);
keys.forEach(function (item) {
row += "<td>" + item + '</td>';
});
//换行
excel += row + "</tr>";
//设置数据
for (var i = 0; i < arrData.length; i++) {
var row = "<tr>";
for (var index in arrData[i]) {
console.log(arrData[i][index]);
//var value = arrData[i][index] === "." ? "" : arrData[i][index];
row += '<td>' + arrData[i][index] + '</td>';
}
excel += row + "</tr>";
}
excel += "</table>";
var excelFile = "<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:x='urn:schemas-microsoft-com:office:excel' xmlns='http://www.w3.org/TR/REC-html40'>";
excelFile += '<meta http-equiv="content-type" content="application/vnd.ms-excel; charset=UTF-8">';
excelFile += '<meta http-equiv="content-type" content="application/vnd.ms-excel';
excelFile += '; charset=UTF-8">';
excelFile += "<head>";
excelFile += "<!--[if gte mso 9]>";
excelFile += "<xml>";
excelFile += "<x:ExcelWorkbook>";
excelFile += "<x:ExcelWorksheets>";
excelFile += "<x:ExcelWorksheet>";
excelFile += "<x:Name>";
excelFile += "{worksheet}";
excelFile += "</x:Name>";
excelFile += "<x:WorksheetOptions>";
excelFile += "<x:DisplayGridlines/>";
excelFile += "</x:WorksheetOptions>";
excelFile += "</x:ExcelWorksheet>";
excelFile += "</x:ExcelWorksheets>";
excelFile += "</x:ExcelWorkbook>";
excelFile += "</xml>";
excelFile += "<![endif]-->";
excelFile += "</head>";
excelFile += "<body>";
excelFile += excel;
excelFile += "</body>";
excelFile += "</html>";
var uri = 'data:application/vnd.ms-excel;charset=utf-8,' + encodeURIComponent(excelFile);
var link = document.createElement("a");
link.href = uri;
link.style = "visibility:hidden";
link.download = FileName + ".xls";
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
判断json格式函数:
//判断是否为JSON格式数据
function isJSON(str) {
try {
if (typeof JSON.parse(str) == "object") {
return true;
}
} catch(e) {
}
return false;
}
Java写法
代码块
json数据转excel函数:
public static void exportExcel(String name,JSONArray ja,String tbName){
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet(name);
XSSFRow row = sheet.createRow(0);
int index = 0;
if(tbName.equals("")){
tbName = "excel";
}
File file = new File("C:\\"+tbName);
if(!file.exists()){
file.mkdir();
}
JSONObject first = ja.getJSONObject(0);
Iterator<String> iterator = first.keys(); // 得到第一项的key集合
while (iterator.hasNext()) { // 遍历key集合
String key = (String) iterator.next(); // 得到key
String value = first.getString(key);
XSSFCell cell = row.createCell(index);
cell.setCellValue(key);
index++;
}
for (int i = 0; i < ja.size(); i++) {
row = sheet.createRow(i+1);
JSONObject jaa = ja.getJSONObject(i);
Iterator<String> iterator1 = jaa.keys();
int index1 = 0;
while (iterator1.hasNext()) { // 遍历key集合
String key1 = (String) iterator1.next(); // 得到key
String value = jaa.getString(key1);
System.out.println(value);
XSSFCell cell = row.createCell(index1);
cell.setCellValue(value);
index1++;
}
}
try {
String tmpPath = "C:\\"+tbName+"\\" + name + ".xlsx";
OutputStream outputStream = new FileOutputStream(tmpPath);
workbook.write(outputStream);
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}