JSON数据导出Excel表

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();
        }
    }
  • 7
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

龚大龙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值