Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export html table to ppt on client side?

I want to know whether we have any jquery or javascript solution to convert html table to powerpoint.Only solution I got is html table export. Here we have all the export options but I want solution only for powerpoint. I can use Html table export but my concern is, for one export i should use whole plugin. Is there a sample code only for ppt?

like image 858
Shrinivas Pai Avatar asked Sep 14 '15 09:09

Shrinivas Pai


2 Answers

If the size of the library is a concern for you, your best bet may be to modify the js library on your own. Taking out pieces of code that may not be related to the power point functions. And then testing, progressively making the library smaller and smaller. Other than that I didn't find any where obvious that already has this solution available.

By performing the exercise above I was able to take the tableExport.js file from 12kb to 5kb (non minimized) while still maintaining the export to power-point functionality.

/*The MIT License (MIT)

Copyright (c) 2014 https://github.com/kayalshri/

Permission is hereby granted....
....
*/

(function($){
    $.fn.extend({
        tableExport: function(options) {
            var defaults = {
                    separator: ',',
                    ignoreColumn: [],
                    tableName:'yourTableName',
                    type:'powerpoint',
                    escape:'true',
                    htmlContent:'false',
                    consoleLog:'false'
            };

            var options = $.extend(defaults, options);
            var el = this;

            if(defaults.type == 'powerpoint'){
                //console.log($(this).html());
                var excel="<table>";
                // Header
                $(el).find('thead').find('tr').each(function() {
                    excel += "<tr>";
                    $(this).filter(':visible').find('th').each(function(index,data) {
                        if ($(this).css('display') != 'none'){                  
                            if(defaults.ignoreColumn.indexOf(index) == -1){
                                excel += "<td>" + parseString($(this))+ "</td>";
                            }
                        }
                    }); 
                    excel += '</tr>';                       

                });                 


                // Row Vs Column
                var rowCount=1;
                $(el).find('tbody').find('tr').each(function() {
                    excel += "<tr>";
                    var colCount=0;
                    $(this).filter(':visible').find('td').each(function(index,data) {
                        if ($(this).css('display') != 'none'){  
                            if(defaults.ignoreColumn.indexOf(index) == -1){
                                excel += "<td>"+parseString($(this))+"</td>";
                            }
                        }
                        colCount++;
                    });                                                         
                    rowCount++;
                    excel += '</tr>';
                });                 
                excel += '</table>'

                if(defaults.consoleLog == 'true'){
                    console.log(excel);
                }

                var excelFile = "<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:x='urn:schemas-microsoft-com:office:"+defaults.type+"' xmlns='http://www.w3.org/TR/REC-html40'>";
                excelFile += "<head>";
                excelFile += "<!--[if gte mso 9]>";
                excelFile += "<xml>";
                excelFile += "<x:ExcelWorkbook>";
                excelFile += "<x:ExcelWorksheets>";
                excelFile += "<x:ExcelWorksheet>";
                excelFile += "<x:Name>";
                excelFile += "{worksheet}";
                excelFile += "</x:Name>";
                excelFile += "<x:WorksheetOptions>";
                excelFile += "<x:DisplayGridlines/>";
                excelFile += "</x:WorksheetOptions>";
                excelFile += "</x:ExcelWorksheet>";
                excelFile += "</x:ExcelWorksheets>";
                excelFile += "</x:ExcelWorkbook>";
                excelFile += "</xml>";
                excelFile += "<![endif]-->";
                excelFile += "</head>";
                excelFile += "<body>";
                excelFile += excel;
                excelFile += "</body>";
                excelFile += "</html>";

                var base64data = "base64," + $.base64.encode(excelFile);
                window.open('data:application/vnd.ms-'+defaults.type+';filename=exportData.doc;' + base64data);

            }

            function parseString(data){

                if(defaults.htmlContent == 'true'){
                    content_data = data.html().trim();
                }else{
                    content_data = data.text().trim();
                }

                if(defaults.escape == 'true'){
                    content_data = escape(content_data);
                }

                return content_data;
            }

        }
    });
})(jQuery);

You can replace your tableExport.js file with this code and call it the same way by passing in powerpoint as the type, or you can omit it and it will still work.

