Hướng dẫn read excel file cdn - đọc tệp excel cdn

import readXlsxFile from 'read-excel-file' // File. const input = document.getElementById('input') input.addEventListener('change', () => { readXlsxFile(input.files[0]).then((rows) => { // `rows` is an array of rows // each row being an array of cells. }) }) // Blob. fetch('https://example.com/spreadsheet.xlsx') .then(response => response.blob()) .then(blob => readXlsxFile(blob)) .then((rows) => { // `rows` is an array of rows // each row being an array of cells. }) // ArrayBuffer. // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer // // Could be obtained from: // * File // * Blob // * Base64 string // readXlsxFile(arrayBuffer).then((rows) => { // `rows` is an array of rows // each row being an array of cells. })0

Đọc các tệp từ nhỏ đến trung bình

import readXlsxFile from 'read-excel-file'

// File.
const input = document.getElementById('input')
input.addEventListener('change', () => {
  readXlsxFile(input.files[0]).then((rows) => {
    // `rows` is an array of rows
    // each row being an array of cells.
  })
})

// Blob.
fetch('https://example.com/spreadsheet.xlsx')
  .then(response => response.blob())
  .then(blob => readXlsxFile(blob))
  .then((rows) => {
    // `rows` is an array of rows
    // each row being an array of cells.
  })

// ArrayBuffer.
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer
//
// Could be obtained from:
// * File
// * Blob
// * Base64 string
//
readXlsxFile(arrayBuffer).then((rows) => {
  // `rows` is an array of rows
  // each row being an array of cells.
})
1 trong trình duyệt hoặc node.js. Phân tích thành JSON với một lược đồ nghiêm ngặt.

Thử nghiệm

Ngoài ra, hãy xem

import readXlsxFile from 'read-excel-file'

// File.
const input = document.getElementById('input')
input.addEventListener('change', () => {
  readXlsxFile(input.files[0]).then((rows) => {
    // `rows` is an array of rows
    // each row being an array of cells.
  })
})

// Blob.
fetch('https://example.com/spreadsheet.xlsx')
  .then(response => response.blob())
  .then(blob => readXlsxFile(blob))
  .then((rows) => {
    // `rows` is an array of rows
    // each row being an array of cells.
  })

// ArrayBuffer.
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer
//
// Could be obtained from:
// * File
// * Blob
// * Base64 string
//
readXlsxFile(arrayBuffer).then((rows) => {
  // `rows` is an array of rows
  // each row being an array of cells.
})
2 để viết các tệp
import readXlsxFile from 'read-excel-file'

// File.
const input = document.getElementById('input')
input.addEventListener('change', () => {
  readXlsxFile(input.files[0]).then((rows) => {
    // `rows` is an array of rows
    // each row being an array of cells.
  })
})

// Blob.
fetch('https://example.com/spreadsheet.xlsx')
  .then(response => response.blob())
  .then(blob => readXlsxFile(blob))
  .then((rows) => {
    // `rows` is an array of rows
    // each row being an array of cells.
  })

// ArrayBuffer.
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer
//
// Could be obtained from:
// * File
// * Blob
// * Base64 string
//
readXlsxFile(arrayBuffer).then((rows) => {
  // `rows` is an array of rows
  // each row being an array of cells.
})
1 đơn giản.

Cài đặt

npm install read-excel-file --save

Nếu bạn không sử dụng Bundler thì hãy sử dụng phiên bản độc lập từ CDN.

Sử dụng

Trình duyệt

<input type="file" id="input" />

import readXlsxFile from 'read-excel-file'

// File.
const input = document.getElementById('input')
input.addEventListener('change', () => {
  readXlsxFile(input.files[0]).then((rows) => {
    // `rows` is an array of rows
    // each row being an array of cells.
  })
})

// Blob.
fetch('https://example.com/spreadsheet.xlsx')
  .then(response => response.blob())
  .then(blob => readXlsxFile(blob))
  .then((rows) => {
    // `rows` is an array of rows
    // each row being an array of cells.
  })

// ArrayBuffer.
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer
//
// Could be obtained from:
// * File
// * Blob
// * Base64 string
//
readXlsxFile(arrayBuffer).then((rows) => {
  // `rows` is an array of rows
  // each row being an array of cells.
})

Lưu ý: Internet Explorer 11 yêu cầu polyfill

import readXlsxFile from 'read-excel-file'

// File.
const input = document.getElementById('input')
input.addEventListener('change', () => {
  readXlsxFile(input.files[0]).then((rows) => {
    // `rows` is an array of rows
    // each row being an array of cells.
  })
})

// Blob.
fetch('https://example.com/spreadsheet.xlsx')
  .then(response => response.blob())
  .then(blob => readXlsxFile(blob))
  .then((rows) => {
    // `rows` is an array of rows
    // each row being an array of cells.
  })

// ArrayBuffer.
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer
//
// Could be obtained from:
// * File
// * Blob
// * Base64 string
//
readXlsxFile(arrayBuffer).then((rows) => {
  // `rows` is an array of rows
  // each row being an array of cells.
})
4. Thí dụ.

Node.js

const readXlsxFile = require('read-excel-file/node')

// File path.
readXlsxFile('/path/to/file').then((rows) => {
  // `rows` is an array of rows
  // each row being an array of cells.
})

// Readable Stream.
readXlsxFile(fs.createReadStream('/path/to/file')).then((rows) => {
  // `rows` is an array of rows
  // each row being an array of cells.
})

// Buffer.
readXlsxFile(Buffer.from(fs.readFileSync('/path/to/file'))).then((rows) => {
  // `rows` is an array of rows
  // each row being an array of cells.
})

Công nhân web

const worker = new Worker('web-worker.js')

worker.onmessage = function(event) {
  // `event.data` is an array of rows
  // each row being an array of cells.
  console.log(event.data)
}

worker.onerror = function(event) {
  console.error(event.message)
}

const input = document.getElementById('input')

input.addEventListener('change', () => {
  worker.postMessage(input.files[0])
})

import readXlsxFile from 'read-excel-file'

// File.
const input = document.getElementById('input')
input.addEventListener('change', () => {
  readXlsxFile(input.files[0]).then((rows) => {
    // `rows` is an array of rows
    // each row being an array of cells.
  })
})

// Blob.
fetch('https://example.com/spreadsheet.xlsx')
  .then(response => response.blob())
  .then(blob => readXlsxFile(blob))
  .then((rows) => {
    // `rows` is an array of rows
    // each row being an array of cells.
  })

// ArrayBuffer.
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer
//
// Could be obtained from:
// * File
// * Blob
// * Base64 string
//
readXlsxFile(arrayBuffer).then((rows) => {
  // `rows` is an array of rows
  // each row being an array of cells.
})
5

import readXlsxFile from 'read-excel-file/web-worker'

onmessage = function(event) {
  readXlsxFile(event.data).then((rows) => {
    // `rows` is an array of rows
    // each row being an array of cells.
    postMessage(rows)
  })
}

Json

Để đọc dữ liệu bảng tính và sau đó chuyển đổi nó thành một mảng các đối tượng JSON, hãy truyền tùy chọn

import readXlsxFile from 'read-excel-file'

// File.
const input = document.getElementById('input')
input.addEventListener('change', () => {
  readXlsxFile(input.files[0]).then((rows) => {
    // `rows` is an array of rows
    // each row being an array of cells.
  })
})

// Blob.
fetch('https://example.com/spreadsheet.xlsx')
  .then(response => response.blob())
  .then(blob => readXlsxFile(blob))
  .then((rows) => {
    // `rows` is an array of rows
    // each row being an array of cells.
  })

// ArrayBuffer.
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer
//
// Could be obtained from:
// * File
// * Blob
// * Base64 string
//
readXlsxFile(arrayBuffer).then((rows) => {
  // `rows` is an array of rows
  // each row being an array of cells.
})
6 khi gọi
import readXlsxFile from 'read-excel-file'

// File.
const input = document.getElementById('input')
input.addEventListener('change', () => {
  readXlsxFile(input.files[0]).then((rows) => {
    // `rows` is an array of rows
    // each row being an array of cells.
  })
})

// Blob.
fetch('https://example.com/spreadsheet.xlsx')
  .then(response => response.blob())
  .then(blob => readXlsxFile(blob))
  .then((rows) => {
    // `rows` is an array of rows
    // each row being an array of cells.
  })

