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

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.

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

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/

Related

Exporting WebBrowser to PDF with userform or in spreadsheet

I am implementing a feature to my excel-program that will show me a map (from google maps) of some coordinates. The idea is to be able to export this map with other pages to PDF. It looks like I have two options:
1) Inserting a ActiveX "Microsoft Web-Browser" directly in to the worksheet and then having excel export said worksheet to a PDF.
2) Inserting a ActiveX "Microsoft Web-Browser" in a userform.
My problem for 1) is that it doesn't seem to work for newer version of excel (2013>), but it does work for Excel 2007.
The problem with 2) is that it is hard to export a userform to PDF when you also want 2 different pages from the workbook in the same PDF (I do not know if userform1.printform has that feature).
My question is if there is either a way to insert a Web-Browser directly in Excel, for all versions of excel, or a way for a macro to export both worksheets and userform in the same PDF.
I suggest the legal way number 3) Using the Google Maps Static API to download a map as image to a temporary folder and import that image (dynamically) into your Excel file. Then you can export that Excel file to PDF as usual.
The Maps Static API service creates your map based on URL parameters sent through a standard HTTP request and returns the map as an image you can display on your web page.
This means you can download the image file through a standard HTTP request to your computer and place it into Excel.
I'm pretty sure the ways you try to automate it is against Google's terms of use because automated access of Google's services without using the official API is probably not allowed, no matter if private or commerial use (you might get banned).

DataTables export to excel

I'm currently working on a project (developed using laravel, php, javascript, jquery, html), containing a large amount of data, so I'm using DataTables to display them with serverside set to true. What I'm trying to figure out is how to export the complete table to an excel file - right now it only saves one page (the one being shown at that very moment ) - using the Button extention.
I've been reading about it for a while now but still can't figure out a way to do this. I understand that, since serverside is set to true,
the only rows that exist on the client-side are those shown in the table at any one time.
But how can I get the complete table? Any help would be appreciated!
UPDATE:
So I create the excel file with the data I want in the backend, using PHPExcel, but now the problem is that it is saved server-side, while I wanna make it downloadable (client-side). From what I've been reading, I must add the appropriate headers to do so, but nothing I've tried works. Using
this, I managed to output the data of the excel in the screen, but it just shows gibberish... I should probably also mention that I'm new at this!

Batch file for inserting macro's

I am trying to create a batch file as icon on desktop, that would open a specific excel file, create a macro in it and copy code from a specific txt file into it.
The reason i need this, is that the file is located on a server and the ending .xlm cannot be cahanged (should not) so i cannot save it as a macro enabled file (to store a macro in it )
Could annyone with better understanding please explain to me how to create the said code ?
Thanks in advance!
In theory it's possible by using the VBE object (https://msdn.microsoft.com/en-us/library/aa443984%28v=vs.60%29.aspx). But that's not activated by default so you can not ensure that each user is able to do that.
Better find other solutions for that problem. Can you upload .XLSB for example? Or do you have another kind of server available - for example a database server - you can use for storing the file? We use this method here for deploying our updates on AddIns and report templates for example.

Workbook.SaveAs Sharepoint Integration

I have developed an Excel file that contains a button with a macro that will save the workbook for the user. This has worked well until recently when we are migrating to Sharepoint and users are trying to save the workbook to a mapped drive that is mapped to a Sharepoint location.
So, for example, my user has \\<server#SSL\sites\<sitename>\<documentlibrary> mapped to her Z drive.
When I use ThisWorkbook.GetSaveAsFilename the result returns a url like so https://<server#SSL\sites\<sitename>\<documentlibrary>\<filename>.xlsm
Before making any changes, I was getting a run-time error on the line
ThisWorkbook.SaveAs Filename:=excelFile
where excelFile is https://<server#SSL\sites\<sitename>\<documentlibrary>\<filename>.xlsm
My first attempt was to do some find/replace on the string. First replacing https: with nothing and then replacing the forward slash / with the backslash \ to make it resemble a file path like so:
\\<server#SSL\sites\<sitename>\<documentlibrary>\filename.xlsm
There was some progress made here. I received no VBA error, however I did get a message:
UPLOAD FAILED We're sorry this didn't work. Try saving again later
and if that still doesn't work, click Save a Copy
and I see the file attempting to be uploaded in the Microsoft Office Upload Center, so this is progress.
I can't seem to make it around this upload failure message.
I have tested that my permissions to the document library are sufficient by manually uploading the file that I am attempting to automatically upload.
So, how can I use ThisWorkbook.SaveAs to save a file to a Sharepoint document library?
I am using Excel 2013 and Sharepoint 2013.
While I cannot explain it, after some tinkering around, I believe I have found the solution.
Changing
ThisWorkbook.SaveAs Filename:=excelFile
To
ThisWorkbook.SaveAs fileName:=excelName, FileFormat:=xlOpenXMLWorkbook, ConflictResolution:=xlUserResolution
Seemed to make it work.
I explicitly set the FileFormat and ConflictResolution arguments and the user is able to save.

Opening xlsx file created with SpreadSheetGear

I have created a simple Excel file using SpreadSheetGear. If I save it as an xls file
workbook.SaveAs("file.xls", SpreadsheetGear.FileFormat.Excel8);
and attach it to an email, I can open it on my phone (tested both with iPhone and Android).
If I save it as an xlsx file
workbook.SaveAs("file.xlsx", SpreadsheetGear.FileFormat.OpenXMLWorkbook);
and attach it to an email, I CANNOT open it on my phone.
If I open the xlsx file attachment on my computer and save it with no changes and attach it to an email, I now can open it on my phone.
Apparently Excel saves the file differently than SSG. The file size of the xlsx file attachment is 9 KB. When I open it on my computer and save it, the new file size is 24 KB.
Some of my users prefer the xlsx format. Is there anything I can do with to make the SSG generated file attachment open like an Excel generated file attachement?
iOS depends on certain attributes being present in the worksheet data of the Open XML file format to properly parse these files. SpreadsheetGear does not write these attributes out because they are listed as optional in the Open XML file format specification and, also, omitting them reduces file size, as you have noted. Excel, for whatever reason, always writes out these optional attributes and other third-party components often times rely on their presence to function correctly. SpreadsheetGear V5 added a workaround to write out these attributes by enabling a certain "Experimental" option. This option was added because the OLE DB provider also exhibits this errant behavior. You might try something like the following and see if this helps in getting SpreadsheetGear to better work with your viewer:
IWorkbookSet workbookSet = Factory.GetWorkbookSet();
workbookSet.Experimental = "OleDbOpenXmlWorkaround";
IWorkbook workbook = workbookSet.Workbooks.Open(#"C:\temp\BadWorkbook.xlsx");
workbook.SaveAs(#"C:\temp\GoodWorkbook.xlsx", FileFormat.OpenXMLWorkbook);
Please see the SpreadsheetGear.IWorkbookSet.Experimental property for more information on this feature.
From what I can tell, iOS/Andriod/etc often also depend on other certain optional features available in the file formats that SpreadsheetGear either doesn't support or write out by default. For instance, iOS depends on a "data cache" stored within charts to display chart series data points and SpreadsheetGear's support for writing out this data cache is limited. This can result in charts not displaying as expected in iOS, Android, etc.

Resources