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

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.

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.

Converting any file to .xlsx leads error in opening it

I am saving as any file to .xlsx format. If the original file is not with .xlsx extension, it is throwing exception while trying to open it. The exception message is :-
Excel cannot open the file 'abc.xlsx because the file format or file
extension is not valid. Verify that the file has not been corrupted
and that the file extenstion matches the format of the file.
Whereas if conversion is in .xls format, I can open the converted file with warning message.
The file you are trying to open, abc.xls, is in a different format
than specified by the file extension. Verify that the file is not
corrupted and is from a trusted source before opening the file. Do you
want to open the file now?
I need to convert file to .xlsx format by C# code regardless its extenstion and open it by Excel 2010.
Thats not how file-types work!
You can not simply rename a file and it gets converted to a different type.
Renaming *.xls to *.xlsx works, because both are Excel files which can be opened by MS Excel, but for all other types (except some other which Excel can handle, like e.g. *.csv) you need to read the file and "manually" convert them.
To write *.xlsx using C# you can use e.g. EPPlus (NuGet).
You need a library that convert the files for you.
I see that you need to open files from Office 2003, so you need to use something like NPOI
Unfortunately, even if EPPLUS is a great library for Office's files, it only support the OXML Documents like .xlsx or .docx but not the .xls.
NPOI is a free opensource library to work with Office 2003->2010/3 files.
Here is the link

How to convert excel files (xslx files) to pdf in the sharepoint web part?

I am developing sharepoint web part. I have customized sharepoint server ribbon. Sharepoint server ribbon now includes one button. On clciking on this button I am downloading the xlsx files in sharepoint library to one of my local drive. Now I want to convert this downloaded xlsx file into pdf file. Then I want to upload this pdf file to one document library in sharepoint. For customizing the server ribbon and handling the button postback I am using the following link in which code sample is provided for download
Customizing and Extending the SharePoint 2010 Server Ribbon
The code sample is working fine for me. I have dowloaded the xlsx files to local folder. Now I want to convert this downloaded xlsx files which are on local folder to pdf files So I am using the following link
Convert xlsx file to pdf file
The code sample given in the above link is also working file for me in console application. I am able to convert xlsx files to pdf files in console application.But when I done the copy and paste of the code into the sharepoint project it is not working. I am not able to understand why the same code is not working in sharepoint project ? The code is as follows
SautinSoft.UseOffice u = new SautinSoft.UseOffice();
//Path to any local file
string inputFilePath = "D:\\a.xlsx";
//string inputFilePath = "http://shailesh-pc/TemplateInvoice/Template.xlsx";
//Path to output resulted file
string outputFilePath = "D:\\afadfasfd.pdf";
//string outputFilePath = "http://shailesh-pc/Invoice/aa.xlsx";
//Prepare UseOffice .Net, loads MS Excel in memory
int ret = u.InitExcel();
//Return values:
//0 - Loading successfully
//1 - Can't load MS Excel library in memory
if (ret == 1)
return;
//Converting
ret = u.ConvertFile(inputFilePath, outputFilePath, SautinSoft.UseOffice.eDirection.XLSX_to_PDF);
//Release MS Excel from memory
u.CloseExcel();
//0 - Converting successfully
//1 - Can't open input file. Check that you are using full local path to input file, URL and relative path are not supported
//2 - Can't create output file. Please check that you have permissions to write by this path or probably this path already used by another application
//3 - Converting failed, please contact with our Support Team
//4 - MS Office isn't installed. The component requires that any of these versions of MS Office should be installed: 2000, XP, 2003, 2007 or 2010
if (ret == 0)
{
//Show produced file
System.Diagnostics.Process.Start(outputFilePath);
}
In the sharepoint project I am getting the value 0 at int ret = u.InitExcel(); But I am getting the value 1 at line ret = u.ConvertFile(inputFilePath, outputFilePath, SautinSoft.UseOffice.eDirection.XLSX_to_PDF);in the sharepoint project. Why this is happening. Is there any probelm with sharepoint project so that they are not able to convert xlsx files to pdf ? Can you please tell me what should I need to do to convert xlsx files in pdf in sharepoint? Can you please provide me any code or link through which I can convert the xlsx files to pdf files ?
Please note that Microsoft doesn't recommend Office automation in such scenarios:
Microsoft does not currently recommend, and does not support,
Automation of Microsoft Office applications from any unattended,
non-interactive client application or component (including ASP,
ASP.NET, DCOM, and NT Services), because Office may exhibit unstable
behavior and/or deadlock when Office is run in this environment.
You may check further details in the following topic: http://support.microsoft.com/kb/257757. So, it is better to consider alternatives.

XlFileFormat for excel 2010 file

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.

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