Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Creating CSV from HTML using jQuery

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 440
    Comment on it

    In one of the project, I needed to create the CSV file from the html using jQuery.

    To make this possible, I created the below code to convert the html into csv file using jQuery.

    <script type="text/javascript" src="jquery.min.js"></script>
    
    <hr>
    <div id="dvData">
        <table class="mustBe" border="1">
            <tr>
                <td>Column One</td>
                <td>Column Two</td>
                <td>Column Three</td>
                            <td>Column One</td>
                <td>Column Two</td>
                <td>Column Three</td>
    
            </tr>
            <tr>
                <td>1</td>
                <td>Rs. 1,22.5</td>
                <td>3</td>
                            <td>Column One</td>
                <td>Column Two</td>
                <td>Column Three</td>
    
            </tr>
            <tr>
                <td>4</td>
                <td>5.121</td>
                <td>6.91</td>
                            <td>Column One</td>
                <td>Column Two</td>
                <td>Column Three</td>
    
            </tr>
            <tr>
                <td>7</td>
                <td>212 8.11</td>
                <td>9</td>
                            <td>Column One</td>
                <td>Column Two</td>
                <td>Column Three</td>
    
            </tr>
        </table>
    
        <table>
            <tr>
                <td>Column One-1</td>
                <td>Column Two-2</td>
                <td>Column Three-3</td>
            </tr>
            <tr>
                <td>1</td>
                <td>Rs. 1,22.5</td>
                <td>3</td>
            </tr>
            <tr>
                <td>4</td>
                <td>5.121</td>
                <td>6.91</td>
            </tr>
            <tr>
                <td>7</td>
                <td>212 8.11</td>
                <td>9</td>
            </tr>
        </table>
    
        <table class="mustBe" border="1">
            <tr>
                <td>Column One-1</td>
                <td>Column Two-2</td>
                <td>Column Three-3</td>
            </tr>
            <tr>
                <td>19</td>
                <td>Rs. 1,22.5</td>
                <td>09/10/2013 08:09:59</td>
            </tr>
            <tr>
                <td>4</td>
                <td>5.121</td>
                <td>09/10/2013 08:09:59</td>
            </tr>
            <tr>
                <td>7</td>
                <td>$212 8.11</td>
                <td>09/10/2013 08:09:59</td>
            </tr>
    
            <tr>
                <td>7</td>
                <td>$2128.11</td>
                <td>09/10/2013 08:09:59</td>
            </tr>
    
            <tr>
                <td>7.9191,27u28</td>
                <td>212 8.11</td>
                <td>9</td>
            </tr>
        </table>
    </div>
    <br/>
    
    <div class="newTableHtml" style="display:none"></div>
    
    <a href="#" class="export">Export Table</a>
    <script>
     $(document).ready(function () {
        function exportTableToCSV($table, filename) {
            var $rows = $table.find('tr:has(td)'),
                tmpColDelim = String.fromCharCode(11), // vertical tab character
                tmpRowDelim = String.fromCharCode(0), // null character
    
                // actual delimiter characters for CSV format
                colDelim = '","',
                rowDelim = '"\r\n"',
    
                // Grab text from table into CSV formatted string
                csv = '"' + $rows.map(function (i, row) {
                    var $row = $(row),
                        $cols = $row.find('td');
    
                    return $cols.map(function (j, col) {
                        var $col = $(col),
                            text = $col.text();
    
                        return text.replace('"', '""'); // escape double quotes
    
                    }).get().join(tmpColDelim);
    
                }).get().join(tmpRowDelim)
                    .split(tmpRowDelim).join(rowDelim)
                    .split(tmpColDelim).join(colDelim) + '"',
    
                // Data URI
                csvData = 'data:application/csv;charset=utf-8,' + encodeURIComponent(csv);
    
            $(this)
                .attr({
                'download': filename,
                    'href': csvData,
                    'target': '&#95;blank'
            });
        }
    
        $(".export").on('click', function (event) {
         var date = new Date(),
        output = document.getElementById( 'output' ),
        dateString = (date.getMonth() + 1) + "/" + date.getDate() + "/" + date.getFullYear().toString().substr(2,2);
    
          var i = 1;
          var dateTable = "<table><tr><td>DATE: </td><td>"+dateString+"</td></tr</table><br/>";
          var newTable ="";
          //alert(dateTable); return false;
          $('table').each(function(){
            if($(this).hasClass('mustBe')) {
               var html = $(this).html();
             <br/>";
             newTable += "<table><tr><td></td></tr><tr><td>-----------REPORT-"+i+"-------------</td></tr>"+html+"<tr><td></td></tr><tr><td></td></tr></table><br/><br/>";
    
               i++;
            }
          });
    
          $('.newTableHtml').html(dateTable+newTable);
    
           exportTableToCSV.apply(this, [$('.newTableHtml'), 'export.csv']);
        });
     });
    </script>
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: