合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
# vue封装html转成excel ``` <pre class="calibre15">``` <span class="token2">let</span> dateFormat \<span class="token">=</span> <span class="token2">function</span> <span class="token1">(</span>timer<span class="token1">)</span> <span class="token1">{</span> <span class="token2">const</span> dt \<span class="token">=</span> <span class="token2">new</span> <span class="token3">Date</span><span class="token1">(</span>timer<span class="token1">)</span> <span class="token2">const</span> y \<span class="token">=</span> dt<span class="token1">.</span><span class="token3">getFullYear</span><span class="token1">(</span><span class="token1">)</span> <span class="token2">const</span> m \<span class="token">=</span> <span class="token1">(</span>dt<span class="token1">.</span><span class="token3">getMonth</span><span class="token1">(</span><span class="token1">)</span> <span class="token">+</span> <span class="token5">1</span> <span class="token">+</span> <span class="token4">''</span><span class="token1">)</span><span class="token1">.</span><span class="token3">padStart</span><span class="token1">(</span><span class="token5">2</span><span class="token1">,</span> <span class="token4">'0'</span><span class="token1">)</span> <span class="token2">const</span> d \<span class="token">=</span> <span class="token1">(</span>dt<span class="token1">.</span><span class="token3">getDate</span><span class="token1">(</span><span class="token1">)</span> <span class="token">+</span> <span class="token4">''</span><span class="token1">)</span><span class="token1">.</span><span class="token3">padStart</span><span class="token1">(</span><span class="token5">2</span><span class="token1">,</span> <span class="token4">'0'</span><span class="token1">)</span> <span class="token2">const</span> hh \<span class="token">=</span> <span class="token1">(</span>dt<span class="token1">.</span><span class="token3">getHours</span><span class="token1">(</span><span class="token1">)</span> <span class="token">+</span> <span class="token4">''</span><span class="token1">)</span><span class="token1">.</span><span class="token3">padStart</span><span class="token1">(</span><span class="token5">2</span><span class="token1">,</span> <span class="token4">'0'</span><span class="token1">)</span> <span class="token2">const</span> mm \<span class="token">=</span> <span class="token1">(</span>dt<span class="token1">.</span><span class="token3">getMinutes</span><span class="token1">(</span><span class="token1">)</span> <span class="token">+</span> <span class="token4">''</span><span class="token1">)</span><span class="token1">.</span><span class="token3">padStart</span><span class="token1">(</span><span class="token5">2</span><span class="token1">,</span> <span class="token4">'0'</span><span class="token1">)</span> <span class="token2">const</span> ss \<span class="token">=</span> <span class="token1">(</span>dt<span class="token1">.</span><span class="token3">getSeconds</span><span class="token1">(</span><span class="token1">)</span> <span class="token">+</span> <span class="token4">''</span><span class="token1">)</span><span class="token1">.</span><span class="token3">padStart</span><span class="token1">(</span><span class="token5">2</span><span class="token1">,</span> <span class="token4">'0'</span><span class="token1">)</span> <span class="token2">return</span> <span><span class="token4">`</span><span><span class="token1">${</span>y<span class="token1">}</span></span><span class="token4">\-</span><span><span class="token1">${</span>m<span class="token1">}</span></span><span class="token4">\-</span><span><span class="token1">${</span>d<span class="token1">}</span></span><span class="token4"> </span><span><span class="token1">${</span>hh<span class="token1">}</span></span><span class="token4">:</span><span><span class="token1">${</span>mm<span class="token1">}</span></span><span class="token4">:</span><span><span class="token1">${</span>ss<span class="token1">}</span></span><span class="token4">`</span></span> <span class="token1">}</span> <span class="token2">let</span> html\_excel \<span class="token">=</span> <span class="token2">function</span> <span class="token1">(</span>tableData<span class="token1">,</span> fileName<span class="token1">,</span> callback<span class="token1">)</span> <span class="token1">{</span> <span class="token2">let</span> tableInnerHTML \<span class="token">=</span> <span class="token4">''</span> <span class="token2">let</span> headerData \<span class="token">=</span> \<span class="token1">[</span><span class="token4">'部门'</span><span class="token1">,</span> <span class="token4">'名称'</span><span class="token1">,</span> <span class="token4">'编号'</span><span class="token1">,</span> <span class="token4">'等级'</span><span class="token1">,</span> <span class="token4">'货位'</span><span class="token1">,</span> <span class="token4">'固定资产'</span>\<span class="token1">]</span> <span class="token2">let</span> bodyData \<span class="token">=</span> tableData <span class="token6">//表格数据</span> tableInnerHTML <span class="token">+=</span> <span class="token4">''</span><span class="token1">;</span> <span class="token6">//头部部分开始拼接!</span> tableInnerHTML <span class="token">+=</span> <span><span class="token4">``</span></span> <span class="token">+</span> <span class="token4">"程序员的将来"</span> <span class="token">+</span> <span class="token4">""</span> tableInnerHTML <span class="token">+=</span> <span class="token4">''</span> headerData<span class="token1">.</span><span class="token3">forEach</span><span class="token1">(</span>item \<span class="token">=></span> <span class="token1">{</span> tableInnerHTML <span class="token">+=</span> <span class="token4">""</span> <span class="token">+</span> item <span class="token">+</span> <span class="token4">""</span> <span class="token1">}</span><span class="token1">)</span> tableInnerHTML <span class="token">+=</span> <span class="token4">''</span><span class="token1">;</span> <span class="token6">//头部部分结束</span> tableInnerHTML <span class="token">+=</span> <span class="token4">''</span> <span class="token6">//身体部分开始</span> bodyData<span class="token1">.</span><span class="token3">forEach</span><span class="token1">(</span>item \<span class="token">=></span> <span class="token1">{</span> tableInnerHTML <span class="token">+=</span> <span class="token4">""</span> tableInnerHTML <span class="token">+=</span> <span class="token4">""</span> <span class="token">+</span> item<span class="token1">.</span>id <span class="token">+</span> <span class="token4">""</span> tableInnerHTML <span class="token">+=</span> <span class="token4">""</span> <span class="token">+</span> item<span class="token1">.</span>name <span class="token">+</span> <span class="token4">""</span> tableInnerHTML <span class="token">+=</span> <span class="token4">""</span> <span class="token">+</span> item<span class="token1">.</span>province <span class="token">+</span> <span class="token4">""</span> tableInnerHTML <span class="token">+=</span> <span class="token4">""</span> <span class="token">+</span> item<span class="token1">.</span>num <span class="token">+</span> <span class="token4">""</span> tableInnerHTML <span class="token">+=</span> <span class="token4">""</span> <span class="token">+</span> item<span class="token1">.</span>level <span class="token">+</span> <span class="token4">""</span> tableInnerHTML <span class="token">+=</span> <span class="token4">""</span> <span class="token">+</span> item<span class="token1">.</span>huowei <span class="token">+</span> <span class="token4">""</span> tableInnerHTML <span class="token">+=</span> <span class="token4">""</span> <span class="token">+</span> item<span class="token1">.</span>status <span class="token">+</span> <span class="token4">""</span> tableInnerHTML <span class="token">+=</span> <span class="token4">""</span> <span class="token1">}</span><span class="token1">)</span> tableInnerHTML <span class="token">+=</span> <span class="token4">''</span><span class="token1">;</span> <span class="token6">//身体结束</span> <span class="token2">function</span> <span class="token3">getExplorer</span><span class="token1">(</span><span class="token1">)</span> <span class="token1">{</span> <span class="token2">var</span> explorer \<span class="token">=</span> window<span class="token1">.</span>navigator<span class="token1">.</span>userAgent<span class="token1">;</span> <span class="token2">if</span> <span class="token1">(</span>explorer<span class="token1">.</span><span class="token3">indexOf</span><span class="token1">(</span><span class="token4">'MSIE'</span><span class="token1">)</span> \<span class="token">>=</span> <span class="token5">0</span><span class="token1">)</span> <span class="token1">{</span> <span class="token2">return</span> <span class="token4">'ie'</span><span class="token1">;</span> <span class="token6">// ie</span> <span class="token1">}</span> <span class="token2">else</span> <span class="token2">if</span> <span class="token1">(</span>explorer<span class="token1">.</span><span class="token3">indexOf</span><span class="token1">(</span><span class="token4">'Firefox'</span><span class="token1">)</span> \<span class="token">>=</span> <span class="token5">0</span><span class="token1">)</span> <span class="token1">{</span> <span class="token2">return</span> <span class="token4">'Firefox'</span><span class="token1">;</span> <span class="token6">// firefox</span> <span class="token1">}</span> <span class="token2">else</span> <span class="token2">if</span> <span class="token1">(</span>explorer<span class="token1">.</span><span class="token3">indexOf</span><span class="token1">(</span><span class="token4">'Chrome'</span><span class="token1">)</span> \<span class="token">>=</span> <span class="token5">0</span><span class="token1">)</span> <span class="token1">{</span> <span class="token2">return</span> <span class="token4">'Chrome'</span><span class="token1">;</span> <span class="token6">// Chrome</span> <span class="token1">}</span> <span class="token2">else</span> <span class="token2">if</span> <span class="token1">(</span>explorer<span class="token1">.</span><span class="token3">indexOf</span><span class="token1">(</span><span class="token4">'Opera'</span><span class="token1">)</span> \<span class="token">>=</span> <span class="token5">0</span><span class="token1">)</span> <span class="token1">{</span> <span class="token2">return</span> <span class="token4">'Opera'</span><span class="token1">;</span> <span class="token6">// Opera</span> <span class="token1">}</span> <span class="token2">else</span> <span class="token2">if</span> <span class="token1">(</span>explorer<span class="token1">.</span><span class="token3">indexOf</span><span class="token1">(</span><span class="token4">'Safari'</span><span class="token1">)</span> \<span class="token">>=</span> <span class="token5">0</span><span class="token1">)</span> <span class="token1">{</span> <span class="token2">return</span> <span class="token4">'Safari'</span><span class="token1">;</span> <span class="token6">// Safari</span> <span class="token1">}</span><span class="token1">;</span> <span class="token1">}</span><span class="token1">;</span> <span class="token2">if</span> <span class="token1">(</span><span class="token3">getExplorer</span><span class="token1">(</span><span class="token1">)</span> <span class="token">!==</span> <span class="token4">'Safari'</span> <span class="token">&&</span> fileName<span class="token1">.</span><span class="token3">substr</span><span class="token1">(</span>\<span class="token">-</span><span class="token5">1</span><span class="token1">,</span> <span class="token5">4</span><span class="token1">)</span> <span class="token">!==</span> <span class="token4">'.xls'</span><span class="token1">)</span> <span class="token1">{</span> fileName <span class="token">+=</span> <span class="token4">'.xls'</span><span class="token1">;</span> <span class="token1">}</span> <span class="token2">if</span> <span class="token1">(</span><span class="token3">getExplorer</span><span class="token1">(</span><span class="token1">)</span> \<span class="token">===</span> <span class="token4">'ie'</span><span class="token1">)</span> <span class="token1">{</span> <span class="token2">var</span> curTbl \<span class="token">=</span> table<span class="token1">;</span> <span class="token2">var</span> oXL \<span class="token">=</span> <span class="token2">new</span> <span class="token3">ActiveXObject</span><span class="token1">(</span><span class="token4">'Excel.Application'</span><span class="token1">)</span><span class="token1">;</span> <span class="token2">var</span> oWB \<span class="token">=</span> oXL<span class="token1">.</span>Workbooks<span class="token1">.</span><span class="token3">Add</span><span class="token1">(</span><span class="token1">)</span><span class="token1">;</span> <span class="token2">var</span> xlsheet \<span class="token">=</span> oWB<span class="token1">.</span><span class="token3">Worksheets</span><span class="token1">(</span><span class="token5">1</span><span class="token1">)</span><span class="token1">;</span> <span class="token2">var</span> sel \<span class="token">=</span> document<span class="token1">.</span>body<span class="token1">.</span><span class="token3">createTextRange</span><span class="token1">(</span><span class="token1">)</span><span class="token1">;</span> sel<span class="token1">.</span><span class="token3">moveToElementText</span><span class="token1">(</span>curTbl<span class="token1">)</span><span class="token1">;</span> sel<span class="token1">.</span><span class="token3">select</span><span class="token1">(</span><span class="token1">)</span><span class="token1">;</span> sel<span class="token1">.</span><span class="token3">execCommand</span><span class="token1">(</span><span class="token4">'Copy'</span><span class="token1">)</span><span class="token1">;</span> xlsheet<span class="token1">.</span><span class="token3">Paste</span><span class="token1">(</span><span class="token1">)</span><span class="token1">;</span> oXL<span class="token1">.</span>Visible \<span class="token">=</span> <span class="token5">true</span><span class="token1">;</span> <span class="token2">try</span> <span class="token1">{</span> <span class="token2">let</span> fname \<span class="token">=</span> oXL<span class="token1">.</span>Application<span class="token1">.</span><span class="token3">GetSaveAsFilename</span><span class="token1">(</span><span class="token4">'Excel.xls'</span><span class="token1">,</span> <span class="token4">'Excel Spreadsheets (\*.xls), \*.xls'</span><span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> <span class="token2">catch</span> <span class="token1">(</span><span class="token3">e</span><span class="token1">)</span> <span class="token1">{</span> <span class="token3">print</span><span class="token1">(</span><span class="token4">'Nested catch caught '</span> <span class="token">+</span> e<span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> <span class="token2">finally</span> <span class="token1">{</span> oWB<span class="token1">.</span><span class="token3">SaveAs</span><span class="token1">(</span>fname<span class="token1">)</span><span class="token1">;</span> <span class="token6">// oWB.Close(savechanges = false);</span> oXL<span class="token1">.</span><span class="token3">Quit</span><span class="token1">(</span><span class="token1">)</span><span class="token1">;</span> oXL \<span class="token">=</span> <span class="token2">null</span><span class="token1">;</span> idTmr \<span class="token">=</span> <span class="token3">setInterval</span><span class="token1">(</span><span class="token3">Cleanup</span><span class="token1">(</span><span class="token1">)</span><span class="token1">,</span> <span class="token5">1</span><span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> <span class="token1">}</span> <span class="token2">else</span> <span class="token1">{</span> <span class="token3">tableToExcel</span><span class="token1">(</span>tableInnerHTML<span class="token1">,</span> fileName<span class="token1">,</span> callback<span class="token1">)</span><span class="token1">;</span> <span class="token">/</span>\<span class="token">*</span>在这调用下面的一个方法,传入拼接完成 的表格,文件名,回调函数。该方法是干嘛的请往下看\<span class="token">*</span><span class="token">/</span> <span class="token1">}</span> <span class="token1">}</span> <span class="token2">function</span> <span class="token3">Cleanup</span><span class="token1">(</span><span class="token1">)</span> <span class="token1">{</span> window<span class="token1">.</span><span class="token3">clearInterval</span><span class="token1">(</span>idTmr<span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> <span class="token2">let</span> tableToExcel \<span class="token">=</span> <span class="token1">(</span><span class="token2">function</span> <span class="token1">(</span><span class="token1">)</span> <span class="token1">{</span> <span class="token2">let</span> template \<span class="token">=</span> <span class="token4">'{table}'</span><span class="token1">;</span> <span class="token2">let</span> format \<span class="token">=</span> <span class="token2">function</span> <span class="token1">(</span>s<span class="token1">,</span> c<span class="token1">)</span> <span class="token1">{</span> <span class="token2">return</span> s<span class="token1">.</span><span class="token3">replace</span><span class="token1">(</span><span class="token7">/{(\\w+)}/g</span><span class="token1">,</span> <span class="token2">function</span> <span class="token1">(</span>m<span class="token1">,</span> p<span class="token1">)</span> <span class="token1">{</span> <span class="token2">return</span> c\<span class="token1">[</span>p\<span class="token1">]</span><span class="token1">;</span> <span class="token1">}</span><span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span><span class="token1">;</span> <span class="token2">return</span> <span class="token2">function</span> <span class="token1">(</span>table<span class="token1">,</span> name<span class="token1">,</span> callback<span class="token1">)</span> <span class="token1">{</span> <span class="token2">let</span> ctx \<span class="token">=</span> <span class="token1">{</span> worksheet<span class="token1">:</span> name <span class="token">||</span> <span class="token4">'Worksheet'</span><span class="token1">,</span> table<span class="token1">:</span> table <span class="token1">}</span><span class="token1">;</span> <span class="token2">let</span> blob \<span class="token">=</span> <span class="token2">new</span> <span class="token3">Blob</span><span class="token1">(</span>\<span class="token1">[</span><span class="token3">format</span><span class="token1">(</span>template<span class="token1">,</span> ctx<span class="token1">)</span>\<span class="token1">]</span><span class="token1">)</span><span class="token1">;</span> <span class="token2">let</span> a \<span class="token">=</span> document<span class="token1">.</span><span class="token3">createElement</span><span class="token1">(</span><span class="token4">'a'</span><span class="token1">)</span><span class="token1">;</span> a<span class="token1">.</span>href \<span class="token">=</span> <span class="token5">URL</span><span class="token1">.</span><span class="token3">createObjectURL</span><span class="token1">(</span>blob<span class="token1">)</span><span class="token1">;</span> a<span class="token1">.</span>download \<span class="token">=</span> name<span class="token1">;</span> <span class="token6">//这里这个name就是对应的文件名!</span> a<span class="token1">.</span><span class="token3">click</span><span class="token1">(</span><span class="token1">)</span><span class="token1">;</span> a<span class="token1">.</span><span class="token3">remove</span><span class="token1">(</span><span class="token1">)</span><span class="token1">;</span> <span class="token3">callback</span><span class="token1">(</span><span class="token4">'success'</span><span class="token1">)</span><span class="token1">;</span> <span class="token">/</span>\<span class="token">*</span>这里调用我们自己传入的回调方法,这样导出Excel完成后你就能 在外面知道导出完毕,并且再往下做自己其他的逻辑\<span class="token">*</span><span class="token">/</span> <span class="token1">}</span><span class="token1">;</span> <span class="token1">}</span><span class="token1">)</span><span class="token1">(</span><span class="token1">)</span><span class="token1">;</span> <span class="token2">export</span> <span class="token1">{</span> dateFormat<span class="token1">,</span> html\_excel <span class="token1">}</span> ``` ```