繁体   English   中英

如何将base64图像导出为ex​​cel?

[英]How do I export a base64 image to excel?

我正在寻找一种从Angularjs中的NVd3导出Excel图表的方法。

我正在尝试这样的代码( jsfiddle

<button id="myButtonControlID">Export Table data into Excel</button>
<div id="divTableDataHolder">
    <table>
        <tbody>
            <tr>
                <td><img src='http://codepattern.net/Blog/pics/CodepatternLogoN.png' alt=''/></td>
            </tr>
            <tr>
                <td><img id="img" src='' alt=''/></td>
            </tr>
        </tbody>
    </table>
</div>
$("[id$=myButtonControlID]").click(function(e) {
    window.open('data:application/vnd.ms-excel,' + encodeURIComponent( $('div[id$=divTableDataHolder]').html()));
    e.preventDefault();
});
document.getElementById("img").src ="data:image/png;base64,..."

Excel似乎不喜欢base64编码的图像......

尝试使用当前失败图像的PNG base64编码数据向服务器执行ajax POST请求,进入存储图像并为其提供托管的服务,一些示例php代码(我假设您可以访问Linux服务器)可以运行php):

<?php
$somecode = unique_name(); //left as excersise for the reader.

move_uploaded_file($_FILES["filename"]["tmp_name"],"destination_$somecode.png");
//also, check for errors.
echo $somecode;

然后只需使用服务器的响应作为excel文件中的URL。 这样你就可以嵌入到图像的链接而不是图像本身。

所以细胞内容可以类似于:

<td><img src='yourhost.com/your_image_location' /></td>

较小的excel文件应该让人开心,但你必须考虑文件将存在多长时间,考虑包括用于生成表单的实际数据,因此未来的Excel用户可以廉价地重建图形,如果图像服务器下降,或图像“过期”。

我希望这有帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM