Convert xlsx to xls issue - c#

I am trying to convert a .xlsx file to .xls and it seems to be working fine, however when I open the .xls file I get a warning message "
The file you are trying to open 'filename', is in a different format
than specified by the file extension.
Verify that the file is not corrupted and >from a trusted source before opening the >file. Do you want to open the file now?"....
Everything looks fine when I open it, but I do not understand why this is happening. My next step in this program is to import the data from the xls into SQL, but I am afraid this will cause issues for that.
Here is the line of code where I call the SaveAs method to change the file extension of the file.
wb.SaveAs("filename.xls", FileFormat: Microsoft.Office.Interop.Excel.XlFileFormat.xlOpenXMLWorkbook);
I initially just had
wb.SaveAs("filename.xls");
and after I got the error I did some more digging around and found the xlOpenXMLWorkbook, but does not seem to help.
Any info that could help me understand why this is happening would be much appreciated.

To save as spreadsheet (OpenXml Format, .xlsx), use XlFileFormat.xlOpenXMLWorkbook:
wb.SaveAs("filename.xlsx", FileFormat: Microsoft.Office.Interop.Excel.XlFileFormat.xlOpenXMLWorkbook);
To save as Excel 1997-2003 format (Biff, .xls), use XlFileFormat.xlExcel8:
wb.SaveAs("filename.xls", FileFormat: Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel8);
Make sure you set the appropriate extension (.xlsx or .xls), otherwise, you have the error you described.
See also my answer here Excel Interop Save as is giving compatibility checker in excel 2007

Related

Modifying Excel Sheet through C# without Interop.Excel