// ArrayBuffer.
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer
//
// Could be obtained from:
// * File
// * Blob
// * Base64 string
//
readXlsxFile(arrayBuffer).then((rows) => {
  // `rows` is an array of rows
  // each row being an array of cells.
})
7. Trong trường hợp đó, thay vì trả lại một loạt các hàng tế bào, nó sẽ trả về một đối tượng hình dạng
import readXlsxFile from 'read-excel-file'

// File.
const input = document.getElementById('input')
input.addEventListener('change', () => {
  readXlsxFile(input.files[0]).then((rows) => {
    // `rows` is an array of rows
    // each row being an array of cells.
  })
})

// Blob.
fetch('https://example.com/spreadsheet.xlsx')
  .then(response => response.blob())
  .then(blob => readXlsxFile(blob))
  .then((rows) => {
    // `rows` is an array of rows
    // each row being an array of cells.
  })

// ArrayBuffer.
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer
//
// Could be obtained from:
// * File
// * Blob
// * Base64 string
//
readXlsxFile(arrayBuffer).then((rows) => {
  // `rows` is an array of rows
  // each row being an array of cells.
})
8 trong đó
import readXlsxFile from 'read-excel-file'

// File.
const input = document.getElementById('input')
input.addEventListener('change', () => {
  readXlsxFile(input.files[0]).then((rows) => {
    // `rows` is an array of rows
    // each row being an array of cells.
  })
})

// Blob.
fetch('https://example.com/spreadsheet.xlsx')
  .then(response => response.blob())
  .then(blob => readXlsxFile(blob))
  .then((rows) => {
    // `rows` is an array of rows
    // each row being an array of cells.
  })

// ArrayBuffer.
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer
//
// Could be obtained from:
// * File
// * Blob
// * Base64 string
//
readXlsxFile(arrayBuffer).then((rows) => {
  // `rows` is an array of rows
  // each row being an array of cells.
})
9 sẽ là một loạt các đối tượng JSON được tạo từ dữ liệu bảng tính theo
import readXlsxFile from 'read-excel-file'

// File.
const input = document.getElementById('input')
input.addEventListener('change', () => {
  readXlsxFile(input.files[0]).then((rows) => {
    // `rows` is an array of rows
    // each row being an array of cells.
  })
})

// Blob.
fetch('https://example.com/spreadsheet.xlsx')
  .then(response => response.blob())
  .then(blob => readXlsxFile(blob))
  .then((rows) => {
    // `rows` is an array of rows
    // each row being an array of cells.
  })

// ArrayBuffer.
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer
//
// Could be obtained from:
// * File
// * Blob
// * Base64 string
//
readXlsxFile(arrayBuffer).then((rows) => {
  // `rows` is an array of rows
  // each row being an array of cells.
})
6 và
const readXlsxFile = require('read-excel-file/node')

// File path.
readXlsxFile('/path/to/file').then((rows) => {
  // `rows` is an array of rows
  // each row being an array of cells.
})

// Readable Stream.
readXlsxFile(fs.createReadStream('/path/to/file')).then((rows) => {
  // `rows` is an array of rows
  // each row being an array of cells.
})

// Buffer.
readXlsxFile(Buffer.from(fs.readFileSync('/path/to/file'))).then((rows) => {
  // `rows` is an array of rows
  // each row being an array of cells.
})
1 sẽ là một mảng của Lỗi gặp phải trong khi chuyển đổi dữ liệu bảng tính sang các đối tượng JSON.

Mỗi thuộc tính của một đối tượng JSON nên được mô tả bởi một "mục" trong

import readXlsxFile from 'read-excel-file'

// File.
const input = document.getElementById('input')
input.addEventListener('change', () => {
  readXlsxFile(input.files[0]).then((rows) => {
    // `rows` is an array of rows
    // each row being an array of cells.
  })
})

// Blob.
fetch('https://example.com/spreadsheet.xlsx')
  .then(response => response.blob())
  .then(blob => readXlsxFile(blob))
  .then((rows) => {
    // `rows` is an array of rows
    // each row being an array of cells.
  })

// ArrayBuffer.
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer
//
// Could be obtained from:
// * File
// * Blob
// * Base64 string
//
readXlsxFile(arrayBuffer).then((rows) => {
  // `rows` is an array of rows
  // each row being an array of cells.
})
6. Chìa khóa của mục phải là tiêu đề của cột trong bảng tính. Giá trị của mục nhập phải là một đối tượng có thuộc tính:

  • const readXlsxFile = require('read-excel-file/node')
    
    // File path.
    readXlsxFile('/path/to/file').then((rows) => {
      // `rows` is an array of rows
      // each row being an array of cells.
    })
    
    // Readable Stream.
    readXlsxFile(fs.createReadStream('/path/to/file')).then((rows) => {
      // `rows` is an array of rows
      // each row being an array of cells.
    })
    
    // Buffer.
    readXlsxFile(Buffer.from(fs.readFileSync('/path/to/file'))).then((rows) => {
      // `rows` is an array of rows
      // each row being an array of cells.
    })
    3 - Tên thuộc tính của đối tượng.
  • const readXlsxFile = require('read-excel-file/node')
    
    // File path.
    readXlsxFile('/path/to/file').then((rows) => {
      // `rows` is an array of rows
      // each row being an array of cells.
    })
    
    // Readable Stream.
    readXlsxFile(fs.createReadStream('/path/to/file')).then((rows) => {
      // `rows` is an array of rows
      // each row being an array of cells.
    })
    
    // Buffer.
    readXlsxFile(Buffer.from(fs.readFileSync('/path/to/file'))).then((rows) => {
      // `rows` is an array of rows
      // each row being an array of cells.
    })
    4 - (Tùy chọn) Các thuộc tính bắt buộc có thể được đánh dấu là
    const readXlsxFile = require('read-excel-file/node')
    
    // File path.
    readXlsxFile('/path/to/file').then((rows) => {
      // `rows` is an array of rows
      // each row being an array of cells.
    })
    
    // Readable Stream.
    readXlsxFile(fs.createReadStream('/path/to/file')).then((rows) => {
      // `rows` is an array of rows
      // each row being an array of cells.
    })
    
    // Buffer.
    readXlsxFile(Buffer.from(fs.readFileSync('/path/to/file'))).then((rows) => {
      // `rows` is an array of rows
      // each row being an array of cells.
    })
    5.
  • const readXlsxFile = require('read-excel-file/node')
    
    // File path.
    readXlsxFile('/path/to/file').then((rows) => {
      // `rows` is an array of rows
      // each row being an array of cells.
    })
    
    // Readable Stream.
    readXlsxFile(fs.createReadStream('/path/to/file')).then((rows) => {
      // `rows` is an array of rows
      // each row being an array of cells.
    })
    
    // Buffer.
    readXlsxFile(Buffer.from(fs.readFileSync('/path/to/file'))).then((rows) => {
      // `rows` is an array of rows
      // each row being an array of cells.
    })
    6 - (Tùy chọn) Hàm xác thực giá trị ô. Chỉ được gọi trên các tế bào không trống. Nếu giá trị ô không hợp lệ, nó sẽ ném lỗi với thông báo lỗi được đặt thành mã lỗi.
  • const readXlsxFile = require('read-excel-file/node')
    
    // File path.
    readXlsxFile('/path/to/file').then((rows) => {
      // `rows` is an array of rows
      // each row being an array of cells.
    })
    
    // Readable Stream.
    readXlsxFile(fs.createReadStream('/path/to/file')).then((rows) => {
      // `rows` is an array of rows
      // each row being an array of cells.
    })
    
    // Buffer.
    readXlsxFile(Buffer.from(fs.readFileSync('/path/to/file'))).then((rows) => {
      // `rows` is an array of rows
      // each row being an array of cells.
    })
    7 - (Tùy chọn) Loại giá trị. Xác định cách giá trị ô sẽ được phân tích cú pháp. Nếu không có
    const readXlsxFile = require('read-excel-file/node')
    
    // File path.
    readXlsxFile('/path/to/file').then((rows) => {
      // `rows` is an array of rows
      // each row being an array of cells.
    })
    
    // Readable Stream.
    readXlsxFile(fs.createReadStream('/path/to/file')).then((rows) => {
      // `rows` is an array of rows
      // each row being an array of cells.
    })
    
    // Buffer.
    readXlsxFile(Buffer.from(fs.readFileSync('/path/to/file'))).then((rows) => {
      // `rows` is an array of rows
      // each row being an array of cells.
    })
    7 được chỉ định thì giá trị ô được trả về "như là": dưới dạng chuỗi, số, ngày hoặc boolean. A
    const readXlsxFile = require('read-excel-file/node')
    
    // File path.
    readXlsxFile('/path/to/file').then((rows) => {
      // `rows` is an array of rows
      // each row being an array of cells.
    })
    
    // Readable Stream.
    readXlsxFile(fs.createReadStream('/path/to/file')).then((rows) => {
      // `rows` is an array of rows
      // each row being an array of cells.
    })
    
    // Buffer.
    readXlsxFile(Buffer.from(fs.readFileSync('/path/to/file'))).then((rows) => {
      // `rows` is an array of rows
      // each row being an array of cells.
    })
    7 có thể là:
    • Loại tích hợp:
      • const worker = new Worker('web-worker.js')
        
        worker.onmessage = function(event) {
          // `event.data` is an array of rows
          // each row being an array of cells.
          console.log(event.data)
        }
        
        worker.onerror = function(event) {
          console.error(event.message)
        }
        
        const input = document.getElementById('input')
        
        input.addEventListener('change', () => {
          worker.postMessage(input.files[0])
        })
        0
      • const worker = new Worker('web-worker.js')
        
        worker.onmessage = function(event) {
          // `event.data` is an array of rows
          // each row being an array of cells.
          console.log(event.data)
        }
        
        worker.onerror = function(event) {
          console.error(event.message)
        }
        
        const input = document.getElementById('input')
        
        input.addEventListener('change', () => {
          worker.postMessage(input.files[0])
        })
        1
      • const worker = new Worker('web-worker.js')
        
        worker.onmessage = function(event) {
          // `event.data` is an array of rows
          // each row being an array of cells.
          console.log(event.data)
        }
        
        worker.onerror = function(event) {
          console.error(event.message)
        }
        
        const input = document.getElementById('input')
        
        input.addEventListener('change', () => {
          worker.postMessage(input.files[0])
        })
        2
      • const worker = new Worker('web-worker.js')
        
        worker.onmessage = function(event) {
          // `event.data` is an array of rows
          // each row being an array of cells.
          console.log(event.data)
        }
        
        worker.onerror = function(event) {
          console.error(event.message)
        }
        
        const input = document.getElementById('input')
        
        input.addEventListener('change', () => {
          worker.postMessage(input.files[0])
        })
        3
    • Loại "Tiện ích" được xuất từ ​​thư viện:
      • const worker = new Worker('web-worker.js')
        
        worker.onmessage = function(event) {
          // `event.data` is an array of rows
          // each row being an array of cells.
          console.log(event.data)
        }
        
        worker.onerror = function(event) {
          console.error(event.message)
        }
        
        const input = document.getElementById('input')
        
        input.addEventListener('change', () => {
          worker.postMessage(input.files[0])
        })
        4
      • const worker = new Worker('web-worker.js')
        
        worker.onmessage = function(event) {
          // `event.data` is an array of rows
          // each row being an array of cells.
          console.log(event.data)
        }
        
        worker.onerror = function(event) {
          console.error(event.message)
        }
        
        const input = document.getElementById('input')
        
        input.addEventListener('change', () => {
          worker.postMessage(input.files[0])
        })
        5
      • const worker = new Worker('web-worker.js')
        
        worker.onmessage = function(event) {
          // `event.data` is an array of rows
          // each row being an array of cells.
          console.log(event.data)
        }
        
        worker.onerror = function(event) {
          console.error(event.message)
        }
        
        const input = document.getElementById('input')
        
        input.addEventListener('change', () => {
          worker.postMessage(input.files[0])
        })
        6
    • Loại tùy chỉnh:
      • Một hàm nhận giá trị ô và trả về giá trị phân tích cú pháp. Nếu giá trị không hợp lệ, nó sẽ ném lỗi với thông báo lỗi được đặt thành mã lỗi.

