【问题标题】:jQuery Datatables export to excelHtml5 HYPERLINK issuejQuery Datatables 导出到 excelHtml5 HYPERLINK 问题
【发布时间】:2016-10-25 15:13:14
【问题描述】:

我有一个使用 jQuery 插件 Datatables 生成的 Google 脚本网站。 我在使用 Excel HYPERLINK 导出数据表插件的 Excel 功能时遇到问题。

我想在导出的 Excel 文件中添加一个可点击的超链接,因此我在 Javascript 中按如下格式设置链接:

=HYPERLINK("photourl";"Photo 1")

生成Excel导出,格式正常。然而,它显示了上面的 sn-p 而不是可点击的链接。当我选择单元格并单击一次定义而不进行更改时,它会自动显示可单击的 URL。

有什么办法可以把它变成可点击的链接吗?

【问题讨论】:

标签: javascript jquery datatables export-to-excel


【解决方案1】:

我希望我的解决方案能帮助某人将 excel 导出中的链接扩展到已经非常 有用的图书馆。

经过数小时的搜索,我发现很多人在此处和 Datatables 的论坛中寻找 Excel 导出链接的解决方案。

主要问题是默认导出只考虑了两种不同的格式。数字和内联字符串。 链接既不是内联字符串也不是数字,它是一个函数,需要 typ str。

在寻找解决方案的过程中,我发现了许多有用的部分。

  1. 您必须调整导出,已经为此提供了“自定义”选项。 https://datatables.net/extensions/buttons/examples/html5/excelTextBold.html 在此示例中,考虑了 C 列中的所有单元格。我们想遍历所有单元格并在那里找到可能的 URL。

  2. 我们想用公式替换链接。默认情况下,它具有单元格类型内联,这必须由类型 str 和用作值的公式替换。 感谢 Dzyann,他展示了它是如何工作的。 https://datatables.net/forums/discussion/42097/can-you-export-a-table-and-format-a-cell-to-use-a-formula-using-orthogonal-data

  3. 要为链接添加下划线,应提供格式 [4]。 可用格式列表:https://datatables.net/reference/button/excelHtml5#Built-in-styles

我的解决方案适合我的要求:

    // (1.) customize export
    customize: function( xlsx ) {

        var sheet = xlsx.xl.worksheets['sheet1.xml'];

        // Loop over all cells in sheet
        $('row c', sheet).each( function () {

            // if cell starts with http
            if ( $('is t', this).text().indexOf("http") === 0 ) {

                // (2.) change the type to `str` which is a formula
                $(this).attr('t', 'str');
                //append the formula
                $(this).append('<f>' + 'HYPERLINK("'+$('is t', this).text()+'","'+$('is t', this).text()+'")'+ '</f>');
                //remove the inlineStr
                $('is', this).remove();
                // (3.) underline
                $(this).attr( 's', '4' );
            }
        });
}




更新!! IE11

在 neirda 发现 IE11 在 $(this) 中添加非 HTML 对象时出现问题后,不得不寻找另一个解决方案。同依据:&lt;f&gt; HYPERLINK

文件:buttons.html5.js

行:1098

插入了一个开关,可以为 URL 内容创建不同的 Celle。 (作为公式,带有 HYPERLINK)

// Formula HYPERLINK for http-content, 
// is a URL if: a) started first char of cell content and 
//      b) without blanks
// s:4 use to unterline
if (    (row[i].indexOf("http") === 0) 
    &&
    (row[i].indexOf(" ") < 0 )  ) {

    cell = _createNode( rels, 'c', {
        attr: {
            t: 'str',
            r: cellId,
            s: 4
        },
        children:{
            row: _createNode( rels, 'f', { text: 'HYPERLINK(\"'+text+'\",\"'+text+'\")' } )
        }
    } );
} else {
    // String output - replace non standard characters for text output
    cell = _createNode( rels, 'c', {
        attr: {
            t: 'inlineStr',
            r: cellId
        },
        children:{
            row: _createNode( rels, 'is', {
                children: {
                    row: _createNode( rels, 't', {
                        text: text
                    } )
                }
            } )
        }
    } );
}

