How do I export html table data as .csv file?

前端 未结 8 1174
轮回少年
轮回少年 2020-11-29 19:27

I have a table of data in an html table on a website and need to know how to export that data as .csv file.

How would this be done?

8条回答
  •  执念已碎
    2020-11-29 20:08

    For exporting html to csv try following this example. More details and examples are available at the author's website.

    Create a html2csv.js file and put the following code in it.

    jQuery.fn.table2CSV = function(options) {
        var options = jQuery.extend({
            separator: ',',
            header: [],
            delivery: 'popup' // popup, value
        },
        options);
    
        var csvData = [];
        var headerArr = [];
        var el = this;
    
        //header
        var numCols = options.header.length;
        var tmpRow = []; // construct header avalible array
    
        if (numCols > 0) {
            for (var i = 0; i < numCols; i++) {
                tmpRow[tmpRow.length] = formatData(options.header[i]);
            }
        } else {
            $(el).filter(':visible').find('th').each(function() {
                if ($(this).css('display') != 'none') tmpRow[tmpRow.length] = formatData($(this).html());
            });
        }
    
        row2CSV(tmpRow);
    
        // actual data
        $(el).find('tr').each(function() {
            var tmpRow = [];
            $(this).filter(':visible').find('td').each(function() {
                if ($(this).css('display') != 'none') tmpRow[tmpRow.length] = formatData($(this).html());
            });
            row2CSV(tmpRow);
        });
        if (options.delivery == 'popup') {
            var mydata = csvData.join('\n');
            return popup(mydata);
        } else {
            var mydata = csvData.join('\n');
            return mydata;
        }
    
        function row2CSV(tmpRow) {
            var tmp = tmpRow.join('') // to remove any blank rows
            // alert(tmp);
            if (tmpRow.length > 0 && tmp != '') {
                var mystr = tmpRow.join(options.separator);
                csvData[csvData.length] = mystr;
            }
        }
        function formatData(input) {
            // replace " with “
            var regexp = new RegExp(/["]/g);
            var output = input.replace(regexp, "“");
            //HTML
            var regexp = new RegExp(/\<[^\<]+\>/g);
            var output = output.replace(regexp, "");
            if (output == "") return '';
            return '"' + output + '"';
        }
        function popup(data) {
            var generator = window.open('', 'csv', 'height=400,width=600');
            generator.document.write('CSV');
            generator.document.write('');
            generator.document.write('');
            generator.document.write('');
            generator.document.close();
            return true;
        }
    };
    

    include the js files into the html page like this:

    
    
    
    

    TABLE:

    Title Name Phone
    Mr. John 07868785831
    Miss Linda 0141-2244-5566
    Master Jack 0142-1212-1234
    Mr. Bush 911-911-911

    EXPORT BUTTON:

    
    

提交回复
热议问题