Sidenote: Khi chuyển đổi các giá trị ô thành các thuộc tính đối tượng, theo mặc định, nó bỏ qua tất cả các giá trị

const worker = new Worker('web-worker.js')

worker.onmessage = function(event) {
  // `event.data` is an array of rows
  // each row being an array of cells.
  console.log(event.data)
}

worker.onerror = function(event) {
  console.error(event.message)
}

const input = document.getElementById('input')

input.addEventListener('change', () => {
  worker.postMessage(input.files[0])
})
7 (bỏ qua tất cả các ô trống). Đó là cho sự đơn giản. Trong một số trường hợp cạnh, có thể cần phải giữ tất cả các giá trị
const worker = new Worker('web-worker.js')

worker.onmessage = function(event) {
  // `event.data` is an array of rows
  // each row being an array of cells.
  console.log(event.data)
}

worker.onerror = function(event) {
  console.error(event.message)
}

const input = document.getElementById('input')

input.addEventListener('change', () => {
  worker.postMessage(input.files[0])
})
7 cho tất cả các ô trống. Ví dụ: đó là trường hợp khi cập nhật dữ liệu trong cơ sở dữ liệu SQL từ bảng tính XLSX bằng thư viện ORM phần tiếp theo yêu cầu một thuộc tính phải rõ ràng là ____47 để xóa nó trong quá trình hoạt động
import readXlsxFile from 'read-excel-file/web-worker'

onmessage = function(event) {
  readXlsxFile(event.data).then((rows) => {
    // `rows` is an array of rows
    // each row being an array of cells.
    postMessage(rows)
  })
}
0. Để giữ tất cả các giá trị
const worker = new Worker('web-worker.js')

worker.onmessage = function(event) {
  // `event.data` is an array of rows
  // each row being an array of cells.
  console.log(event.data)
}

worker.onerror = function(event) {
  console.error(event.message)
}

const input = document.getElementById('input')

input.addEventListener('change', () => {
  worker.postMessage(input.files[0])
})
7, hãy vượt qua tùy chọn
import readXlsxFile from 'read-excel-file/web-worker'

onmessage = function(event) {
  readXlsxFile(event.data).then((rows) => {
    // `rows` is an array of rows
    // each row being an array of cells.
    postMessage(rows)
  })
}
2 khi gọi
import readXlsxFile from 'read-excel-file'

// File.
const input = document.getElementById('input')
input.addEventListener('change', () => {
  readXlsxFile(input.files[0]).then((rows) => {
    // `rows` is an array of rows
    // each row being an array of cells.
  })
})

// Blob.
fetch('https://example.com/spreadsheet.xlsx')
  .then(response => response.blob())
  .then(blob => readXlsxFile(blob))
  .then((rows) => {
    // `rows` is an array of rows
    // each row being an array of cells.
  })

// ArrayBuffer.
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer
//
// Could be obtained from:
// * File
// * Blob
// * Base64 string
//
readXlsxFile(arrayBuffer).then((rows) => {
  // `rows` is an array of rows
  // each row being an array of cells.
})
7.

const readXlsxFile = require('read-excel-file/node')

// File path.
readXlsxFile('/path/to/file').then((rows) => {
  // `rows` is an array of rows
  // each row being an array of cells.
})

// Readable Stream.
readXlsxFile(fs.createReadStream('/path/to/file')).then((rows) => {
  // `rows` is an array of rows
  // each row being an array of cells.
})

// Buffer.
readXlsxFile(Buffer.from(fs.readFileSync('/path/to/file'))).then((rows) => {
  // `rows` is an array of rows
  // each row being an array of cells.
})
1

Nếu có bất kỳ lỗi nào trong khi chuyển đổi dữ liệu bảng tính thành các đối tượng JSON, thuộc tính

const readXlsxFile = require('read-excel-file/node')

// File path.
readXlsxFile('/path/to/file').then((rows) => {
  // `rows` is an array of rows
  // each row being an array of cells.
})

// Readable Stream.
readXlsxFile(fs.createReadStream('/path/to/file')).then((rows) => {
  // `rows` is an array of rows
  // each row being an array of cells.
})

