首页 > 解决方案 > How to Convert nested(Complex) JSON to excel IN Jquery?

问题描述

Is there any way to convert complex json to Excel format? For Example:

{ 
  "accounting" : [   
                     { "firstName" : "John",  
                       "lastName"  : "Doe",
                       "age"       : 23 }
                 ],                            
  "sales"      : [ 
                     { "firstName" : "Sally", 
                       "lastName"  : "Green",
                        "age"      : 27 }
                 ] 
} 

标签: javascriptjqueryjsonexcel

解决方案


我们公司正在使用 Kendo UI 在 JavaScript 中创建 Excel 文档。但这不是免费产品。https://docs.telerik.com/kendo-ui/framework/excel/introduction

如果链接断开。

<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jszip/2.4.0/jszip.js"></script>
<script src="http://kendo.cdn.telerik.com/2018.3.1017/js/kendo.all.min.js"></script>

<script>
var workbook = new kendo.ooxml.Workbook({
  sheets: [
    {
      // Column settings (width)
      columns: [
        { autoWidth: true },
        { autoWidth: true }
      ],
      // Title of the sheet
      title: "Customers",
      // Rows of the sheet
      rows: [
        // First row (header)
        {
          cells: [
            // First cell
            { value: "Company Name" },
            // Second cell
            { value: "Contact" }
          ]
        },
        // Second row (data)
        {
          cells: [
            { value: "Around the Horn" },
            { value: "Thomas Hardy" }
          ]
        },
        // Third row (data)
        {
          cells: [
            { value: "B's Beverages" },
            { value: "Victoria Ashworth" }
          ]
        }
      ]
    }
  ]
});
kendo.saveAs({
    dataURI: workbook.toDataURL(),
    fileName: "Test.xlsx"
});
</script>

推荐阅读