I'm using Open XML to open an excel file as in:
using (SpreadsheetDocument myWorkbook = SpreadsheetDocument.Open("generated.xlsx", true))
...
but I cannot figure out how to actually launch excel and show the file through code.
Thanks for any help.
Take a look at either one of these answers:
how to create and download excel document using asp.net
OpenXML file download without temporary file
Related
It is possible to manually save an Excel spreadsheet as a "Strict Open XML" file type in Excel when using "Save as" instead of saving it as the default workbook OOXML file, which is a "Transitional" variant of the OOXML standard. The extension is .xlsx for both Strict and Transitional file format variants of the OOXML standard.
How can I do the same thing programmatically through automated workflows in e.g. C#? The purpose is to bulk convert Transitional Excel files to Strict Excel files.
I have found these code snippets part of the Office XML SDK:
https://docs.microsoft.com/en-us/dotnet/api/documentformat.openxml.spreadsheet.conformanceclass?view=openxml-2.8.1
https://docs.microsoft.com/en-us/dotnet/api/documentformat.openxml.spreadsheet.workbook.conformance?view=openxml-2.8.1#documentformat-openxml-spreadsheet-workbook-conformance
Can I use them or other ways of doing bulk conversion from Transitional to Strict?
UPDATE: I am in dialogue with the developers of Open XML SDK in this issue.
The only way possible, that I have found after extensive research is to use Excel.Interop and have your Excel installation handle the conversion in the background. It is not a pretty solution, because it is Excel dependent, but it is a programmatic approach.
You can find the Excel.Interop package here: https://www.nuget.org/packages/Microsoft.Office.Interop.Excel
I had an issue, where the package would not find my Excel installation, so I had to make a new reference to the Excel Interop DLL on your computer.
Add reference > Browse > C:\Windows\assembly\GAC\Microsoft.Office.Interop.Excel and pick the DLL from the folder in here. I also added the DLL from the subfolder in this path C:\Windows\assembly\GAC_MSIL\office
Conversion code
using Excel = Microsoft.Office.Interop.Excel;
void Convert_Transitional_to_Strict(string input_filepath, string output_filepath)
{
Excel.Application app = new Excel.Application(); // Create Excel instance
app.DisplayAlerts = false; // Don't display any Excel prompts
Excel.Workbook excelWorkbook = app.Workbooks.Open(input_filepath); // Create workbook instance and open Excel Workbook for conversion
excelWorkbook.SaveAs(output_filepath, 61); // Save file as .xlsx Strict
excelWorkbook.Close(); // Close the Workbook
app.Quit(); // Quit Excel Application
}
PS. If anyone finds a programmatic approach using Open XML SDK or another C# framework, do still post your answer.
I'm writing a excel AddIn to load and save from json files.
I'm doing this because I didn't find any solution to have merge-able & diff-able in git.
so far, I can write a ribbon extension with 2 buttons, "load json" and "save json". this works.
But I dont like the ribbon dependency. I would like my new file format (.xlsx.json) to be added to the list of supported file format in the open file dialog.
I know that Application has FileConverters property, but it is read only.
is there a way to write my own custom FileConverter for excel?
btw, I'm using Microsoft.Office.Interop.Excel in C#
I have created an embedded pdf with the insert> object > create from file> browse > display as icon function in excel.
I would like to then use the embedded pdf as an attachment for my outlook email using vba code. I have tried to use the .Attachment.Add code but it seems to fail to detech an embedded object.
Could anyone advise a correct code? Thanks!
Get the file from the source?
I don't understand why you would need to embed the.PDF object in the workbook, if you're going to be emailing it separately anyhow...
Regardless, you could just grab the actual/original .PDF to attach a copy to the email, directly from the same location from where it was embedded. (If it's not there, what happened to it?)
Another option:
As soon as you right-click the embedded object, Excel 2016 "gets ready" for you to open it by extracting it to your local temp folder. (I'm unsure whether this applies to previous versions.)
Therefore, you could programmatically right-click the embedded icon, and then check the temp folder located at the path that you'll find stored in Environ("temp"). One or more copies of your file will be located there (and it should be the 'newest' PDF).
Yet another option:
Excel's XLSM file is simply a compressed ZIP file, if you change the extension. You could programmatically make a copy of the file, changing it's extension to .ZIP.
Embedded object are stored as .BIN files within the ZIP file in the xl\embeddings\ folder. It would have to be extracted and then renamed back to a PDF. Note that this method is a little flakey and won't work with all PDF's.
More Information:
VBA Express : Save embedded PDF file as a separate PDf file
How-to-Geek : How to Extract Images, Text, and Embedded Files from Office Documents
I code an asp file to response an excel file using Excel.Application object. I change the content type below.
Response.ContentType = "application/vnd.ms-excel"
When user downloads this file and open it, he get the following message.
The file format and extension of 'test.xls' don't match. The file
could be corrupted or unsafe. Unless you trust its source, don't open
it. Do you want to open it anyway?
I don't want to give this message to use. How can I remove this message from excel file.
This is due to a feature in MS Excel called Extension Hardening. There are 2 ways to fix this problem
You need to generate proper XLS files that are truly excel files, not HTML Tables saved with XLS extension
Each of the client machines need a registry tweak as explained here
I am using EPPlus open source tool for open excel files on server.
When I try to open it using EPPlus some time I am getting error. Then I download it to local machine & when I open it using MS Excel I got error like excel found unreadable content Do you want to recover workbook?
I repair it using MS Excel again upload it and then it start working correctly.
What is actual problem?
Can I reapair it using EPPlus itself?
Try with new version of EPPlus
EPPlus 4.0 fixed mostly bugs
Save() and Dispose() created instance of excelPackage for better performance