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.

 array('tx'=> '12121', 'type' =>'Giftcard', 'sender'=>'', 'receiver'=>'Marco', 'value'=>'100', 'note'=>'1001'),
    '1' => array('tx'=> '12121', 'type' =>'Giftcard', 'sender'=>'', 'receiver'=>'Marco', 'value'=>'100', 'note'=>'1001'),
    '2' => array('tx'=> '12121', 'type' =>'Giftcard', 'sender'=>'', 'receiver'=>'Marco', 'value'=>'100', 'note'=>'1001'),
    '3' => array('tx'=> '12121', 'type' =>'Giftcard', 'sender'=>'', 'receiver'=>'Marco', 'value'=>'100', 'note'=>'1001'),
    '4' => array('tx'=> '12121', 'type' =>'Giftcard', 'sender'=>'', 'receiver'=>'Marco', 'value'=>'100', 'note'=>'1001'),
    '5' => array('tx'=> '12121', 'type' =>'Giftcard', 'sender'=>'', 'receiver'=>'Marco', 'value'=>'100', 'note'=>'1001')
);

?>



    
        
        Excel export
        
    
    
        

Export PHP JSON Data to Excel using JavaScript

Làm cách nào để xuất dữ liệu JSON sang Excel?

Để nhập tệp JSON vào Excel, hãy mở Excel vào tab dữ liệu và điều hướng để lấy dữ liệu> từ tệp> từ JSON. Chọn cách bạn muốn dữ liệu của mình được nhập và Excel sẽ tự động xử lý phần còn lại.open Excel to the Data tab and navigate to Get Data > From File > From JSON. Pick how you want your data to be imported, and Excel will handle the rest automatically.

Làm cách nào để xuất dữ liệu JSON sang Excel trong React JS?

React.js mã..
Nhập phản ứng, {Thành phần} từ 'React' ;.
Nhập khẩu xuất khẩu từ 'Xuất từ-Json'.
const data = [{foo: 'foo'}, {bar: 'bar'}].
const filename = 'Tải xuống'.
const exportType = 'xls'.
Ứng dụng lớp mở rộng thành phần {.
ExportToExcel = () => {.
ExportFromJson ({Data, FileName, ExportType}).

Làm cách nào để xuất dữ liệu JSON sang Excel trong Node JS?

Cách lưu dữ liệu JSON trong tệp Excel bằng nút ...
Xác định dữ liệu của bạn bạn muốn được lưu trữ trong Excel.....
Nhập thư viện Excel4Node.....
Tạo một sổ làm việc và đặt một số tên tuyệt vời.....
Bây giờ hãy xác định tên cột.....
Bây giờ hãy viết CộtName trong tệp Excel bằng các hàm trong Excel4Node.....
(Đừng quên bắt đầu số hàng từ 2).

Làm cách nào tôi có thể tải xuống dữ liệu JSON từ jQuery xuống Excel?

Làm thế nào để sử dụng nó:..
Download and insert the JavaScript file excelexportjs. js after jQuery JavaScript library. < script src = "//code.jquery.com/jquery-3.2.0.min.js" > ... .
Xuất dữ liệu JSON của bạn sang Excel: Xem nguồn.....
Export your tabular data to Excel: < table id = 'tableData' > ... .
Tất cả các cài đặt plugin mặc định ..