>  기사  >  웹 프론트엔드  >  json 형식을 Excel 테이블로 변환(자세한 튜토리얼)

json 형식을 Excel 테이블로 변환(자세한 튜토리얼)

亚连
亚连원래의
2018-06-19 10:22:148253검색

아래에서는 javascript를 사용하여 json 형식 배열을 Excel 테이블로 다운로드하는 방법을 공유하겠습니다. 이는 좋은 참조 값이 있으며 모든 사람에게 도움이 되기를 바랍니다.

예제는 다음과 같습니다.

<html> 
<head> 
  <meta http-equiv="content-type" content="text/html; charset=utf-8"> 
  <script type="text/javascript" src="jquery183.min.js"></script> 
  <script type="text/javascript"> 
    $(document).ready(function(){ 
      $(&#39;#wwo&#39;).click(function(){ 
        var data = {
"title":
[
{"value":"A1标题"}, 
{"value":"B1标题"}
],
"data":
[
[
{"value":"好好"}, 
{"value":"2015-08-24"}
],
[
{"value":"123"}, 
{"value":"hahah"}
]
]
}; 
        if(data == &#39;&#39;){ 
          return; 
}else{
JSONToExcelConvertor(data.data, "Report", data.title); 
}
      }); 
    }); 
 
    function JSONToExcelConvertor(JSONData, FileName, ShowLabel) { 
      //先转化json 
      var arrData = typeof JSONData != &#39;object&#39; ? JSON.parse(JSONData) : JSONData; 
       
      var excel = &#39;<table>&#39;;   
       
      //设置表头 
      var row = "<tr>"; 
      for (var i = 0, l = ShowLabel.length; i < l; i++) { 
        row += "<td>" + ShowLabel[i].value + &#39;</td>&#39;; 
      } 
       
       
      //换行 
      excel += row + "</tr>"; 
       
      //设置数据 
      for (var i = 0; i < arrData.length; i++) { 
        var row = "<tr>"; 
         
        for (var index in arrData[i]) { 
          var value = arrData[i][index].value === "." ? "" : arrData[i][index].value; 
          row += &#39;<td>&#39; + value + &#39;</td>&#39;; 
        } 
         
        excel += row + "</tr>"; 
      } 
 
      excel += "</table>"; 
 
      var excelFile = "<html xmlns:o=&#39;urn:schemas-microsoft-com:office:office&#39; xmlns:x=&#39;urn:schemas-microsoft-com:office:excel&#39; xmlns=&#39;http://www.w3.org/TR/REC-html40&#39;>"; 
      excelFile += &#39;<meta http-equiv="content-type" content="application/vnd.ms-excel; charset=UTF-8">&#39;; 
      excelFile += &#39;<meta http-equiv="content-type" content="application/vnd.ms-excel&#39;; 
      excelFile += &#39;; charset=UTF-8">&#39;; 
      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 = &#39;data:application/vnd.ms-excel;charset=utf-8,&#39; + 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); 
    } 
  </script> 
</head> 
<body> 
  <input type="button" id="wwo" value="导出" /> 
</body> 
</html>

위 내용은 제가 모두를 위해 정리한 내용입니다. 앞으로 모든 분들께 도움이 되기를 바랍니다.

관련 기사:

npm을 사용하여 vue 프로젝트 만들기(자세한 튜토리얼)

Node.js에서 fs.renameSync를 호출할 때 오류가 있나요?

VSCode에서 React Native 개발 환경을 구성하는 방법

vue에 Mint-UI를 설치하는 방법

AngularJS에서 수집 데이터 순회 표시를 구현하는 방법

vue에 mint를 통합하는 방법. js - UI의 회전식 이미지

위 내용은 json 형식을 Excel 테이블로 변환(자세한 튜토리얼)의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.