升龙决 發表於 2020-4-28 14:53:00

JavaScript读取Excel文件

<p>作者:不老猫</p>
<p>日期:2020年04月28日</p>
<p>(如果文章确实地帮助你解决了问题,请点个推荐,让我开心一下,助人为乐,嘿嘿(*^▽^*))</p>
<p>前端时间想做一个读取EXCEL的相关应用,只想在单页面上使用,一直没有找到可以用的。后来在一个之前的前端页面框架中找到了,然后截出来用,特此做个笔记。</p>
<h3 id="h3-u5BFCu5165u5E93">导入库</h3>
<p>首先需要一个套件,这个套件叫sheetJS,需要导入一个xlsx库,不过我npm不成功,我找到一个http直连。在html页面中添加如下代码:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)"> 引入xlsx库 </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">script </span><span style="color: rgba(255, 0, 0, 1)">type</span><span style="color: rgba(0, 0, 255, 1)">="text/javascript"</span><span style="color: rgba(255, 0, 0, 1)"> src</span><span style="color: rgba(0, 0, 255, 1)">="https://unpkg.com/xlsx/dist/xlsx.full.min.js"</span><span style="color: rgba(0, 0, 255, 1)">&gt;&lt;/</span><span style="color: rgba(128, 0, 0, 1)">script</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span></pre>
</div>
<h3>页面组件</h3>
<p>1、在页面中需要添加一个input组件,以从电脑中获取文件。</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">input </span><span style="color: rgba(255, 0, 0, 1)">type</span><span style="color: rgba(0, 0, 255, 1)">="file"</span><span style="color: rgba(255, 0, 0, 1)"> accept</span><span style="color: rgba(0, 0, 255, 1)">=".xlsx, .xls"</span><span style="color: rgba(255, 0, 0, 1)">@change</span><span style="color: rgba(0, 0, 255, 1)">="Excelinput_handle_change"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span></pre>
</div>
<p>change 事件就是导入文件之后触发的事件。</p>
<p>2、事件处理</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 选择文件完毕</span>
<span style="color: rgba(0, 128, 128, 1)">2</span> <span style="color: rgba(0, 0, 0, 1)">Excelinput_handle_change(e) {
</span><span style="color: rgba(0, 128, 128, 1)">3</span>   const files = e.target.files; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 导入的文件是数组的</span>
<span style="color: rgba(0, 128, 128, 1)">4</span>   const file = files; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> only use files</span>
<span style="color: rgba(0, 128, 128, 1)">5</span>   <span style="color: rgba(0, 0, 255, 1)">if</span> (!file) <span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">6</span>
<span style="color: rgba(0, 128, 128, 1)">7</span>   console.log('导入的文件:' +<span style="color: rgba(0, 0, 0, 1)"> file.name);
</span><span style="color: rgba(0, 128, 128, 1)">8</span>   <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.Excelfile_reader_file(file);
</span><span style="color: rgba(0, 128, 128, 1)">9</span> }</pre>
</div>
<h3 id="-">读取文件</h3>
<p>读取文件是异步的。直接上代码:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)"> 1</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 读取文件,是个异步的</span>
<span style="color: rgba(0, 128, 128, 1)"> 2</span> <span style="color: rgba(0, 0, 0, 1)">Excelfile_reader_file(rawFile) {
</span><span style="color: rgba(0, 128, 128, 1)"> 3</span>   <span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">new</span> Promise((resolve, reject) =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 128, 128, 1)"> 4</span>               const reader = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> FileReader();
</span><span style="color: rgba(0, 128, 128, 1)"> 5</span>               reader.onload = e =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 128, 128, 1)"> 6</span>                     const data =<span style="color: rgba(0, 0, 0, 1)"> e.target.result
</span><span style="color: rgba(0, 128, 128, 1)"> 7</span>                     const workbook = XLSX.read(data, { type: 'array'<span style="color: rgba(0, 0, 0, 1)"> });
</span><span style="color: rgba(0, 128, 128, 1)"> 8</span>                     <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> console.log('读取出的源表格数据');</span>
<span style="color: rgba(0, 128, 128, 1)"> 9</span>                     <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> console.log(workbook);</span>
<span style="color: rgba(0, 128, 128, 1)">10</span>                     const firstSheetName = workbook.SheetNames;
</span><span style="color: rgba(0, 128, 128, 1)">11</span>                     const worksheet =<span style="color: rgba(0, 0, 0, 1)"> workbook.Sheets;
</span><span style="color: rgba(0, 128, 128, 1)">12</span>                     const header = <span style="color: rgba(0, 0, 255, 1)">this</span>.Excelfile_get_header_row(worksheet);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 第一行为标题栏</span>
<span style="color: rgba(0, 128, 128, 1)">13</span>                     const results =<span style="color: rgba(0, 0, 0, 1)"> XLSX.utils.sheet_to_json(worksheet);
</span><span style="color: rgba(0, 128, 128, 1)">14</span>                     <span style="color: rgba(0, 0, 255, 1)">this</span>.file_excel.file_data.header =<span style="color: rgba(0, 0, 0, 1)"> header;
</span><span style="color: rgba(0, 128, 128, 1)">15</span>                     <span style="color: rgba(0, 0, 255, 1)">this</span>.file_excel.file_data.results =<span style="color: rgba(0, 0, 0, 1)"> results;
</span><span style="color: rgba(0, 128, 128, 1)">16</span>                     <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> console.log('读取出的数据');</span>
<span style="color: rgba(0, 128, 128, 1)">17</span>                     <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> console.log(this.file_excel.file_data);</span>
<span style="color: rgba(0, 128, 128, 1)">18</span> <span style="color: rgba(0, 0, 0, 1)">                  resolve();
</span><span style="color: rgba(0, 128, 128, 1)">19</span> <span style="color: rgba(0, 0, 0, 1)">                }
</span><span style="color: rgba(0, 128, 128, 1)">20</span> <span style="color: rgba(0, 0, 0, 1)">                reader.readAsArrayBuffer(rawFile);
</span><span style="color: rgba(0, 128, 128, 1)">21</span> <span style="color: rgba(0, 0, 0, 1)">            })
</span><span style="color: rgba(0, 128, 128, 1)">22</span> },</pre>
</div>
<p>这是标题转化的函数,直接粘贴使用即可:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)"> 1</span> <span style="color: rgba(0, 0, 0, 1)">Excelfile_get_header_row(sheet) {
</span><span style="color: rgba(0, 128, 128, 1)"> 2</span>   const headers =<span style="color: rgba(0, 0, 0, 1)"> [];
</span><span style="color: rgba(0, 128, 128, 1)"> 3</span>   const range = XLSX.utils.decode_range(sheet['!ref'<span style="color: rgba(0, 0, 0, 1)">])
</span><span style="color: rgba(0, 128, 128, 1)"> 4</span> <span style="color: rgba(0, 0, 0, 1)">    let C;
</span><span style="color: rgba(0, 128, 128, 1)"> 5</span>   const R =<span style="color: rgba(0, 0, 0, 1)"> range.s.r;
</span><span style="color: rgba(0, 128, 128, 1)"> 6</span>   <span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> start in the first row </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 128, 128, 1)"> 7</span>   <span style="color: rgba(0, 0, 255, 1)">for</span> (C = range.s.c; C &lt;= range.e.c; ++C) { <span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> walk every column in the range </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 128, 128, 1)"> 8</span>         const cell =<span style="color: rgba(0, 0, 0, 1)"> sheet[XLSX.utils.encode_cell({
</span><span style="color: rgba(0, 128, 128, 1)"> 9</span> <span style="color: rgba(0, 0, 0, 1)">            c: C,
</span><span style="color: rgba(0, 128, 128, 1)">10</span> <span style="color: rgba(0, 0, 0, 1)">            r: R
</span><span style="color: rgba(0, 128, 128, 1)">11</span> <span style="color: rgba(0, 0, 0, 1)">      })]
</span><span style="color: rgba(0, 128, 128, 1)">12</span>         <span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> find the cell in the first row </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 128, 128, 1)">13</span>         let hdr = 'UNKNOWN ' + C <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> &lt;-- replace with your desired default</span>
<span style="color: rgba(0, 128, 128, 1)">14</span>         <span style="color: rgba(0, 0, 255, 1)">if</span> (cell &amp;&amp; cell.t) hdr =<span style="color: rgba(0, 0, 0, 1)"> XLSX.utils.format_cell(cell)
</span><span style="color: rgba(0, 128, 128, 1)">15</span> <span style="color: rgba(0, 0, 0, 1)">      headers.push(hdr);
</span><span style="color: rgba(0, 128, 128, 1)">16</span> <span style="color: rgba(0, 0, 0, 1)">    }
</span><span style="color: rgba(0, 128, 128, 1)">17</span>   <span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> headers;
</span><span style="color: rgba(0, 128, 128, 1)">18</span> }</pre>
</div>
<h2 id="-">这些足够了,其余没了……</h2><br><br>
来源:https://www.cnblogs.com/Romanticcat-Lin/p/12794280.html
頁: [1]
查看完整版本: JavaScript读取Excel文件