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" } ] }

12
  • Please see the following answer.. it shows a few options. Commented Apr 28, 2017 at 2: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? Commented Apr 28, 2017 at 2: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. Commented Apr 28, 2017 at 2:36
  • @MohammedShereif Can you create a plnkr plnkr.co to demonstrate? Commented Apr 28, 2017 at 2:37
  • 1
    Thanks to all who helped me. Finally it worked by using ExcellentExport.js v1.4 from the 2nd solution from the link : stackoverflow.com/questions/15547198/export-html-table-to-csv Commented Apr 28, 2017 at 4:09

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.