I know questions like this are around in stack and there are 3rd part libraries to do the trick but none of them is fixing my issue at the moment. So the issue.
I have an Excel workbook (.xlsx) with multiple sheets generated by another system. I have to read the data from this via SSIS and dump it to a SQL DB.
Now the issue is although the Excel sheet contains data and when I open manually it opens without any error and the data displays when I use a script task and use OLEDB connection to connect to the excel and open it up the connection is made successfully but when reading data the column names are not picked (I get F1, F2 likewise) and no data rows are read. I simply get a blank row and that's about it. I have tried with HDR= YES and NO and IMEX=1 and 0 but always the result is same.
Funny thing is if I open the excel sheet do some modification (like change a sheet name save and change back the sheet name and save and close) and after that I try to run the package the data gets picked without any issue (also I noticed that the file size increases from 164KB to 196KB). Now because of this what am trying to do is modify the the file a bit and save via code.
So the initial step I tried was through using Office.Interop.Excel and it works like a charm in my machine but on the server NO OFFICE so IT NO WORKS. And nope the IT guys are never going to install access engine or excel or anything there.
Then I tried via OpenXML and 3rd party library like NPOI and even via OLEDB connection to modify the file. in both NPOI and OLEDB methods the file got changed but still it didn't get picked up properly by the SSIS package (I noticed that the file size didn't change and remained at 164kb). In OpenXML it wasn't able to open the file and threw an error saying "the document cannot be opened because there is and invalid part with an unexpected content type".
So right now I am stuck with no proper method in sight and would appreciate any help in solving this either through c# code or any other SSIS method available. SSIS version am using is 2008.
Edit 1
So I noticed that the script task is able to read the data from the first sheet out f the multiple sheets but the other sheets are the problem. So somewhere the xml for these sheets are broken. Anyway I can copy the xml configs of the first sheet to other ones? Just a thought...
Edit 2
So the first sheet is of ContentType "application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml" while all the other sheets are of ContentType "application/xml"
Ultimately ended up using two libraries for this. The data was read without an issue by using exceldatareader (http://exceldatareader.codeplex.com/). Using this the data was read into a dataset easily and then it was written to a new Excel file using epplus (http://epplus.codeplex.com/).
After that when the new excel file was read via the SSIS package data got picked without an issue. Hope this will help someone out there.

C# File open / convert and save

I am very new to C# and hoping this is a simple question. Not finding what I need on google
I have a file C:\test\losses.csv
That I want to open up then convert to an xlsx file and save in a different directory.
Save to
C:\test\Losses.xlsx
The reason for opening the file is the move command does not convert it to xlsx, unfortunately it keeps the same structure as the csv and is unusable in that format.
File.Copy(#"C:\test\losses.csv", #"C:\test1\Losses.xlsx");
The above code works great but still is a csv file (well really a hybrid of one). That is another SAP story.
Any help will be greatly appreciated. Thanks
File.Copy only copies the file - similar to copying a file in DOS or in windows file explorer.
You'd need to translate your CSV to an XLSX file. The format should be pretty straight-forward, but you'll need to do more research:
Load the CSV as a data table
Use the Excel.XlFileFormat.xlOpenXMLWorkbook class to translate the file.
A different StackOverflow problem addresses how to use the xlOpenXMLWorkbook:
Exporting to .xlsx using Microsoft.Office.Interop.Excel SaveAs Error
Hope this helps. Good luck.

Creating excel - improve performance

Good afternoon,
we have a small problem with performance of generating excel.
First, we was creating excel cell by cell - it is ... let's say unacceptable.
Second, we started insert into excel with one command - range creating and it is much faster, but still not perfect so we are searching next solutions.
Because we can load XML file from database, we tried used XSLT and from these two files create xls file. It is nice, but after open this file there is error message shown (it is because of problem or bug in registry). User has to accept this message and after excel is opened. We want to eliminate this error message. However we don't know how.
We was thinking about convert this xls file into xlsx but we are unable to do it becouse we can't install office on server (we cannot use Interop) and OpenXML libraries don't know work with normal xls file. So my question is:
Is possible to generate from XML file with using of some XLST (or something) the xlsx file?
Eventually can what files do we need to create and zip together if we want create xlsx file?
Thank you for information
You mention not being able to use the OpenXML libraries because they don't work with .xls files, but you also say "creating cell by cell", which implies that you are generating the file from scratch. Where is the xls file coming from? You mention excel opening, but then say you can't install it on the server. So, it appears to me that a user is uploading an xls file to your server, and then you are doing something with it and giving it back to them? If that is the case and you must be able to read/write an xls file without installing office, then I would suggest using ExcelLibrary, as mentioned in this post
Indeed, creating an xlsx file is much magnitudes faster with the open xml sdk.

Save .xls file into .txt file in C#.Net

How to save (in fact Save As) .xls file into a text file from C#. I tried File.Copy() but I am unable to see data. I am getting some special characters in the text file.
Any help would be appreciated.
You should use the Microsoft.Office.Tools.Excel.Workbook.SaveAs() method

Is it possible to output .XLSX file with XSLT in ASP.NET Response

I currently have a .xsl file which I am using XSLT with to output it as a .xls file and open it with excel.
I am outputting this in an ASP.NET application using the response object as follows:
context.Response.ContentType = "application/vnd.ms-excel";
context.Response.AddHeader("content-disposition", "inline;filename=MyFile.xml");
However, this outputs the file as a .xsl extension and displays the following popup message:
"The file you are trying to open, 'MyFile.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 now?"
I am trying to find out two things, is it possible to output this as a .xlsx extension? Because when I try to do that and change the content type it simply doesn't allow me to open the file until I set the extension back to .xls.
The second, is how do you get rid of that message by making changes to the code? I have found a few responses that simply instruct you to change settings such as here: http://support.microsoft.com/kb/948615
However the client requests the error message not be displayed at all. Any help is appreciated here, thanks.
Don't use XSLT to try to produce an XLSX file, because it's not a simple XML file, as someone has said. You can use NPOI to create pure XLS files, ExcelPackage or ClosedXML to produce XLSX files. More work, but it's actually doable.
I found some interesting information out about how the .XML file extension can be handled. It seems that you can flag an XML file to be associated with Excel, which will cause that individual XML file to be opened with Excel by the Microsoft XML Editor.

Categories

Resources