like image 165
Trevor Avatar answered Oct 20 '22 23:10

Trevor


You can use the same solution, by modifying the JS files to use only the part related to MS-Office. Actually, the function that transforms Porwerpoint table is the same as it transforms Excel and Word.

To do this, you would need only the jquery.base64.js file, and the file tableExport.js, but changing all the tableExport.js contents of the following:

/*The MIT License (MIT)
Copyright (c) 2014 https://github.com/kayalshri/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.*/

(function($){
        $.fn.extend({
            tableExport: function(options) {
                var defaults = {
                        separator: ',',
                        ignoreColumn: [],
                        tableName:'yourTableName',
                        type:'excel',
                        escape:'true',
                        htmlContent:'false',
                        consoleLog:'false'
                };

                var options = $.extend(defaults, options);
                var el = this;

                if(defaults.type == 'excel' || defaults.type == 'doc'|| defaults.type == 'powerpoint'  ){
                    //console.log($(this).html());
                    var excel="<table>";
                    // Header
                    $(el).find('thead').find('tr').each(function() {
                        excel += "<tr>";
                        $(this).filter(':visible').find('th').each(function(index,data) {
                            if ($(this).css('display') != 'none'){                  
                                if(defaults.ignoreColumn.indexOf(index) == -1){
                                    excel += "<td>" + parseString($(this))+ "</td>";
                                }
                            }
                        }); 
                        excel += '</tr>';                       

                    });                 


                    // Row Vs Column
                    var rowCount=1;
                    $(el).find('tbody').find('tr').each(function() {
                        excel += "<tr>";
                        var colCount=0;
                        $(this).filter(':visible').find('td').each(function(index,data) {
                            if ($(this).css('display') != 'none'){  
                                if(defaults.ignoreColumn.indexOf(index) == -1){
                                    excel += "<td>"+parseString($(this))+"</td>";
                                }
                            }
                            colCount++;
                        });                                                         
                        rowCount++;
                        excel += '</tr>';
                    });                 
                    excel += '</table>'

                    if(defaults.consoleLog == 'true'){
                        console.log(excel);
                    }

                    var excelFile = "<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:x='urn:schemas-microsoft-com:office:"+defaults.type+"' xmlns='http://www.w3.org/TR/REC-html40'>";
                    excelFile += "<head>";
                    excelFile += "<!--[if gte mso 9]>";
                    excelFile += "<xml>";
                    excelFile += "<x:ExcelWorkbook>";
                    excelFile += "<x:ExcelWorksheets>";
                    excelFile += "<x:ExcelWorksheet>";
                    excelFile += "<x:Name>";
                    excelFile += "{worksheet}";
                    excelFile += "</x:Name>";
                    excelFile += "<x:WorksheetOptions>";
                    excelFile += "<x:DisplayGridlines/>";
                    excelFile += "</x:WorksheetOptions>";
                    excelFile += "</x:ExcelWorksheet>";
                    excelFile += "</x:ExcelWorksheets>";
                    excelFile += "</x:ExcelWorkbook>";
                    excelFile += "</xml>";
                    excelFile += "<![endif]-->";
                    excelFile += "</head>";
                    excelFile += "<body>";
                    excelFile += excel;
                    excelFile += "</body>";
                    excelFile += "</html>";

                    var base64data = "base64," + $.base64.encode(excelFile);
                    window.open('data:application/vnd.ms-'+defaults.type+';filename=exportData.doc;' + base64data);

                }


                function parseString(data){

                    if(defaults.htmlContent == 'true'){
                        content_data = data.html().trim();
                    }else{
                        content_data = data.text().trim();
                    }

                    if(defaults.escape == 'true'){
                        content_data = escape(content_data);
                    }



                    return content_data;
                }

            }
        });
    })(jQuery);

All other files, including jspdf folder can be deleted.

This way you can export the table to the excel, doc and PowerPoint format (actually, when exported to doc and powerpoint, what you do is embed an Excel spreadsheet in the documents, so the plugin is the same for all three formats)

Use the plugin in the same way as the original plugin

like image 6
Juan C. V. Avatar answered Oct 20 '22 21:10

Juan C. V.