// Buffer.
readXlsxFile(Buffer.from(fs.readFileSync('/path/to/file'))).then((rows) => {
  // `rows` is an array of rows
  // each row being an array of cells.
})
1 được trả về từ hàm sẽ là một mảng không trống. Một phần tử của thuộc tính
const readXlsxFile = require('read-excel-file/node')

// File path.
readXlsxFile('/path/to/file').then((rows) => {
  // `rows` is an array of rows
  // each row being an array of cells.
})

// Readable Stream.
readXlsxFile(fs.createReadStream('/path/to/file')).then((rows) => {
  // `rows` is an array of rows
  // each row being an array of cells.
})

// Buffer.
readXlsxFile(Buffer.from(fs.readFileSync('/path/to/file'))).then((rows) => {
  // `rows` is an array of rows
  // each row being an array of cells.
})
1 chứa các thuộc tính:

  • import readXlsxFile from 'read-excel-file/web-worker'
    
    onmessage = function(event) {
      readXlsxFile(event.data).then((rows) => {
        // `rows` is an array of rows
        // each row being an array of cells.
        postMessage(rows)
      })
    }
    7 - Mã lỗi. Ví dụ:
    import readXlsxFile from 'read-excel-file/web-worker'
    
    onmessage = function(event) {
      readXlsxFile(event.data).then((rows) => {
        // `rows` is an array of rows
        // each row being an array of cells.
        postMessage(rows)
      })
    }
    8,
    import readXlsxFile from 'read-excel-file/web-worker'
    
    onmessage = function(event) {
      readXlsxFile(event.data).then((rows) => {
        // `rows` is an array of rows
        // each row being an array of cells.
        postMessage(rows)
      })
    }
    9.
    • Nếu hàm
      // An example *.xlsx document:
      // -----------------------------------------------------------------------------------------
      // | START DATE | NUMBER OF STUDENTS | IS FREE | COURSE TITLE |    CONTACT     |  STATUS   |
      // -----------------------------------------------------------------------------------------
      // | 03/24/2018 |         10         |   true  |  Chemistry   | (123) 456-7890 | SCHEDULED |
      // -----------------------------------------------------------------------------------------
      
      const schema = {
        'START DATE': {
          // JSON object property name.
          prop: 'date',
          type: Date
        },
        'NUMBER OF STUDENTS': {
          prop: 'numberOfStudents',
          type: Number,
          required: true
        },
        // Nested object example.
        // 'COURSE' here is not a real Excel file column name,
        // it can be any string — it's just for code readability.
        'COURSE': {
          // Nested object path: `row.course`
          prop: 'course',
          // Nested object schema:
          type: {
            'IS FREE': {
              prop: 'isFree',
              type: Boolean
            },
            'COURSE TITLE': {
              prop: 'title',
              type: String
            }
          }
        },
        'CONTACT': {
          prop: 'contact',
          required: true,
          // A custom `type` can be defined.
          // A `type` function only gets called for non-empty cells.
          type: (value) => {
            const number = parsePhoneNumber(value)
            if (!number) {
              throw new Error('invalid')
            }
            return number
          }
        },
        'STATUS': {
          prop: 'status',
          type: String,
          oneOf: [
            'SCHEDULED',
            'STARTED',
            'FINISHED'
          ]
        }
      }
      
      readXlsxFile(file, { schema }).then(({ rows, errors }) => {
        // `errors` list items have shape: `{ row, column, error, reason?, value?, type? }`.
        errors.length === 0
      
        rows === [{
          date: new Date(2018, 2, 24),
          numberOfStudents: 10,
          course: {
            isFree: true,
            title: 'Chemistry'
          },
          contact: '+11234567890',
          status: 'SCHEDULED'
        }]
      })
      0 tùy chỉnh được xác định và nó ném
      // An example *.xlsx document:
      // -----------------------------------------------------------------------------------------
      // | START DATE | NUMBER OF STUDENTS | IS FREE | COURSE TITLE |    CONTACT     |  STATUS   |
      // -----------------------------------------------------------------------------------------
      // | 03/24/2018 |         10         |   true  |  Chemistry   | (123) 456-7890 | SCHEDULED |
      // -----------------------------------------------------------------------------------------
      
      const schema = {
        'START DATE': {
          // JSON object property name.
          prop: 'date',
          type: Date
        },
        'NUMBER OF STUDENTS': {
          prop: 'numberOfStudents',
          type: Number,
          required: true
        },
        // Nested object example.
        // 'COURSE' here is not a real Excel file column name,
        // it can be any string — it's just for code readability.
        'COURSE': {
          // Nested object path: `row.course`
          prop: 'course',
          // Nested object schema:
          type: {
            'IS FREE': {
              prop: 'isFree',
              type: Boolean
            },
            'COURSE TITLE': {
              prop: 'title',
              type: String
            }
          }
        },
        'CONTACT': {
          prop: 'contact',
          required: true,
          // A custom `type` can be defined.
          // A `type` function only gets called for non-empty cells.
          type: (value) => {
            const number = parsePhoneNumber(value)
            if (!number) {
              throw new Error('invalid')
            }
            return number
          }
        },
        'STATUS': {
          prop: 'status',
          type: String,
          oneOf: [
            'SCHEDULED',
            'STARTED',
            'FINISHED'
          ]
        }
      }
      
      readXlsxFile(file, { schema }).then(({ rows, errors }) => {
        // `errors` list items have shape: `{ row, column, error, reason?, value?, type? }`.
        errors.length === 0
      
        rows === [{
          date: new Date(2018, 2, 24),
          numberOfStudents: 10,
          course: {
            isFree: true,
            title: 'Chemistry'
          },
          contact: '+11234567890',
          status: 'SCHEDULED'
        }]
      })
      1 thì thuộc tính
      // An example *.xlsx document:
      // -----------------------------------------------------------------------------------------
      // | START DATE | NUMBER OF STUDENTS | IS FREE | COURSE TITLE |    CONTACT     |  STATUS   |
      // -----------------------------------------------------------------------------------------
      // | 03/24/2018 |         10         |   true  |  Chemistry   | (123) 456-7890 | SCHEDULED |
      // -----------------------------------------------------------------------------------------
      
      const schema = {
        'START DATE': {
          // JSON object property name.
          prop: 'date',
          type: Date
        },
        'NUMBER OF STUDENTS': {
          prop: 'numberOfStudents',
          type: Number,
          required: true
        },
        // Nested object example.
        // 'COURSE' here is not a real Excel file column name,
        // it can be any string — it's just for code readability.
        'COURSE': {
          // Nested object path: `row.course`
          prop: 'course',
          // Nested object schema:
          type: {
            'IS FREE': {
              prop: 'isFree',
              type: Boolean
            },
            'COURSE TITLE': {
              prop: 'title',
              type: String
            }
          }
        },
        'CONTACT': {
          prop: 'contact',
          required: true,
          // A custom `type` can be defined.
          // A `type` function only gets called for non-empty cells.
          type: (value) => {
            const number = parsePhoneNumber(value)
            if (!number) {
              throw new Error('invalid')
            }
            return number
          }
        },
        'STATUS': {
          prop: 'status',
          type: String,
          oneOf: [
            'SCHEDULED',
            'STARTED',
            'FINISHED'
          ]
        }
      }
      
      readXlsxFile(file, { schema }).then(({ rows, errors }) => {
        // `errors` list items have shape: `{ row, column, error, reason?, value?, type? }`.
        errors.length === 0
      
        rows === [{
          date: new Date(2018, 2, 24),
          numberOfStudents: 10,
          course: {
            isFree: true,
            title: 'Chemistry'
          },
          contact: '+11234567890',
          status: 'SCHEDULED'
        }]
      })
      2 sẽ giống như giá trị
      // An example *.xlsx document:
      // -----------------------------------------------------------------------------------------
      // | START DATE | NUMBER OF STUDENTS | IS FREE | COURSE TITLE |    CONTACT     |  STATUS   |
      // -----------------------------------------------------------------------------------------
      // | 03/24/2018 |         10         |   true  |  Chemistry   | (123) 456-7890 | SCHEDULED |
      // -----------------------------------------------------------------------------------------
      
      const schema = {
        'START DATE': {
          // JSON object property name.
          prop: 'date',
          type: Date
        },
        'NUMBER OF STUDENTS': {
          prop: 'numberOfStudents',
          type: Number,
          required: true
        },
        // Nested object example.
        // 'COURSE' here is not a real Excel file column name,
        // it can be any string — it's just for code readability.
        'COURSE': {
          // Nested object path: `row.course`
          prop: 'course',
          // Nested object schema:
          type: {
            'IS FREE': {
              prop: 'isFree',
              type: Boolean
            },
            'COURSE TITLE': {
              prop: 'title',
              type: String
            }
          }
        },
        'CONTACT': {
          prop: 'contact',
          required: true,
          // A custom `type` can be defined.
          // A `type` function only gets called for non-empty cells.
          type: (value) => {
            const number = parsePhoneNumber(value)
            if (!number) {
              throw new Error('invalid')
            }
            return number
          }
        },
        'STATUS': {
          prop: 'status',
          type: String,
          oneOf: [
            'SCHEDULED',
            'STARTED',
            'FINISHED'
          ]
        }
      }
      
      readXlsxFile(file, { schema }).then(({ rows, errors }) => {
        // `errors` list items have shape: `{ row, column, error, reason?, value?, type? }`.
        errors.length === 0
      
        rows === [{
          date: new Date(2018, 2, 24),
          numberOfStudents: 10,
          course: {
            isFree: true,
            title: 'Chemistry'
          },
          contact: '+11234567890',
          status: 'SCHEDULED'
        }]
      })
      3.
    • Nếu hàm
      // An example *.xlsx document:
      // -----------------------------------------------------------------------------------------
      // | START DATE | NUMBER OF STUDENTS | IS FREE | COURSE TITLE |    CONTACT     |  STATUS   |
      // -----------------------------------------------------------------------------------------
      // | 03/24/2018 |         10         |   true  |  Chemistry   | (123) 456-7890 | SCHEDULED |
      // -----------------------------------------------------------------------------------------
      
      const schema = {
        'START DATE': {
          // JSON object property name.
          prop: 'date',
          type: Date
        },
        'NUMBER OF STUDENTS': {
          prop: 'numberOfStudents',
          type: Number,
          required: true
        },
        // Nested object example.
        // 'COURSE' here is not a real Excel file column name,
        // it can be any string — it's just for code readability.
        'COURSE': {
          // Nested object path: `row.course`
          prop: 'course',
          // Nested object schema:
          type: {
            'IS FREE': {
              prop: 'isFree',
              type: Boolean
            },
            'COURSE TITLE': {
              prop: 'title',
              type: String
            }
          }
        },
        'CONTACT': {
          prop: 'contact',
          required: true,
          // A custom `type` can be defined.
          // A `type` function only gets called for non-empty cells.
          type: (value) => {
            const number = parsePhoneNumber(value)
            if (!number) {
              throw new Error('invalid')
            }
            return number
          }
        },
        'STATUS': {
          prop: 'status',
          type: String,
          oneOf: [
            'SCHEDULED',
            'STARTED',
            'FINISHED'
          ]
        }
      }
      
      readXlsxFile(file, { schema }).then(({ rows, errors }) => {
        // `errors` list items have shape: `{ row, column, error, reason?, value?, type? }`.
        errors.length === 0
      
        rows === [{
          date: new Date(2018, 2, 24),
          numberOfStudents: 10,
          course: {
            isFree: true,
            title: 'Chemistry'
          },
          contact: '+11234567890',
          status: 'SCHEDULED'
        }]
      })
      4 tùy chỉnh được xác định và nó ném
      // An example *.xlsx document:
      // -----------------------------------------------------------------------------------------
      // | START DATE | NUMBER OF STUDENTS | IS FREE | COURSE TITLE |    CONTACT     |  STATUS   |
      // -----------------------------------------------------------------------------------------
      // | 03/24/2018 |         10         |   true  |  Chemistry   | (123) 456-7890 | SCHEDULED |
      // -----------------------------------------------------------------------------------------
      
      const schema = {
        'START DATE': {
          // JSON object property name.
          prop: 'date',
          type: Date
        },
        'NUMBER OF STUDENTS': {
          prop: 'numberOfStudents',
          type: Number,
          required: true
        },
        // Nested object example.
        // 'COURSE' here is not a real Excel file column name,
        // it can be any string — it's just for code readability.
        'COURSE': {
          // Nested object path: `row.course`
          prop: 'course',
          // Nested object schema:
          type: {
            'IS FREE': {
              prop: 'isFree',
              type: Boolean
            },
            'COURSE TITLE': {
              prop: 'title',
              type: String
            }
          }
        },
        'CONTACT': {
          prop: 'contact',
          required: true,
          // A custom `type` can be defined.
          // A `type` function only gets called for non-empty cells.
          type: (value) => {
            const number = parsePhoneNumber(value)
            if (!number) {
              throw new Error('invalid')
            }
            return number
          }
        },
        'STATUS': {
          prop: 'status',
          type: String,
          oneOf: [
            'SCHEDULED',
            'STARTED',
            'FINISHED'
          ]
        }
      }
      
      readXlsxFile(file, { schema }).then(({ rows, errors }) => {
        // `errors` list items have shape: `{ row, column, error, reason?, value?, type? }`.
        errors.length === 0
      
        rows === [{
          date: new Date(2018, 2, 24),
          numberOfStudents: 10,
          course: {
            isFree: true,
            title: 'Chemistry'
          },
          contact: '+11234567890',
          status: 'SCHEDULED'
        }]
      })
      1 thì thuộc tính
      // An example *.xlsx document:
      // -----------------------------------------------------------------------------------------
      // | START DATE | NUMBER OF STUDENTS | IS FREE | COURSE TITLE |    CONTACT     |  STATUS   |
      // -----------------------------------------------------------------------------------------
      // | 03/24/2018 |         10         |   true  |  Chemistry   | (123) 456-7890 | SCHEDULED |
      // -----------------------------------------------------------------------------------------
      
      const schema = {
        'START DATE': {
          // JSON object property name.
          prop: 'date',
          type: Date
        },
        'NUMBER OF STUDENTS': {
          prop: 'numberOfStudents',
          type: Number,
          required: true
        },
        // Nested object example.
        // 'COURSE' here is not a real Excel file column name,
        // it can be any string — it's just for code readability.
        'COURSE': {
          // Nested object path: `row.course`
          prop: 'course',
          // Nested object schema:
          type: {
            'IS FREE': {
              prop: 'isFree',
              type: Boolean
            },
            'COURSE TITLE': {
              prop: 'title',
              type: String
            }
          }
        },
        'CONTACT': {
          prop: 'contact',
          required: true,
          // A custom `type` can be defined.
          // A `type` function only gets called for non-empty cells.
          type: (value) => {
            const number = parsePhoneNumber(value)
            if (!number) {
              throw new Error('invalid')
            }
            return number
          }
        },
        'STATUS': {
          prop: 'status',
          type: String,
          oneOf: [
            'SCHEDULED',
            'STARTED',
            'FINISHED'
          ]
        }
      }
      
      readXlsxFile(file, { schema }).then(({ rows, errors }) => {
        // `errors` list items have shape: `{ row, column, error, reason?, value?, type? }`.
        errors.length === 0
      
        rows === [{
          date: new Date(2018, 2, 24),
          numberOfStudents: 10,
          course: {
            isFree: true,
            title: 'Chemistry'
          },
          contact: '+11234567890',
          status: 'SCHEDULED'
        }]
      })
      2 sẽ giống như giá trị
      // An example *.xlsx document:
      // -----------------------------------------------------------------------------------------
      // | START DATE | NUMBER OF STUDENTS | IS FREE | COURSE TITLE |    CONTACT     |  STATUS   |
      // -----------------------------------------------------------------------------------------
      // | 03/24/2018 |         10         |   true  |  Chemistry   | (123) 456-7890 | SCHEDULED |
      // -----------------------------------------------------------------------------------------
      
      const schema = {
        'START DATE': {
          // JSON object property name.
          prop: 'date',
          type: Date
        },
        'NUMBER OF STUDENTS': {
          prop: 'numberOfStudents',
          type: Number,
          required: true
        },
        // Nested object example.
        // 'COURSE' here is not a real Excel file column name,
        // it can be any string — it's just for code readability.
        'COURSE': {
          // Nested object path: `row.course`
          prop: 'course',
          // Nested object schema:
          type: {
            'IS FREE': {
              prop: 'isFree',
              type: Boolean
            },
            'COURSE TITLE': {
              prop: 'title',
              type: String
            }
          }
        },
        'CONTACT': {
          prop: 'contact',
          required: true,
          // A custom `type` can be defined.
          // A `type` function only gets called for non-empty cells.
          type: (value) => {
            const number = parsePhoneNumber(value)
            if (!number) {
              throw new Error('invalid')
            }
            return number
          }
        },
        'STATUS': {
          prop: 'status',
          type: String,
          oneOf: [
            'SCHEDULED',
            'STARTED',
            'FINISHED'
          ]
        }
      }
      
      readXlsxFile(file, { schema }).then(({ rows, errors }) => {
        // `errors` list items have shape: `{ row, column, error, reason?, value?, type? }`.
        errors.length === 0
      
        rows === [{
          date: new Date(2018, 2, 24),
          numberOfStudents: 10,
          course: {
            isFree: true,
            title: 'Chemistry'
          },
          contact: '+11234567890',
          status: 'SCHEDULED'
        }]
      })
      3.
  • // An example *.xlsx document:
    // -----------------------------------------------------------------------------------------
    // | START DATE | NUMBER OF STUDENTS | IS FREE | COURSE TITLE |    CONTACT     |  STATUS   |
    // -----------------------------------------------------------------------------------------
    // | 03/24/2018 |         10         |   true  |  Chemistry   | (123) 456-7890 | SCHEDULED |
    // -----------------------------------------------------------------------------------------
    
    const schema = {
      'START DATE': {
        // JSON object property name.
        prop: 'date',
        type: Date
      },
      'NUMBER OF STUDENTS': {
        prop: 'numberOfStudents',
        type: Number,
        required: true
      },
      // Nested object example.
      // 'COURSE' here is not a real Excel file column name,
      // it can be any string — it's just for code readability.
      'COURSE': {
        // Nested object path: `row.course`
        prop: 'course',
        // Nested object schema:
        type: {
          'IS FREE': {
            prop: 'isFree',
            type: Boolean
          },
          'COURSE TITLE': {
            prop: 'title',
            type: String
          }
        }
      },
      'CONTACT': {
        prop: 'contact',
        required: true,
        // A custom `type` can be defined.
        // A `type` function only gets called for non-empty cells.
        type: (value) => {
          const number = parsePhoneNumber(value)
          if (!number) {
            throw new Error('invalid')
          }
          return number
        }
      },
      'STATUS': {
        prop: 'status',
        type: String,
        oneOf: [
          'SCHEDULED',
          'STARTED',
          'FINISHED'
        ]
      }
    }
    
    readXlsxFile(file, { schema }).then(({ rows, errors }) => {
      // `errors` list items have shape: `{ row, column, error, reason?, value?, type? }`.
      errors.length === 0
    
      rows === [{
        date: new Date(2018, 2, 24),
        numberOfStudents: 10,
        course: {
          isFree: true,
          title: 'Chemistry'
        },
        contact: '+11234567890',
        status: 'SCHEDULED'
      }]
    })
    8 - Mã lỗi thứ cấp tùy chọn cung cấp thêm chi tiết về lỗi. Hiện tại, nó chỉ được trả lại cho "tích hợp" ____37s. Ví dụ:
    {
      'COLUMN_TITLE': {
        // This function will only be called for a non-empty cell.
        type: (value) => {
          try {
            return parseValue(value)
          } catch (error) {
            console.error(error)
            throw new Error('invalid')
          }
        }
      }
    }
    0 cho
    {
      'COLUMN_TITLE': {
        // This function will only be called for a non-empty cell.
        type: (value) => {
          try {
            return parseValue(value)
          } catch (error) {
            console.error(error)
            throw new Error('invalid')
          }
        }
      }
    }
    1 có nghĩa là "giá trị ô không hợp lệ vì nó không phải là số".
  • {
      'COLUMN_TITLE': {
        // This function will only be called for a non-empty cell.
        type: (value) => {
          try {
            return parseValue(value)
          } catch (error) {
            console.error(error)
            throw new Error('invalid')
          }
        }
      }
    }
    2 - Số hàng trong tệp gốc.
    {
      'COLUMN_TITLE': {
        // This function will only be called for a non-empty cell.
        type: (value) => {
          try {
            return parseValue(value)
          } catch (error) {
            console.error(error)
            throw new Error('invalid')
          }
        }
      }
    }
    3 có nghĩa là hàng đầu tiên, v.v.
  • {
      'COLUMN_TITLE': {
        // This function will only be called for a non-empty cell.
        type: (value) => {
          try {
            return parseValue(value)
          } catch (error) {
            console.error(error)
            throw new Error('invalid')
          }
        }
      }
    }
    4 - Tiêu đề cột.
  • {
      'COLUMN_TITLE': {
        // This function will only be called for a non-empty cell.
        type: (value) => {
          try {
            return parseValue(value)
          } catch (error) {
            console.error(error)
            throw new Error('invalid')
          }
        }
      }
    }
    5 - Giá trị ô.
  • {
      'COLUMN_TITLE': {
        // This function will only be called for a non-empty cell.
        type: (value) => {
          try {
            return parseValue(value)
          } catch (error) {
            console.error(error)
            throw new Error('invalid')
          }
        }
      }
    }
    6 - lược đồ
    const readXlsxFile = require('read-excel-file/node')
    
    // File path.
    readXlsxFile('/path/to/file').then((rows) => {
      // `rows` is an array of rows
      // each row being an array of cells.
    })
    
    // Readable Stream.
    readXlsxFile(fs.createReadStream('/path/to/file')).then((rows) => {
      // `rows` is an array of rows
      // each row being an array of cells.
    })
    
    // Buffer.
    readXlsxFile(Buffer.from(fs.readFileSync('/path/to/file'))).then((rows) => {
      // `rows` is an array of rows
      // each row being an array of cells.
    })
    7 cho cột này.

