简体   繁体   English

SheetJs,反应。 我正在阅读一个巨大的 XLSX 文件,但不是从第 0 行开始,我想从最后一行开始

[英]SheetJs, React. I'm reading a huge XLSX file, but instead of starting at row 0 I want to start from the last row

I've been tasked with importing and parsing a huge XLSX file and displaying as a dashboard (I'm aware it would be a task for the backend, but I need to do it on frontend).我的任务是导入和解析一个巨大的 XLSX 文件并显示为仪表板(我知道这将是后端的任务,但我需要在前端完成)。

I need the last 100 rows, can anyone help me out?我需要最后 100 行,有人可以帮我吗? Here's my code so far:到目前为止,这是我的代码:

  const handleFile = async(e) => {

const file = e.target.files[0];
const data = await file.arrayBuffer();
const workbook = XLSX.readFile(data, {sheetRows: 100});

const worksheet = workbook.Sheets[workbook.SheetNames[0]];
const jsonData = XLSX.utils.sheet_to_json(worksheet, { raw: false, range: 1048476});

setChartData(jsonData)

} }

I've tried the Range parameter, but it brings me more than one objects and crashes my dashboards.我已经尝试过 Range 参数,但它给我带来了不止一个对象并使我的仪表板崩溃。

The one issue with range is that you need to set the header column, otherwise it's going to interpret your first row as the header.范围的一个问题是您需要设置标题列,否则它将把您的第一行解释为标题。 You can do that by just reading the first two rows and getting the keys from that.您可以通过读取前两行并从中获取密钥来做到这一点。 Then, you can get the number of rows by using XLSX.utils.decode_range(worksheet['!ref'])然后,您可以使用XLSX.utils.decode_range(worksheet['!ref'])

Altogether, it should look like something like:总而言之,它应该类似于:

 var csv = "a,b,c\n1,2,3\n4,5,6\n7,8,9\n10,11,12\n13,14,15\n16,17,18\n19,20,21\n22,23,24\n25,26,27\n28,29,30\n1,2,3\n4,5,6\n7,8,9\n10,11,12\n13,14,15\n16,17,18\n19,20,21\n22,23,24\n25,26,27\n67,68,69\n70,71,72\n73,74,75\n76,77,78\n79,80,81\n82,83,84\n85,86,87\n88,89,90\n91,92,93\n94,95,96\n97,98,99" var wb = XLSX.read(csv, {type:"binary"}); const worksheet = wb.Sheets[wb.SheetNames[0]]; var range = XLSX.utils.decode_range(worksheet['!ref']); var numRows = range.er - range.sr + 1 const header = Object.keys(XLSX.utils.sheet_to_json(wb.Sheets.Sheet1, {raw:false, range: `A1:ZZ2`})[0]); const numCols = header.length - 1; const lastCol = String.fromCharCode('A'.charCodeAt(0) + numCols); var data = XLSX.utils.sheet_to_json(wb.Sheets.Sheet1, {raw:false, defval:null, range: numRows-10, header}); console.log(data)
 <script src="https://unpkg.com/xlsx@0.18.5/dist/xlsx.full.min.js"></script> <pre id="out"></pre>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SheetJs 使用节点读取 xlsx 文件 - SheetJs reading a xlsx file with node 反应 从表中删除特定行 - React. Delete specific row from the table 我正在尝试在 React 中制作折叠/手风琴。 但问题是,它不会触发我想要切换的特定内容 - I'm trying to make a collapse/accordion in React. But the problem is, it is not triggering the specific content that i want to toggle 将 React 与 Bootstrap 一起使用,我想开始一行拉四个数据集,将它们放入 col 然后用下一个 4 开始一个新行 - Using React with Bootstrap and I want to start a row pull four data sets, put them into col then start a new row with the next 4 如何在没有 React 的情况下从 JSX 获取 HTML 字符串。 例如,我正在使用 SolidJS - How do you get an HTML string from JSX without React. I'm using SolidJS for example 如何将数组复制到从某个单元格开始的列的每个单元格,而不是复制到行的每个单元格? - How can I copy an array to each cell of a column starting from a certain cell, instead of to each cell of a row? SheetJS:将行值从数组转置到 object - SheetJS: transpose row values from array to object 我想从表中获取行 ID - I want to get row Id from Table Append 到从特定列开始的最后一行 - Append to the last row starting from a specific column 我想从同一个 position 开始文本,并且连续右对齐 - I want to start text from same position and also right aligned in a row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM