在客户端将HTML表格导出为PPT?

11

我想知道是否有任何jquery或javascript的解决方案可以将html表格转换为PowerPoint。我找到的唯一解决方案是html表格导出。在这里,我们有所有的导出选项,但我只需要PowerPoint的解决方案。我可以使用Html表格导出,但我的问题是,对于一个导出,我应该使用整个插件。是否有仅用于ppt的示例代码?


我建议最好使用服务器端来处理这个问题。 - Oscar Jara
@OscarJara 为什么要过载服务器,当你可以将一些处理委托给客户端呢?服务器生成一个HTML表格,这是一个相当轻的负载,但如果它必须提供下载文档,从服务器加载数据会不必要地增加。由于这不是安全漏洞,因此在客户端导出格式是相当合理的。 - Juan C. V.
2个回答

8
如果库的大小让你担忧,最好的选择可能是自己修改js库。删除与幻灯片功能无关的代码片段。然后进行测试,逐步使库变得更小。除此之外,我没有发现任何已经可用此解决方案的地方。
通过上述练习,我能够将tableExport.js文件从12kb减少到5kb(非最小化),同时仍然保持导出到PowerPoint的功能。
/*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);

您可以用这段代码替换您的tableExport.js文件,并使用相同的方式调用它,通过将powerpoint作为类型传递进去,或者您可以省略它,它仍然可以正常工作。

我正在使用相同的插件,但在 PowerPoint 中它显示空白数据, 我已经在 GitHub 上检查了这个问题: https://github.com/wenzhixin/bootstrap-table/issues/1660 如果它适用于 PowerPoint,你能指导我需要哪些更改吗? - Alok Jha
@AlokJha 抱歉,我从未使用过这个插件。但是在您提供的链接中,似乎bootstrap-table曾经有PDF选项,但由于从未得到支持/工作而被删除。请参见:https://github.com/wenzhixin/bootstrap-table/commit/0679c9ed319b1629f2088cb9089acf856f4e87dd - Trevor

6
您可以使用相同的解决方案,通过修改JS文件仅使用与MS-Office相关部分。实际上,将PowerPoint表格转换的函数与将Excel和Word转换的函数相同。
要做到这一点,您只需要jquery.base64.js文件和文件tableExport.js,但是将所有tableExport.js内容更改为以下内容
/*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);

所有其他文件,包括 jspdf 文件夹均可删除。

这样您就可以将表格导出为Excel、doc和PowerPoint格式(实际上,当导出为doc和powerpoint时,您所做的是在文档中嵌入一个Excel电子表格,因此插件在三种格式下都相同)。

使用方式与原始插件相同


好的,我会尝试这个并回来告诉你。 - Shrinivas Pai

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接