XlFileFormat for excel 2010 file - c#

I want to create a excel 2010 file from C#
so what will be the XlFileFormat Enumeration for it.
http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.xlfileformat.aspx

xlOpenXMLWorkbook for a .xlsx macro free workbook.
xlOpenXMLWorkbookMacroEnabled for a .xlsm macro enabled workbook.
The key piece of knowledge is that the new Office 2007 file formats are called Office Open XML.

Related

Save ole file as office document

I have some ole file with ole2 format in legacy system.
These are office word or excel & with embed object (e.g. picture) I think.
If I rename the file with docx or xlsx externsion, it will say file is corrupted.
Could I extract the ole file with some existing C# library? And save it as word or excel document?
OLEFileStructure PNG
NOTE:
The OlePres\d\d\d stream are embed ole object I think.
The Ole stream says it's a embed file not link.
The compObj stream indicate it's file type. e.g. Microsoft Word Document
For package type ole file, I have follow below blog to extract the file from ole10native stream successfully -- https://eigenein.wordpress.com/2011/08/03/how-to-extract-ole-attachment-body-from-ole10native-stream/
Updates: (Possible solution)
For old style, e.g. xls, doc, it could just rename the ole file to those extension and it works. But some of the file cannot be opened via MS Office, but it open successfully via Libre Office.
For new style, e.g. xlsx, docx. It could extract the Package stream and save as xlsx or docx. file.
For old style, e.g. xls, doc, it could just rename the ole file to those extension and it works.
But some of the file cannot be opened via MS Office, but it open successfully via Libre Office. So I use the Libre office command line tool to convert it with same format, e.g. soffice --convert-to docx *.docx --outdir ../Converted
Then it could be opened via MS Office.
For new style, e.g. xlsx, docx. It could extract the Package stream and save as xlsx or docx. file.

Can I build the vsto project in excel file

I have faced the following problem: if I write an extension in Visual Studio for Excel file, it saves as the .vsto file in the same location and then uploads in the Excel file on run. But I need .vsto file to be automatically uploaded, when the Excel file starts to execute, and not visible to a person, to whom I will pass the excel file.
Is there any way to do this? I want to place USB-key checking code in the VSTO workbook project (Excel file will be unprotected and opened only if there is a USB-key in the USB-port)
In case you need further clarifications, please do not hesitate to ask in this thread
There is no support in VSTO for embedding your code inside an Excel workbook.
The easiest way of embedding code in a workbook is by using VBA.
Otherwise you would need to find a way of forcing your VSTO code and the workbook to be in the same location.

How will I open xlsx File in C#.NET?

I want to open xlsx File in C#.NET, but it shows error .
But if the file is xls extension then easily i can open it but when file format is xlsx then shows error.
My code is
oXL.Workbooks.Open(Path, 0, false, 5, "", "", false,
//Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0)
The exception is:
Excel cannot open the file 'New Microsoft Excel Worksheet.xlsx'
because the file format or file extension is not valid. Verify that
the file has not been corrupted and that the file extension matches
the format of the file.
Workbooks.Open will fail if you have a version of Excel in your machine that can't read xlsx files (eg Excel 2003).
If you don't need actually Office Interop (so if you just need to read and write files and not use Excel functionalities) then you should take a look at the Office Open XML SDK (v2.5 for .Net 4.5 and v2.0 for .Net 3.5) at the official download site.
You're then able to open an excel file like this:
SpreadsheetDocument ExcelDocument = SpreadsheetDocument.Open(FileName, false);
and perform read and write operations.
To be clear:
The Office Open XML SDK allows for read- and write operations on an Excel (and other Office XML) files, but if you require MS Excel do perform calculations or macros, then this will not solve your issue.

Running Excel macro in a specific workbook fails from C#

I'm trying to run a macro in an Excel workbook from C# using the standard Microsoft.Office.Interop.Excel library. In my scenario the version of Excel could be either 2010 or 2013.
There could be multiple workbooks open for the same Application object, each potentially with the same macro names, so I'm explicitly passing in the workbook name
string macro = string.Format("{0}!{1}", workbook.Name, macroName);
Application.Run(macro);
Weirdly, I get a COMException if the workbook name has hyphens in it:
Application.Run("My-Workbook!macro1")
But runs fine without hyphens:
Application.Run("MyWorkbook!macro1")
Is it just me or is this weird? Can anyone think of a workaround?
Wrap the workbook name in quotes
string macro = string.Format("'{0}'!{1}", workbook.Name, macroName);
Application.Run(macro);

Programmatically finding an Excel file's Excel version

I'm using an OleDbConnection to connect to a spreadsheet from a C# program. One of the parameters in the connection string is the Excel version.
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Book1.xls;Extended Properties="Excel 8.0;HDR=YES"
Given the path of an Excel file how can I find out which Excel format version it uses?
Thanks in advance,
T.
I addition to what was said and apart from using Excel automation to open the file you can try reading file version from your code:
xls files: those are saved as structured storage. you can use the technique from the article here: How To Determine Which Version of Excel Wrote a Workbook
xlsx files: you can open them as zip files. Version is in app.xml file AppVersion field.
Download OLE File Property Reader. Register dsofile.dll with regsvr32 and add it as a reference in your application. The following code will output the type of Excel file. It will not work on xlsx/docx since those are not OLE compunds object files, but should work on all older Office formats (2007/2003/XP).
var doc = new OleDocumentPropertiesClass();
doc.Open(#"c:\spreadhseet.xls", false, dsoFileOpenOptions.dsoOptionDefault);
Console.WriteLine(doc.OleDocumentType);

Categories