0

I would like to know how to create a .CSV file from a HTML table. I have a Jquery code to create a .XLS file from an HTML table. But, I want to create .CSV file. Can anyone help me to figure it out?

This is the code to create a .XLS file from HTML table

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);
        }
    });

Thanks.

Example of JSON file for the purpose of plunker:

{ "count": 105, "next": "sdfsdfdsfsdfsdf=2", "previous": null, "results": [ { "id": 248, "name": "kasar", "slug": "managadd", "description": "was a small child", "status": "active", "absolute_url": "http://laboursday.com", "city": { "id": 11, "name": "saaid", "slug": "deploy", "list_camera_uri": "karachi.com" }, "longitude": 234.4334, "latitude": 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" } ] }

Mohammed Shereif
  • 145
  • 1
  • 11
  • Please see the following [answer](http://stackoverflow.com/questions/15547198/export-html-table-to-csv?rq=1).. it shows a few options. – Dan Apr 28 '17 at 02:07
  • `javascript` at Question appears to create a `.csv` file at `sa = window.open('data:application/csv;charset=utf-8' + encodeURIComponent(tab_text));`? Have you tried removing `if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./)) {} else`? – guest271314 Apr 28 '17 at 02:09
  • my apologies @guest271314 . There was a small mistake in my code and I have corrected it. By the way, I have tried the two ways also. It shows page not found. – Mohammed Shereif Apr 28 '17 at 02:36
  • @MohammedShereif Can you create a plnkr https://plnkr.co to demonstrate? – guest271314 Apr 28 '17 at 02:37
  • @guest271314 . I have created a plnkr project. Link: https://plnkr.co/edit/i5NmavEXfukgYkodgNyl – Mohammed Shereif Apr 28 '17 at 03:08
  • There are errors logged at `console` at plnkr. `jsonTable.js` is not included as a file at left panel. – guest271314 Apr 28 '17 at 03:16
  • What input are expected at ` – guest271314 Apr 28 '17 at 03:21
  • Only JSON text can be pasted there. For eg: (I will edit at the question itself – Mohammed Shereif Apr 28 '17 at 03:37
  • Possible duplicate of [export html table to csv](http://stackoverflow.com/questions/15547198/export-html-table-to-csv) – Sam Axe Apr 28 '17 at 03:43
  • @SamAxe . Hi, I have already tried the example, but the table2CSV library can't be accessed, the downloadable file is in a website, which is offline now. – Mohammed Shereif Apr 28 '17 at 03:47
  • Is this what you're looking for? https://jsfiddle.net/terryyounghk/KPEGU/ – SupposedlySam Apr 28 '17 at 04:06
  • 1
    Thanks to all who helped me. Finally it worked by using ExcellentExport.js v1.4 from the 2nd solution from the link : http://stackoverflow.com/questions/15547198/export-html-table-to-csv – Mohammed Shereif Apr 28 '17 at 04:09

0 Answers0