Một ví dụ về việc sử dụng
import readXlsxFile from 'read-excel-file'

// File.
const input = document.getElementById('input')
input.addEventListener('change', () => {
  readXlsxFile(input.files[0]).then((rows) => {
    // `rows` is an array of rows
    // each row being an array of cells.
  })
})

// Blob.
fetch('https://example.com/spreadsheet.xlsx')
  .then(response => response.blob())
  .then(blob => readXlsxFile(blob))
  .then((rows) => {
    // `rows` is an array of rows
    // each row being an array of cells.
  })

// ArrayBuffer.
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer
//
// Could be obtained from:
// * File
// * Blob
// * Base64 string
//
readXlsxFile(arrayBuffer).then((rows) => {
  // `rows` is an array of rows
  // each row being an array of cells.
})
6

// An example *.xlsx document:
// -----------------------------------------------------------------------------------------
// | START DATE | NUMBER OF STUDENTS | IS FREE | COURSE TITLE |    CONTACT     |  STATUS   |
// -----------------------------------------------------------------------------------------
// | 03/24/2018 |         10         |   true  |  Chemistry   | (123) 456-7890 | SCHEDULED |
// -----------------------------------------------------------------------------------------

const schema = {
  'START DATE': {
    // JSON object property name.
    prop: 'date',
    type: Date
  },
  'NUMBER OF STUDENTS': {
    prop: 'numberOfStudents',
    type: Number,
    required: true
  },
  // Nested object example.
  // 'COURSE' here is not a real Excel file column name,
  // it can be any string — it's just for code readability.
  'COURSE': {
    // Nested object path: `row.course`
    prop: 'course',
    // Nested object schema:
    type: {
      'IS FREE': {
        prop: 'isFree',
        type: Boolean
      },
      'COURSE TITLE': {
        prop: 'title',
        type: String
      }
    }
  },
  'CONTACT': {
    prop: 'contact',
    required: true,
    // A custom `type` can be defined.
    // A `type` function only gets called for non-empty cells.
    type: (value) => {
      const number = parsePhoneNumber(value)
      if (!number) {
        throw new Error('invalid')
      }
      return number
    }
  },
  'STATUS': {
    prop: 'status',
    type: String,
    oneOf: [
      'SCHEDULED',
      'STARTED',
      'FINISHED'
    ]
  }
}