【讨论】:

  • 我在 Firefox 上成功使用了你的代码,但它似乎在 IE 11 下不起作用,知道为什么吗?
  • 感谢您的测试!对,不幸的是它在 IE11 中不起作用。在 Edge、Chrome Firefox 中,链接已成功导出。不显示错误消息。相关步骤都通过了debug,没有异常……谁能帮帮我们?
  • 显然是 jQuery ".append()" 在 IE 11 中无法正常工作,也许有替代技术,但我不知道
  • 在 IE11 中,以这种方式创建非 HTML 对象似乎是一个问题。此外,此非 HTML 对象无法通过 append 附加。所以我不得不做我不想改变模块“jQuery.Datatable 按钮/js/buttons.html5.js”
  • 非常感谢这个替代解决方案,我确认它在我的用例中适用于 IE 11。
【解决方案2】:

一种解决方案是使用 Excel 超链接公式格式的表达式,例如:

='=HYPERLINK("https://[my website].com/' & [identifier] &'","' & [Friendly Excel Value] & '")'

然后你会发现在Excel里面默认是不会自动识别公式的。要强制识别,最简单的方法是将 (Ctrl+H) All equals '=' 替换为 equals '='。

然后链接应该可以工作了。

http://office.microsoft.com/en-gb/excel-help/hyperlink-function-HP010062412.aspx

https://superuser.com/questions/448376/what-is-the-excel-hotkey-to-re-calculate-all-formula-in-sheet

【讨论】:

    【解决方案3】:

    在不使用任何服务器端语言的情况下,在 Execl 中导出是一项非常艰巨的工作,但是您可以编写 XML 代码以 xls 格式导出数据表我有一些工作示例 请找到代码和文件here

    这是jquery插件

    我正在编写示例代码来导出文件

    <html>
            <head>
                <meta charset="UTF-8">
                <link  rel="stylesheet"  href="css/chintanTableDesign_1.css"/>
                <title></title>
               <script src="http://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
                 <script src="http://eportal.esparkbiz.com/asset/js/export_execl_js/jquery.battatech.excelexport.js" language="javascript" type="text/javascript"></script>
    
            </head>
            <body>
                <table class="tableChintan" width="100%">
                    <tr>
                        <th colspan="10" style="font-size: 25px;text-align: center">
                            ABC Pvt. ltd.
                        </th>
                        <th>
                            <select id="type_account" name="type_account" onchange="GetFilter();">
                                <option value="ALL" >ALL</option>
                                <option value="AC" >AC</option>
                                <option value="CASH" >CASH</option>
                                <option value="PF" selected>PF</option>                        
                            </select>
                        </th>
                        <th>
                            <a id="btnExport" href="javascript:void(0);"> EXPORT</a>
                        </th>
                    </tr></table>
                <table class="tableChintan" width="100%" id="tblExport">
                    <tr>
                        <th>Employee Name</th>
                        <th>Month</th>
                        <th>BASIC</th>
                        <th>DA</th>
                        <th>HRA</th>
                        <th>TA</th>
                        <th>Sp Allownce</th>
                        <th>LEAVE ENCASH</th>
                        <th>abs_days</th>
                        <th>extra_days</th>
                        <th>PF EMP</th>
                        <th>PF COMP</th>
        <!--                <th>ESI EMP</th>
                        <th>ESI COMP</th>-->
                        <th>PT</th>
                        <th>TOTAL SAL CHEQUE</th>
                        <th>actual_sal </th>
                        <th>actual_sal WP</th>
                        <th>NA</th>
                        <th></th>
    
                    </tr>
                   </tr></table>       
         </table>
            </body>
        </html>
    
        <script type="text/javascript">
            $(document).ready(function () {
                $("#btnExport").click(function () {
                    $("#tblExport").btechco_excelexport({
                        containerid: "tblExport"
                        , datatype: $datatype.Table
                    });
                });
    
            });
    </script>
    

    请不要忘记包含您的 jquery.min.js

    如果你想强制重命名文件,请尝试,然后告诉我我有另一个相同的 jquery 插件

    享受!!!!!!!!!!!!

    【讨论】:

      猜你喜欢
      • 2022-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-16
      • 2015-08-04
      • 1970-01-01
      相关资源
      最近更新 更多