如何从HTML表创建.CSV文件(MVC5)

时间:2017-04-28 01:56:31

标签: javascript jquery html-table export-to-csv asp.net-mvc-5

我想知道如何从HTML表创建.CSV文件。我有一个Jquery代码来从HTML表创建.XLS文件。但是,我想创建.CSV文件。谁能帮我解决一下呢?

这是从HTML表格

创建.XLS文件的代码
function fnExcelReport() {
            var tab_text = "<table border='2px'><tr bgcolor='#87AFC6'>";
            var textRange; var j = 0;
            tab = document.getElementById('table1'); // id of table

            for (j = 0; j < tab.rows.length; j++) {
                tab_text = tab_text + tab.rows[j].innerHTML + "</tr>";
                //tab_text=tab_text+"</tr>";
            }

            tab_text = tab_text + "</table>";
            tab_text = tab_text.replace(/<A[^>]*>|<\/A>/g, "");//remove if u want links in your table
            tab_text = tab_text.replace(/<img[^>]*>/gi, ""); // remove if u want images in your table
            tab_text = tab_text.replace(/<input[^>]*>|<\/input>/gi, ""); // reomves input params

            var ua = window.navigator.userAgent;
            var msie = ua.indexOf("MSIE ");

            if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./))      // If Internet Explorer
            {
                txtArea1.document.open("txt/html", "replace");
                txtArea1.document.write(tab_text);
                txtArea1.document.close();
                txtArea1.focus();
                sa = txtArea1.document.execCommand("SaveAs", true, "QI.xls");
            }
            else                 //other browser not tested on IE 11
                sa = window.open('data:application/vnd.ms-excel,' + encodeURIComponent(tab_text));

            return (sa);
        }
    });

感谢。

用于plunker的JSON文件示例:

{“count”:105,“next”:“sdfsdfdsfsdfsdf = 2”,“previous”:null,“results”:[{“id”:248,“name”:“kasar”,“slug”: “managadd”,“description”:“是一个小孩”,“status”:“active”,“absolute_url”:“http://laboursday.com”,“city”:{“id”:11,“name”: “saaid”,“slug”:“deploy”,“list_camera_uri”:“karachi.com”},“经度”:234.4334,“纬度”:23.2323,“image_url”:“gggadjggjasd.jpg”,“image_mobile_url”:“ asddfggdsaf.jpg“,”image_thumbnail_url“:”http://masdd.com/.jpg“,”image_squarethumb_url“:”asdfgfer.jpg“,”video_url“:”swrtyhnhn.mp4“,”resource_uri“:”sddssdfwweff“}]}

0 个答案:

没有答案