readXlsxFile(file, { schema }).then(({ rows, errors }) => {
  // `errors` list items have shape: `{ row, column, error, reason?, value?, type? }`.
  errors.length === 0

  rows === [{
    date: new Date(2018, 2, 24),
    numberOfStudents: 10,
    course: {
      isFree: true,
      title: 'Chemistry'
    },
    contact: '+11234567890',
    status: 'SCHEDULED'
  }]
})

Ví dụ tùy chỉnh ____37. example.

{
  'COLUMN_TITLE': {
    // This function will only be called for a non-empty cell.
    type: (value) => {
      try {
        return parseValue(value)
      } catch (error) {
        console.error(error)
        throw new Error('invalid')
      }
    }
  }
}

Bỏ qua các hàng trống..

Theo mặc định, nó bỏ qua bất kỳ hàng trống. Để vô hiệu hóa hành vi đó, hãy vượt qua tùy chọn

readXlsxFile(file, {
  schema,
  ignoreEmptyRows: false
})
0.

readXlsxFile(file, {
  schema,
  ignoreEmptyRows: false
})

Cách sửa dữ liệu bảng tính trước khi phân tích cú pháp
import readXlsxFile from 'read-excel-file'

// File.
const input = document.getElementById('input')
input.addEventListener('change', () => {
  readXlsxFile(input.files[0]).then((rows) => {
    // `rows` is an array of rows
    // each row being an array of cells.
  })
})

