Loại tệp excel javascript

Khi bạn mở tệp trong Excel, bạn nhận được thông báo rằng phần mở rộng tên tệp không khớp với định dạng tệp thực tế. Ví dụ, tập tin có một. xlsx nhưng nó thực sự là một tệp văn bản và phải có phần mở rộng. phần mở rộng tên tệp txt

Trước khi bạn bấm có để mở tệp, bạn có thể muốn xem xét lý do tại sao tệp có thể có phần mở rộng tên tệp không chính xác

Các tập tin có thể đã được

  • Vô tình đổi tên    Nếu bạn hoặc ai đó mà bạn biết vô tình đổi tên tệp bằng phần mở rộng tên tệp không chính xác, bạn có thể mở tệp một cách an toàn. Sau đó, lưu tệp ở định dạng tệp có phần mở rộng tên tệp thích hợp

  • Đã đổi tên có chủ ý    Nếu bạn hoặc ai đó mà bạn biết cố tình đổi tên tệp bằng một phần mở rộng khác để đảm bảo rằng Excel sẽ nhận ra phần mở rộng tên tệp, thì bạn có thể mở tệp. Tuy nhiên, cách tốt nhất là bạn nên lưu tệp với phần mở rộng tên tệp phản ánh định dạng tệp thực của nó

Tuy nhiên, ở bước đầu tiên, điều quan trọng là phải quyết định xem bạn có tin cậy nguồn của tệp hay không. Nếu bạn không thể xác minh rằng người tạo tệp là một nguồn đáng tin cậy, thì bạn không nên mở tệp. Tin tặc (người dùng ác ý) có thể đã đổi tên tệp với mục đích đánh lừa bạn mở tệp trong Excel

Sự cố với phần mở rộng tên tệp có thể xảy ra khi sổ làm việc mà bạn đang cố mở không thể đọc được và nội dung không còn giống với định dạng tệp dự kiến. Nếu sổ làm việc bị hỏng, bạn có thể thử sửa sổ làm việc đó trước khi mở. Để biết thông tin về cách khôi phục sổ làm việc bị hỏng, hãy xem Sửa chữa sổ làm việc bị hỏng

Nếu bạn thấy thông báo này khi cố gắng mở một sổ làm việc mà một nguồn đáng tin cậy đã gửi cho bạn trong email, thì bạn không thể mở sổ làm việc đó từ chương trình email của mình. Yêu cầu người đã gửi nó cho bạn gửi lại sổ làm việc trong Sổ làm việc nhị phân Excel (*. xlsb) hoặc Sổ làm việc Excel 97-2003 (*. xls) định dạng tệp. Ngoài ra, bạn có thể lưu sổ làm việc đính kèm trên máy tính của mình, rồi mở sổ làm việc đó trong Excel

Để đọ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 chuyể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ả về một mảng các hàng ô, nó sẽ trả về một đối tượng có 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 mảng 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 gồm

Mỗi thuộc tính của một đối tượng JSON phải được mô tả bằng một "mục nhập" 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. Khóa của mục nhập 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) 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) Chức năng xác thực giá trị ô. Chỉ được gọi trên các ô không trống. Nếu giá trị ô không hợp lệ, nó sẽ báo 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ề "nguyên trạng". dưới dạng chuỗi, số, ngày hoặc boolean. Một
    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à một
    • 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
      • Hàm nhận giá trị ô và trả về giá trị được phân tích cú pháp. Nếu giá trị không hợp lệ, nó sẽ báo lỗi với thông báo lỗi được đặt thành mã lỗi

phụ lục. Theo mặc định, khi chuyển đổi giá trị ô thành thuộc tính đối tượng, nó sẽ 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à vì sự đơn giản. Tuy nhiên, trong một số trường hợp cạnh, có thể 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 cách sử dụng thư viện ORM Sequelize yêu cầu một thuộc tính rõ ràng là
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 để xóa nó trong một 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 chuyển 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 đố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 một 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ó đưa ra một 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'
        }]
      })
      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 với 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 một 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ó đưa ra một 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'
        }]
      })
      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 với 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 phụ 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
    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.
    })
    7s "tích hợp sẵn". Thí 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 một
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ụ 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.
})
7 tùy chỉnh

