jQuery数据表导出到excelHtml5 HYPERLINK问题

时间:2016-10-25 15:13:14

标签: javascript jquery datatables export-to-excel

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

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

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

生成Excel导出并且格式正常。但是它会显示上面的确切片段而不是可点击的链接。当我选择单元格并单击定义而不进行更改时,它会自动显示可单击的URL。

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

3 个答案:

答案 0 :(得分:2)

  

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

经过几个小时的搜索,我发现很多人在这里以及在Datatables的论坛中寻找Excel导出链接的解决方案。

主要问题是默认导出只考虑两种不同的格式。数字和inlinestring。 链接既不是inlinestring也不是数字,它是一个函数,需要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

  4. 我的解决方案符合我的要求:

        // (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' );
                }
            });
    }
    

    <强> UPDATE !! IE11

    在neirda发现IE11在向$(this)添加非HTML对象时遇到问题后,必须找到另一个解决方案。同样的基础:<f> HYPERLINK

    文件:buttons.html5.js

    行:1098

    插入了一个为网址内容创建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
                        } )
                    }
                } )
            }
        } );
    }
    

答案 1 :(得分:1)

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

这是jquery插件

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

output = etree.tostring(
    tree,
    xml_declaration=True,
    pretty_print=True,
    encoding=tree.docinfo.encoding,
)
output = output.replace("\" ","\"\n")
with open(filename, "w") as f:
    parent_tag_line = None
    for i, line in enumerate(output.splitlines()):
        line_stripped = line.lstrip(" ")
        line_ident = len(line) - len(line_stripped)
        if parent_tag_line is not None:
            if line_ident == 0 and line[:2] != "</":
                line = (parent_line_ident+2)*" " + line
            else:
                parent_tag_line = line
                parent_line_ident = line_ident
                line_stripped = line.lstrip()
                if line_stripped[:4] != "<!--" and line_stripped[:2] != "</":
                    line="\n"+line
        else:
            parent_tag_line = line
            parent_line_ident = line_ident
        print >>f, line

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

如果你想强行重命名文件请尝试,然后让我知道我有另一个相同的jquery插件

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

答案 2 :(得分:0)

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

='= HYPERLINK(“https://[my网站] .com /'&amp; [标识符]&amp;'”,“'&amp; [友好的Excel值]&amp;'”)'

然后你会发现在Excel中它默认不会自动识别公式。要强制识别,最简单的方法是替换(Ctrl + H)所有等于'='等于'='。

该链接应该有效。

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