// Blob.
fetch('https://example.com/spreadsheet.xlsx')
  .then(response => response.blob())
  .then(blob => readXlsxFile(blob))
  .then((rows) => {
    // `rows` is an array of rows
    // each row being an array of cells.
  })

// ArrayBuffer.
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer
//
// Could be obtained from:
// * File
// * Blob
// * Base64 string
//
readXlsxFile(arrayBuffer).then((rows) => {
  // `rows` is an array of rows
  // each row being an array of cells.
})
6. Ví dụ, làm thế nào để bỏ qua các hàng trống.how to ignore empty rows.

Đôi khi, một bảng tính không chính xác có cấu trúc được yêu cầu bởi tính năng phân tích cú pháp

import readXlsxFile from 'read-excel-file'

// File.
const input = document.getElementById('input')
input.addEventListener('change', () => {
  readXlsxFile(input.files[0]).then((rows) => {
    // `rows` is an array of rows
    // each row being an array of cells.
  })
})

// Blob.
fetch('https://example.com/spreadsheet.xlsx')
  .then(response => response.blob())
  .then(blob => readXlsxFile(blob))
  .then((rows) => {
    // `rows` is an array of rows
    // each row being an array of cells.
  })

// ArrayBuffer.
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer
//
// Could be obtained from:
// * File
// * Blob
// * Base64 string
//
readXlsxFile(arrayBuffer).then((rows) => {
  // `rows` is an array of rows
  // each row being an array of cells.
})
6 của thư viện này: ví dụ, nó có thể bị thiếu một hàng tiêu đề hoặc chứa một số hàng trình bày hoàn toàn / trống / "rác" hoàn toàn nên được gỡ bỏ. Để khắc phục điều đó, người ta có thể vượt qua chức năng
readXlsxFile(file, {
  schema,
  ignoreEmptyRows: false
})
3 tùy chọn sẽ sửa đổi nội dung bảng tính theo yêu cầu.

readXlsxFile(file, {
  schema,
  transformData(data) {
    // Add a missing header row.
    return [['ID', 'NAME', ...]].concat(data)
    // Remove empty rows.
    return data.filter(row => row.filter(column => column !== null).length > 0)
  }
})

Chức năng chuyển đổi dữ liệu thành các đối tượng JSON bằng lược đồ cũng được xuất từ ​​thư viện này, nếu có ai muốn nó.function for converting data to JSON objects using a schema is exported from this library too, if anyone wants it.

<input type="file" id="input" />
0

Một thành phần phản ứng để hiển thị các lỗi xảy ra trong quá trình phân tích/xác thực lược đồ.React component for displaying errors that occured during schema parsing/validation.

<input type="file" id="input" />
1

JSON (Bản đồ)

Tương tự như trên, nhưng đơn giản hơn: không có bất kỳ phân tích cú pháp hoặc xác nhận.

Đôi khi, một nhà phát triển có thể muốn sử dụng một số giải pháp khác (nâng cao hơn) để phân tích cú pháp và xác nhận lược đồ (như

readXlsxFile(file, {
  schema,
  ignoreEmptyRows: false
})
4). Nếu nhà phát triển vượt qua tùy chọn
readXlsxFile(file, {
  schema,
  ignoreEmptyRows: false
})
5 thay vì tùy chọn
import readXlsxFile from 'read-excel-file'

// File.
const input = document.getElementById('input')
input.addEventListener('change', () => {
  readXlsxFile(input.files[0]).then((rows) => {
    // `rows` is an array of rows
    // each row being an array of cells.
  })
})

// Blob.
fetch('https://example.com/spreadsheet.xlsx')
  .then(response => response.blob())
  .then(blob => readXlsxFile(blob))
  .then((rows) => {
    // `rows` is an array of rows
    // each row being an array of cells.
  })

// ArrayBuffer.
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer
//
// Could be obtained from:
// * File
// * Blob
// * Base64 string
//
readXlsxFile(arrayBuffer).then((rows) => {
  // `rows` is an array of rows
  // each row being an array of cells.
})
6 cho
import readXlsxFile from 'read-excel-file'

// File.
const input = document.getElementById('input')
input.addEventListener('change', () => {
  readXlsxFile(input.files[0]).then((rows) => {
    // `rows` is an array of rows
    // each row being an array of cells.
  })
})

// Blob.
fetch('https://example.com/spreadsheet.xlsx')
  .then(response => response.blob())
  .then(blob => readXlsxFile(blob))
  .then((rows) => {
    // `rows` is an array of rows
    // each row being an array of cells.
  })

// ArrayBuffer.
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer
//
// Could be obtained from:
// * File
// * Blob
// * Base64 string
//
readXlsxFile(arrayBuffer).then((rows) => {
  // `rows` is an array of rows
  // each row being an array of cells.
})
7, thì nó sẽ chỉ ánh xạ từng hàng dữ liệu đến đối tượng JSON mà không cần phân tích cú pháp hoặc xác thực. Các giá trị ô sẽ vẫn "như là": như một chuỗi, số, ngày hoặc boolean.

<input type="file" id="input" />
2

Nhiều tờ

Theo mặc định, nó đọc tờ đầu tiên trong tài liệu. Nếu bạn có nhiều tờ trong bảng tính của mình thì hãy chuyển một số trang (bắt đầu từ

{
  'COLUMN_TITLE': {
    // This function will only be called for a non-empty cell.
    type: (value) => {
      try {
        return parseValue(value)
      } catch (error) {
        console.error(error)
        throw new Error('invalid')
      }
    }
  }
}
3) hoặc tên trang tính trong đối số
readXlsxFile(file, {
  schema,
  ignoreEmptyRows: false
})
9.

<input type="file" id="input" />
3

<input type="file" id="input" />
4

Theo mặc định,

readXlsxFile(file, {
  schema,
  transformData(data) {
    // Add a missing header row.
    return [['ID', 'NAME', ...]].concat(data)
    // Remove empty rows.
    return data.filter(row => row.filter(column => column !== null).length > 0)
  }
})
0 là
{
  'COLUMN_TITLE': {
    // This function will only be called for a non-empty cell.
    type: (value) => {
      try {
        return parseValue(value)
      } catch (error) {
        console.error(error)
        throw new Error('invalid')
      }
    }
  }
}
3.

Để có được tên của tất cả các tờ, hãy sử dụng hàm

readXlsxFile(file, {
  schema,
  transformData(data) {
    // Add a missing header row.
    return [['ID', 'NAME', ...]].concat(data)
    // Remove empty rows.
    return data.filter(row => row.filter(column => column !== null).length > 0)
  }
})
2:

<input type="file" id="input" />
5

ngày

