How to automatically export a chart from Excel (or Calc) to PNG

不想你离开。 提交于 2020-01-05 05:28:05

问题


The problem:

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.

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.

Desired solution:

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?

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

Posible solutions:

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.

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.


回答1:


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

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



回答2:


No need to use the API. This command worked for me:

"C:\Program Files\LibreOffice 5\program\soffice.exe" -convert-to png "Untitled 1.ods" -headless

For a discussion see: https://ask.libreoffice.org/en/question/2641/convert-to-command-line-parameter/



来源:https://stackoverflow.com/questions/38787803/how-to-automatically-export-a-chart-from-excel-or-calc-to-png

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!