Hướng dẫn javascript export excel from json - javascript xuất excel từ json

Nhiều lần, chúng tôi có thể cần tải dữ liệu vào một tệp Excel một cách nhanh chóng. Trong blog này, chúng tôi sẽ nói về cách sử dụng Node.js.

Xuất JSON sang tệp Excel là một trường hợp sử dụng rất phổ biến để sao lưu dữ liệu quan trọng hoặc di chuyển dữ liệu giữa hai phiên bản khác nhau của chương trình. Xuất dữ liệu là trích xuất và chuyển đổi dữ liệu thô từ định dạng hiện có thành định dạng theo yêu cầu của

Đoạn mã này đang sử dụng Node.js với các mô -đun Excel4Node và Express để chuyển đổi dữ liệu JSON thành tệp Excel và gửi cho máy khách, sử dụng JavaScript.

const xl = require['excel4node'];
const express = require['express'];
const app = express[];

var json = [{"Vehicle":"BMW","Date":"30, Jul 2013 09:24 AM","Location":"Hauz Khas, Enclave, New Delhi, Delhi, India","Speed":42},{"Vehicle":"Honda CBR","Date":"30, Jul 2013 12:00 AM","Location":"Military Road,  West Bengal 734013,  India","Speed":0},{"Vehicle":"Supra","Date":"30, Jul 2013 07:53 AM","Location":"Sec-45, St. Angel's School, Gurgaon, Haryana, India","Speed":58},{"Vehicle":"Land Cruiser","Date":"30, Jul 2013 09:35 AM","Location":"DLF Phase I, Marble Market, Gurgaon, Haryana, India","Speed":83},{"Vehicle":"Suzuki Swift","Date":"30, Jul 2013 12:02 AM","Location":"Behind Central Bank RO, Ram Krishna Rd by-lane, Siliguri, West Bengal, India","Speed":0},{"Vehicle":"Honda Civic","Date":"30, Jul 2013 12:00 AM","Location":"Behind Central Bank RO, Ram Krishna Rd by-lane, Siliguri, West Bengal, India","Speed":0},{"Vehicle":"Honda Accord","Date":"30, Jul 2013 11:05 AM","Location":"DLF Phase IV, Super Mart 1, Gurgaon, Haryana, India","Speed":71}]

const createSheet = [] => {

  return new Promise[resolve => {

// setup workbook and sheet
var wb = new xl.Workbook[];

var ws = wb.addWorksheet['Sheet'];

// Add a title row

ws.cell[1, 1]
  .string['Vehicle']

ws.cell[1, 2]
  .string['Date']

ws.cell[1, 3]
  .string['Location']

ws.cell[1, 4]
  .string['Speed']

// add data from json

for [let i = 0; i < json.length; i++] {

  let row = i + 2

  ws.cell[row, 1]
    .string[json[i].Vehicle]

  ws.cell[row, 2]
    .date[json[i].Date]

  ws.cell[row, 3]
    .string[json[i].Location]

  ws.cell[row, 4]
    .number[json[i].Speed]
}

resolve[ wb ]

  }]
}

app.get['/excel', function [req, res] {

  createSheet[].then[ file => {
file.write['ExcelFile.xlsx', res];
  }]

}];

app.listen[3040, function [] {
  console.log['Excel app listening on port 3040'];
}];

Về tác giả: Chirag Sharma

Chirag có kỹ năng trong JavaScript và có kinh nghiệm thực hành với Node.js, Redis và AWS. Ông đã hoàn thành bằng thạc sĩ ứng dụng máy tính của Đại học Kỹ thuật Rajasthan.

Thực hiện theo các bước dưới đây để xuất dữ liệu PHP JSON sang tệp Excel bằng JavaScript/JQuery:

  1. Sao chép mã bên dưới và sử dụng nó trong tệp PHP. Nó sẽ hoạt động.





    
        
        Excel export
        
            .export-data-block {
                padding-top: 50px;
                text-align: center;
                font-family: sans-serif;
            }
            .export-data-block form button {
                font-size: 25px;
                background: yellow;
                padding: 7px 21px;
            }
        
    
    
        

Export PHP JSON Data to Excel using JavaScript

Bài Viết Liên Quan

Chủ Đề