您好,登錄后才能下訂單哦!
這篇文章主要介紹了JS怎么實(shí)現(xiàn)導(dǎo)出Excel和CSV文件的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡(jiǎn)單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇JS怎么實(shí)現(xiàn)導(dǎo)出Excel和CSV文件文章都會(huì)有所收獲,下面我們一起來(lái)看看吧。
<html> <head> </head> <body> <script type="text/javascript" src="./xlsx.core.min.js"></script> <script type="text/javascript"> /** * 通用的打開(kāi)下載對(duì)話框方法,沒(méi)有測(cè)試過(guò)具體兼容性 * @param url 下載地址,也可以是一個(gè)blob對(duì)象,必選 * @param saveName 保存文件名,可選 */ function openDownloadDialog(url, saveName) { if (typeof url == 'object' && url instanceof Blob) { url = URL.createObjectURL(url); // 創(chuàng)建blob地址 } var aLink = document.createElement('a'); aLink.href = url; aLink.download = saveName || ''; // HTML5新增的屬性,指定保存文件名,可以不要后綴,注意,file:///模式下不會(huì)生效 var event; if (window.MouseEvent) event = new MouseEvent('click'); else { event = document.createEvent('MouseEvents'); event.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null); } aLink.dispatchEvent(event); } // 將一個(gè)sheet轉(zhuǎn)成最終的excel文件的blob對(duì)象,然后利用URL.createObjectURL下載 function sheet2blob(sheet, sheetName) { sheetName = sheetName || 'sheet1'; var workbook = { SheetNames: [sheetName], Sheets: {} }; workbook.Sheets[sheetName] = sheet; // 生成excel的配置項(xiàng) var wopts = { bookType: 'xlsx', // 要生成的文件類(lèi)型 bookSST: false, // 是否生成Shared String Table,官方解釋是,如果開(kāi)啟生成速度會(huì)下降,但在低版本IOS設(shè)備上有更好的兼容性 type: 'binary' }; var wbout = XLSX.write(workbook, wopts); var blob = new Blob([s2ab(wbout)], { type: "application/octet-stream" }); // 字符串轉(zhuǎn)ArrayBuffer function s2ab(s) { var buf = new ArrayBuffer(s.length); var view = new Uint8Array(buf); for (var i = 0; i != s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF; return buf; } return blob; } </script> <script type="text/javascript"> var exportDataSource = [{ name: '路人甲', phone: '123456789', email: '000@123456.com' }, { name: '炮灰乙', phone: '123456789', email: '000@123456.com' }, { name: '土匪丙', phone: '123456789', email: '000@123456.com' }, { name: '流氓丁', phone: '123456789', email: '000@123456.com' }, ]; //導(dǎo)出json數(shù)據(jù)源 function exportExcel() { var excelItems = []; for (let i = 0; i < exportDataSource.length; i++) { if (exportDataSource[i].name != "") { excelItems.push({ "姓名": exportDataSource[i].name, "電話": exportDataSource[i].phone, "郵箱": exportDataSource[i].email }); //屬性 } } var sheet = XLSX.utils.json_to_sheet(excelItems); openDownloadDialog(sheet2blob(sheet), 'exportdata.xlsx'); } </script> <a onclick="exportExcel()">導(dǎo)出下載</a> </body> </html>
<html> <head> <p >使用a標(biāo)簽方式將json導(dǎo)出csv文件</p> <button onclick='tableToExcel()'>導(dǎo)出</button> </head> <body> <script> function tableToExcel(){ //要導(dǎo)出的json數(shù)據(jù) const jsonData = [ { name:'路人甲', phone:'123456789', email:'000@123456.com' }, { name:'炮灰乙', phone:'123456789', email:'000@123456.com' }, { name:'土匪丙', phone:'123456789', email:'000@123456.com' }, { name:'流氓丁', phone:'123456789', email:'000@123456.com' }, ] //列標(biāo)題,逗號(hào)隔開(kāi),每一個(gè)逗號(hào)就是隔開(kāi)一個(gè)單元格 let str = `姓名,電話,郵箱\n`; //增加\t為了不讓表格顯示科學(xué)計(jì)數(shù)法或者其他格式 for(let i = 0 ; i < jsonData.length ; i++ ){ for(let item in jsonData[i]){ str+=`${jsonData[i][item] + '\t'},`; } str+='\n'; } //encodeURIComponent解決中文亂碼 let uri = 'data:text/csv;charset=utf-8,\ufeff' + encodeURIComponent(str); //通過(guò)創(chuàng)建a標(biāo)簽實(shí)現(xiàn) let link = document.createElement("a"); link.href = uri; //對(duì)下載的文件命名 link.download = "json數(shù)據(jù)表.csv"; document.body.appendChild(link); link.click(); document.body.removeChild(link); } </script> </body> </html>
<html> <head> <p >使用table標(biāo)簽方式將json導(dǎo)出xls文件</p> <button onclick='tableToExcel()'>導(dǎo)出</button> </head> <body> <script> function tableToExcel(){ //要導(dǎo)出的json數(shù)據(jù) const jsonData = [ { name:'路人甲', phone:'123456', email:'123@123456.com' }, { name:'炮灰乙', phone:'123456', email:'123@123456.com' }, { name:'土匪丙', phone:'123456', email:'123@123456.com' }, { name:'流氓丁', phone:'123456', email:'123@123456.com' }, ] //列標(biāo)題 let str = '<tr><td>姓名</td><td>電話</td><td>郵箱</td></tr>'; //循環(huán)遍歷,每行加入tr標(biāo)簽,每個(gè)單元格加td標(biāo)簽 for(let i = 0 ; i < jsonData.length ; i++ ){ str+='<tr>'; for(let item in jsonData[i]){ //增加\t為了不讓表格顯示科學(xué)計(jì)數(shù)法或者其他格式 str+=`<td>${ jsonData[i][item] + '\t'}</td>`; } str+='</tr>'; } //Worksheet名 let worksheet = 'Sheet1' let uri = 'data:application/vnd.ms-excel;base64,'; //下載的表格模板數(shù)據(jù) let template = `<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"> <head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet> <x:Name>${worksheet}</x:Name> <x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet> </x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--> </head><body><table>${str}</table></body></html>`; //下載模板 window.location.href = uri + base64(template) } //輸出base64編碼 function base64 (s) { return window.btoa(unescape(encodeURIComponent(s))) } </script> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>excel 導(dǎo)出測(cè)試</title> <style> table { border-collapse: collapse; text-align: center; vertical-align: middle; width: 800px; font-size: 20px; } button { height: 30px; width: 100px; margin: 20px 20px; background: yellowgreen; border-radius: 10px; outline: none; } input { height: 30px; padding-left: 10px; margin: 10px; } </style> </head> <body> <div id="wrap" > <h4>js腳本 導(dǎo)出excel測(cè)試</h4> <table id="tb" border="1" cellspacing="0" cellpadding="0"> <thead> <tr> <th>ID</th> <th>姓名</td> <th>年齡</td> <th>座右銘</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>張三</td> <td>18</td> <td>走的人多了,變成了路。</td> </tr> <tr> <td>2</td> <td>李四</td> <td>88</td> <td>人人都有自己走的路,哪條屬于自己呢?</td> </tr> <tr> <td>3</td> <td>王五</td> <td>81</td> <td>走別人的路,讓他沒(méi)道可走!</td> </tr> </tbody> </table> <label> 姓名:<input type="text" autocomplete id="name" placeholder="請(qǐng)輸入您的姓名..." > </label> <label> 年齡:<input type="text" autocomplete id="age" placeholder="請(qǐng)輸入您的年齡..."> </label> <label> 座右銘:<input type="text" autocomplete id="sex" placeholder="請(qǐng)輸入您的座右銘..." > </label> <button id="add">添加信息</button> <button id="out" onclick="btn_export()">導(dǎo)出文件</button> </div> <script src="./xlsx.core.min.js"></script> <script src="./jquery.min.js"></script> <script src="./excel.js"></script> <script> function btn_export() { var table = document.querySelector("#tb"); var sheet = XLSX.utils.table_to_sheet(table); //將一個(gè)table對(duì)象轉(zhuǎn)換成一個(gè)sheet對(duì)象 openDownloadDialog(sheet2blob(sheet), 'excel.xlsx'); } var id = $("input").length + 1; $(function() { $("#add").click(function() { var name = $("#name").val(); var age = $("#age").val(); var sex = $("#sex").val(); $("tbody").append("<tr><td>" + id + "</td><td>" + name + "</td> <td>" + age + "</td> <td>" + sex + "</td></tr>"); id++; $("input").val(''); }); }) </script> </body> </html>
關(guān)于“JS怎么實(shí)現(xiàn)導(dǎo)出Excel和CSV文件”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“JS怎么實(shí)現(xiàn)導(dǎo)出Excel和CSV文件”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。