ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • sheetjs json 엑셀다운로드
    언어의 온도/javascript&jquery 2023. 3. 22. 10:23

     

    ▶ 2종류의 엑셀다운로드 방식을 알려주겠다

    → 1. json 엑셀다운로드

    → 2. table 엑셀다운로드

     

    * sheetjs 라이브러리 다운로드

    ▶ 파일명 : xlsx.full.min.js

    ( 아래사이트에서 다운로드를 하자 )

    https://github.com/SheetJS/sheetjs/tree/github/dist

     

    * sheetjs 스크립트 로드

    ( 다운로드 후 원하는 경로에 배치한뒤 로드하자 )

    <script src="/js/xlsx.full.min.js"></script>
     

     

     

    * sheetjs 로 엑셀다운로드를 해보자~ java poi 는 안녕

    ( json 데이터를 바로 엑셀다운로드 )

    //시트 초기화
    let sheet_js = "";
    
    //data 예제
    let data = [
      { name: "Alice", age: 25, city: "New York" },
      { name: "Bob", age: 30, city: "San Francisco" },
      { name: "Charlie", age: 35, city: "Seattle" }
    ];
    
    //json 데이터 → sheet 처리
    sheet_js = XLSX.utils.json_to_sheet(data);
    
    //엑셀 구성
    let wb = XLSX.utils.book_new();
    XLSX.utils.book_append_sheet(wb, sheet_js, "Sheet1");
    
    //엑셀 파일생성
    XLSX.writeFile(wb, ('엑셀_다운로드.xlsx'));
     

     

     

    * 조회된 테이블 데이터 엑셀다운로드

    ( 기본 샘플 table )

    <table id="list_table">
      <thead>
    	<th>한식<th>
        <th>일식<th>
      </thead>
    
      <tbody>
        <tr>
         <td>소불고기</td>
         <td>광어초밥</td>
        </tr>
        <tr>
         <td>소갈비</td>
         <td>연어초밥</td>
        </tr>
      </tbody>
    </table>
     

     

    * table 엑셀다운로드

    //옵션
    let wb_option =  {sheet:"시트이름",raw:true};
    
    //엑셀 구성
    let wb = XLSX.utils.table_to_book(document.getElementById('list_table'), wb_option);
    
    //엑셀 파일생성
    XLSX.writeFile(wb, ('테이블_엑셀다운로드.xlsx'));
     

     

     

    참~쉽죠잉~

     

    댓글

Designed by Tistory.