Định dạng XLSX ban đầu không có loại "ngày" chuyên dụng, do đó ngày trong hầu hết các trường hợp được lưu trữ đơn giản là số (số ngày kể từ

readXlsxFile(file, {
  schema,
  transformData(data) {
    // Add a missing header row.
    return [['ID', 'NAME', ...]].concat(data)
    // Remove empty rows.
    return data.filter(row => row.filter(column => column !== null).length > 0)
  }
})
3) cùng với mô tả "định dạng" (như
readXlsxFile(file, {
  schema,
  transformData(data) {
    // Add a missing header row.
    return [['ID', 'NAME', ...]].concat(data)
    // Remove empty rows.
    return data.filter(row => row.filter(column => column !== null).length > 0)
  }
})
4) hướng dẫn phần mềm xem bảng tính để định dạng ngày trong ô bằng cách sử dụng định dạng nhất định đó.

Khi sử dụng

readXlsxFile(file, {
  schema,
  transformData(data) {
    // Add a missing header row.
    return [['ID', 'NAME', ...]].concat(data)
    // Remove empty rows.
    return data.filter(row => row.filter(column => column !== null).length > 0)
  }
})
5 với tham số
import readXlsxFile from 'read-excel-file'

// File.
const input = document.getElementById('input')
input.addEventListener('change', () => {
  readXlsxFile(input.files[0]).then((rows) => {
    // `rows` is an array of rows
    // each row being an array of cells.
  })
})

// Blob.
fetch('https://example.com/spreadsheet.xlsx')
  .then(response => response.blob())
  .then(blob => readXlsxFile(blob))
  .then((rows) => {
    // `rows` is an array of rows
    // each row being an array of cells.
  })

// ArrayBuffer.
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer
//
// Could be obtained from:
// * File
// * Blob
// * Base64 string
//
readXlsxFile(arrayBuffer).then((rows) => {
  // `rows` is an array of rows
  // each row being an array of cells.
})
6, tất cả các cột lược đồ có loại
const worker = new Worker('web-worker.js')

worker.onmessage = function(event) {
  // `event.data` is an array of rows
  // each row being an array of cells.
  console.log(event.data)
}

worker.onerror = function(event) {
  console.error(event.message)
}

const input = document.getElementById('input')

input.addEventListener('change', () => {
  worker.postMessage(input.files[0])
})
3 được phân tích điện tử tự động là ngày. Khi sử dụng
readXlsxFile(file, {
  schema,
  transformData(data) {
    // Add a missing header row.
    return [['ID', 'NAME', ...]].concat(data)
    // Remove empty rows.
    return data.filter(row => row.filter(column => column !== null).length > 0)
  }
})
5 mà không có tham số
import readXlsxFile from 'read-excel-file'

// File.
const input = document.getElementById('input')
input.addEventListener('change', () => {
  readXlsxFile(input.files[0]).then((rows) => {
    // `rows` is an array of rows
    // each row being an array of cells.
  })
})

// Blob.
fetch('https://example.com/spreadsheet.xlsx')
  .then(response => response.blob())
  .then(blob => readXlsxFile(blob))
  .then((rows) => {
    // `rows` is an array of rows
    // each row being an array of cells.
  })

// ArrayBuffer.
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer
//
// Could be obtained from:
// * File
// * Blob
// * Base64 string
//
readXlsxFile(arrayBuffer).then((rows) => {
  // `rows` is an array of rows
  // each row being an array of cells.
})
6, thư viện này cố gắng đoán xem một ô có chứa một ngày hay chỉ là một số bằng cách kiểm tra "định dạng" của ô-nếu "định dạng" là một trong các định dạng ngày tích hợp thì các giá trị của các ô như vậy được tự động phân tích cú pháp là ngày. Trong các trường hợp khác, khi các ô ngày sử dụng định dạng không được xây dựng (như
<input type="file" id="input" />
00), người ta có thể truyền tham số
<input type="file" id="input" />
01 rõ ràng để hướng dẫn thư viện phân tích các ô số có "định dạng" như ngày:

<input type="file" id="input" />
6

Cắt

Theo mặc định, nó tự động cắt tất cả các giá trị chuỗi. Để tắt tính năng này, hãy vượt qua tùy chọn

<input type="file" id="input" />
02.

<input type="file" id="input" />
7

Biến đổi

Đôi khi, một bảng tính không chính xác có cấu trúc được yêu cầu bởi tính năng phân tích cú pháp

import readXlsxFile from 'read-excel-file'

// File.
const input = document.getElementById('input')
input.addEventListener('change', () => {
  readXlsxFile(input.files[0]).then((rows) => {
    // `rows` is an array of rows
    // each row being an array of cells.
  })
})

// Blob.
fetch('https://example.com/spreadsheet.xlsx')
  .then(response => response.blob())
  .then(blob => readXlsxFile(blob))
  .then((rows) => {
    // `rows` is an array of rows
    // each row being an array of cells.
  })

// ArrayBuffer.
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer
//
// Could be obtained from:
// * File
// * Blob
// * Base64 string
//
readXlsxFile(arrayBuffer).then((rows) => {
  // `rows` is an array of rows
  // each row being an array of cells.
})
6 của thư viện này: ví dụ, nó có thể bị thiếu một hàng tiêu đề hoặc chứa một số hàng trình bày hoàn toàn / trống / "rác" hoàn toàn nên được gỡ bỏ. Để khắc phục điều đó, người ta có thể vượt qua chức năng
readXlsxFile(file, {
  schema,
  ignoreEmptyRows: false
})
3 tùy chọn sẽ sửa đổi nội dung bảng tính theo yêu cầu.

readXlsxFile(file, {
  schema,
  transformData(data) {
    // Add a missing header row.
    return [['ID', 'NAME', ...]].concat(data)
    // Remove empty rows.
    return data.filter(row => row.filter(column => column !== null).length > 0)
  }
})

Giới hạn

Màn biểu diễn

Đã có một số báo cáo về các vấn đề về hiệu suất khi đọc bảng tính

import readXlsxFile from 'read-excel-file'

// File.
const input = document.getElementById('input')
input.addEventListener('change', () => {
  readXlsxFile(input.files[0]).then((rows) => {
    // `rows` is an array of rows
    // each row being an array of cells.
  })
})

// Blob.
fetch('https://example.com/spreadsheet.xlsx')
  .then(response => response.blob())
  .then(blob => readXlsxFile(blob))
  .then((rows) => {
    // `rows` is an array of rows
    // each row being an array of cells.
  })

// ArrayBuffer.
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer
//
// Could be obtained from:
// * File
// * Blob
// * Base64 string
//
readXlsxFile(arrayBuffer).then((rows) => {
  // `rows` is an array of rows
  // each row being an array of cells.
})
1 rất lớn bằng thư viện này. Đúng là điểm chính của thư viện này là khả năng sử dụng và thuận tiện, và không hiệu suất khi xử lý các bộ dữ liệu lớn. Ví dụ: thời gian phân tích một tệp có 2000 hàng / 20 cột là khoảng 3 giây. Vì vậy, để đọc các bộ dữ liệu khổng lồ, có lẽ sử dụng một cái gì đó như gói
<input type="file" id="input" />
06 thay thế. Không có điểm chuẩn so sánh giữa hai người, vì vậy nếu bạn sẽ tạo một điểm, hãy chia sẻ nó trong các vấn đề.

Công thức

Các tế bào được tính toán động bằng cách sử dụng các công thức (

<input type="file" id="input" />
07, v.v.) không được hỗ trợ.

TYPEXTRIPT

Tôi không phải là một chuyên gia bản thảo, vì vậy cộng đồng phải viết các kiểu đánh máy (và kiểm tra chúng). Xem ví dụ

<input type="file" id="input" />
08.

CDN

Người ta có thể sử dụng bất kỳ dịch vụ NPM CDN nào, ví dụ: unpkg.com hoặc jsdelivr.net

<input type="file" id="input" />
9

TYPEXTRIPT

Tôi không phải là một chuyên gia bản thảo, vì vậy cộng đồng phải viết các kiểu đánh máy (và kiểm tra chúng). Xem ví dụ

<input type="file" id="input" />
08.

CDN

Người ta có thể sử dụng bất kỳ dịch vụ NPM CDN nào, ví dụ: unpkg.com hoặc jsdelivr.net

Thư viện này đi kèm với "Typings" TypeScript. Nếu bạn tình cờ tìm thấy bất kỳ lỗi nào trong đó, hãy tạo ra một vấn đề.

Người giới thiệu

Sử dụng 09 để phân tích cú pháp XML.

GitHub