js download file, FileSaver.js export txt, excel file

Source code download: https://download.csdn.net/download/qq_29132907/11177565
1. Export txt

A new "Certificate Expiry Alarm" function has been newly developed. One function to be implemented is: the background will pass the certificate to me in the form of a string to realize the export of Txt files.

I chose the FileSaver.js plugin to export txt files

Download address: https://github.com/eligrey/FileSaver.js/

usage:

Step 1: Quote

Step 2: Export generated text

function downloadText (data) {
var blob = new Blob ([JSON.stringify (data)], {type: “text / plain; charset = utf-8”});
saveAs (blob, “export.txt”);
}
Step 3: Call

downloadText (data)
2. Export excel


<! DOCTYPE html>
<html>
<head>
    <meta http-equiv = "Content-Type" content = "text / html; charset = utf-8" />
    <title> </ title>
    <meta charset = "utf -8 "/>
    <style>
        / * This style is only used for browser page effects, Excel will not separate the table borders, this style is not required * /
        table {
            border-collapse: collapse;
        }
    </ style>
</ head>
<body>
    <!-Set border = "1" to display the table border->
    <table border = "1">
        <!-caption element can generate a table title, and its cell column span is the number of table columns ->
        <caption> Student score table </ caption>
        <tr>
            <!-You can use rowspan and colspan to merge cells->
            <th rowspan = "2"> number </ th>
            <th rowspan="2">学号</th>
            <th rowspan="2">姓名</th>
            <th rowspan="2">性别</th>
            <th rowspan="2">年龄</th>
            <th colspan="3">成绩</th>
        </tr>
        <tr>
            <th>语文</th>
            <th>数学</th>
            <th>英语</th>
        </tr>
        <tr>
            <td>1</td>
            <td>2016001</td>
            <td>张三</td>
            <td>男</td>
            <td>13</td>
            <td>85</td>
            <td>94</td>
            <td>77</td>
        </tr>
        <tr>
            <td>2</td>
            <td>2016002</td>
            <td>李四</td>
            <td>女</td>
            <td>12</td>
            <td>96</td>
            <td>84</td>
            <td>89</td>
        </tr>
    </table>

    <a> Export table </a>

    <script>
        // Use the outerHTML attribute to get the HTML code of the entire table element (including the <table> tag), then wrap it into a complete HTML document, set charset to urf-8 to prevent Chinese garbled
        var html = "<html> < head> <meta charset = 'utf-8' /> </ head> <body> "+ document.getElementsByTagName (" table ") [0] .outerHTML +" </ body> </ html>

Published 51 original articles · 19 praises · 60,000+ views

Guess you like

Origin blog.csdn.net/qq_40999917/article/details/105297496