{
  '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 mọi 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ụ: cách bỏ qua các hàng trống

Đôi khi, một bảng tính không có cấu trúc chính xác theo yêu cầu của 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ể thiếu hàng tiêu đề hoặc chứa một số hàng hoàn toàn mang tính trình bày/trống/"rác" cần được xóa. Để khắc phục điều đó, người ta có thể chuyển một hàm
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

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

Một thành phần React để hiển thị các lỗi xảy ra trong quá trình phân tích/xác thực lược đồ

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

JSON (ánh xạ)

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

Đôi khi, 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 thực lược đồ (như

readXlsxFile(file, {
  schema,
  ignoreEmptyRows: false
})
4). Nếu nhà phát triển chuyển 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ì nhà phát triển đó sẽ chỉ ánh xạ từng hàng dữ liệu tới đối tượng JSON mà không thực hiện bất kỳ phân tích cú pháp hoặc xác thực nào. Giá trị ô sẽ vẫn "như hiện trạng". dưới dạng chuỗi, số, ngày hoặc boolean

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

Nhiều trang tính

Theo mặc định, nó đọc trang đầu tiên trong tài liệu. Nếu bạn có nhiều trang tính trong bảng tính của mình thì hãy chuyển số trang tính (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

Để lấy tên của tất cả các trang tính, 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 đó, trong hầu hết các trường hợp, ngày được lưu trữ đơn giản dưới dạng số (số ngày kể từ ngày

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ả (chẳng hạn 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 định dạng

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 sẽ tự động được phân tích thành ngày tháng. 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 sẽ cố gắng đoán xem một ô có chứa ngày hay chỉ 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 sẵn thì các giá trị của ô đó . Trong các trường hợp khác, khi các ô ngày tháng sử dụng định dạng không tích hợp sẵn (chẳng hạn như
<input type="file" id="input" />
00), người ta có thể chuyển một 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 tháng

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

cắt tỉa

Theo mặc định, nó sẽ 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 có cấu trúc chính xác theo yêu cầu của 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ể thiếu hàng tiêu đề hoặc chứa một số hàng hoàn toàn mang tính trình bày/trống/"rác" cần được xóa. Để khắc phục điều đó, người ta có thể chuyển một hàm
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)
  }
})

Hạn chế

Hiệu suất

Đã có một số 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à sự tiện lợi chứ không phải hiệu suất khi xử lý các bộ dữ liệu khổng lồ. Ví dụ: thời gian phân tích 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ẽ hãy sử dụng một cái gì đó như gói thay thế. Không có tiêu chuẩn so sánh nào giữa hai tiêu chuẩn này, vì vậy nếu bạn định tạo một tiêu chuẩn, hãy chia sẻ tiêu chuẩn đó trong phần Vấn đề

công thức

Các ô được tính toán động sử dụng công thức (_______62_______07, v.v.) không được hỗ trợ

bản đánh máy

Tôi không phải là chuyên gia về TypeScript, vì vậy cộng đồng phải viết các kiểu chữ (và kiểm tra chúng). Thấy

CDN

Người ta có thể sử dụng bất kỳ dịch vụ CDN npm nào, e. g. giải nén. com hoặc jsdelivr. net

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

bản đánh máy

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

Người giới thiệu

Sử dụng

<input type="file" id="input" />
09 để phân tích cú pháp XML

GitHub

Vào ngày 9 tháng 3 năm 2020, GitHub, Inc. âm thầm cấm tài khoản của tôi (xóa tất cả các kho lưu trữ, các vấn đề và nhận xét của tôi, ngay cả trong các kho lưu trữ riêng tư của chủ nhân của tôi) mà không có bất kỳ thông báo hay lời giải thích nào. Do đó, tất cả các mã nguồn phải được chuyển ngay sang GitLab. Repo GitHub hiện chỉ được sử dụng làm bản sao lưu (bạn cũng có thể gắn dấu sao cho repo ở đó) và repo chính hiện là GitLab. Các vấn đề có thể được báo cáo trong bất kỳ repo nào

Làm cách nào để kiểm tra loại tệp trong JavaScript?

Sử dụng JavaScript, bạn có thể dễ dàng kiểm tra phần mở rộng tệp đã chọn với các phần mở rộng tệp được phép và có thể hạn chế người dùng chỉ tải lên các loại tệp được phép. Đối với điều này, chúng tôi sẽ sử dụng hàm fileValidation() . Chúng tôi sẽ tạo hàm fileValidation() chứa mã xác thực loại tệp hoàn chỉnh.

Làm cách nào để kiểm tra định dạng tệp CSV trong JavaScript?

Tệp CSV được chọn trong điều khiển FileUpload (Đầu vào tệp HTML) và nút Tải lên được nhấp và chức năng Tải lên JavaScript được gọi. Bên trong hàm, trước tiên, kiểm tra được thực hiện để xác minh xem tệp là tệp CSV hay tệp văn bản hợp lệ

Tệp CSV là gì?

Tệp CSV (các giá trị được phân tách bằng dấu phẩy) là tệp văn bản có định dạng cụ thể cho phép lưu dữ liệu ở định dạng có cấu trúc bảng.

Ví dụ về tệp CSV là gì?

Tệp CSV là danh sách dữ liệu được phân tách bằng dấu phẩy. Ví dụ, nó có thể trông giống như sau. Tên, email, số điện thoại, địa chỉ . Ví dụ,ví dụ@ví dụ. com,555-555-5555,Địa chỉ ví dụ .