如何自动将图表从Excel(或Calc)导出到PNG [英] How to automatically export a chart from Excel (or Calc) to PNG

查看:662
本文介绍了如何自动将图表从Excel(或Calc)导出到PNG的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:

我正在开发一个Web应用程序,该应用程序将数据从数据库导出到Excel,包括图表,首先是导出的主要原因。

I'm working on a web application that exports data from a database to Excel, including a chart, which is the main reason for the export in the first place.

现在,我希望图表也可以在网页上显示,而无需导出数据并打开下载的Excel文件。当然,可以使用JS库完成此操作,但是由于图表非常复杂,我想重新使用现有的导出功能来转换为excel,或者现在再次使用JS重新编写。

Now I would like for the chart to also be visible on the web page, without the need of exporting the data and opening the downloaded excel file. This could be of course done with JS libraries, but seeing as the chart is quite complicated, I would like to re-use existing export to excel instead or rewriting it all over again, now in JS.

所需的解决方案:

因此,这是可能的最佳方案:如果可以从中导出图表将excel文件转换为png(或jpg或其他格式),而无需通过命令行等方式操作excel文件。另外,生成的文件在OpenOffice中可以很好地打开,因此我可以接受。然后我可以将其导出到服务器上的excel,然后从excel中导出图像,然后将图像发送到客户端。

So, this is the best scenario possible: If it was possible to export a chart from excel file to png (or jpg or whatever) without even opeing the excel file, like from a command line or something. Also the generated files open well in OpenOffice, so I could go with that. Then I could just export to excel on server, then export the image from excel, and just send the image to client.

这样的事情是否可能?如果可以,我不反对第三方程序。如果没有,那么您认为此情形的下一个最佳解决方案是什么?

Is something like this even possible? I'm not against a third party program if it would do the trick. If not, what do you think is the next best solution for this scenario?

我有Excel 2016,并且正在使用PHPExcel生成excel文件,

I have Excel 2016, and I'm generating the excel file with PHPExcel, if that is of any importance.

可能的解决方案:

似乎有些不错的选择到将文档另存为网页,但是我不知道是否可以在不打开excel UI的情况下通过命令行执行操作。

Some good options seem to be to save the document as web page, but I don't know if you can do that fro mcommand line / without opening the excel UI.

也<一个href = https://wiki.openoffice.org/wiki/API rel = nofollow title = wiki> Open Office API 看起来不错,但我从未使用过,是否可以在不打开Calc UI的情况下通过此API(使用Java或其他工具)导出图表?我知道开放式办公室有--invisible选项,这可能会有用。

Also the Open Office API doesn't look half bad, but I have never used it before, could you export a chart via this API (with Java or something) without opening the Calc UI? I know open office has the --invisible option, that could prove useful.

推荐答案

好的,这会将图表导出为.png文件,但会打开excel文件(然后将其关闭),您可以尝试以下操作:

Alright, this will export the chart as a .png file, but it opens the excel file (and closes it afterwards), you can try this:

将其放入 .vbs 文件并通过控制台运行它。

Put it into a .vbs-file and run it via console.

    Set objXLS = CreateObject("Excel.Application")
    Set yWkbk = objXLS.Application.Workbooks.Open("C:\yourpath\yourfile.xls")
    'Sheet ID can change of course 
    Set yWksht = yWkbk.Sheets(1)
    Set yChart = yWksht.ChartObjects("yourchartname").Chart
    yChart.Export "C:\yourpath\yourfilename.png", "PNG"

    objXLS.Quit
    Set objXLS = Nothing

这篇关于如何自动将图表从Excel(或Calc)导出到PNG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