export html table to csv

后端 未结 8 2010
走了就别回头了
走了就别回头了 2020-12-07 18:20

I am trying to add a feature of csv download option in my website. It should convert the html table present in the website in to csv content and make it downloadable. Ive be

8条回答
  •  一生所求
    2020-12-07 19:03

    You don't need PHP script on server side. Do that in the client side only, in browsers that accept Data URIs:

    data:application/csv;charset=utf-8,content_encoded_as_url
    

    The Data URI will be something like:

    data:application/csv;charset=utf-8,Col1%2CCol2%2CCol3%0AVal1%2CVal2%2CVal3%0AVal11%2CVal22%2CVal33%0AVal111%2CVal222%2CVal333
    

    You can call this URI by:

    • using window.open
    • or setting the window.location
    • or by the href of an anchor
    • by adding the download attribute it will work in chrome, still have to test in IE.

    To test, simply copy the URIs above and paste in your browser address bar. Or test the anchor below in a HTML page:

    Example
    

    To create the content, getting the values from the table, you can use table2CSV mentioned by MelanciaUK and do:

    var csv = $table.table2CSV({delivery:'value'});
    window.location.href = 'data:application/csv;charset=UTF-8,' + encodeURIComponent(csv);
    

提交回复
热议问题