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
Related
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.
Is it possible to convert a .csv file to .xlsb programatically ?
(Preferrably C#)
Convert XLS to XLSB Programatically?
shows how to convert XLS to XLSB , can I use the same to convert it from.csv? Won't there be any formatting issues?
I am doing this on the server side because the .xlsb file size is very small compared to a .csv file, and I don't have an option of zipping my file.
Take a look at EPPlus. I have used this extensively with great success. Fantastic Excel library and it's open source.
And, yes, there certainly will be formatting issues. You cannot go from XLS to CSV and maintain any of the original formatting because .CSV is pure text.
Aspose.Cells File Format API can convert your CSV file to XLSB format. Please see the following sample code and screenshots for your reference.
C#
// Directory path for input and output files.
string dirPath = "D:\\Download\\";
// Load your sample CSV file inside the Workbook object.
Aspose.Cells.Workbook wb = new Aspose.Cells.Workbook(dirPath + "sampleConvertCSVToExcelFormats.csv");
// Save CSV file to XLSB format.
wb.Save(dirPath + "outputConvertCSVToExcelFormats.xlsb", SaveFormat.Xlsb);
Screenshots of Sample CSV and Output XLSB files
Sample CSV File:
Output XLSB file
Thank you and regards.
Well I have a little problem.
I want to save a excel file in C# which format is xlw, but I can`t.
Then I have a idea. I try to save this file in xlsx and after change the format to .xlw, but I don't know how I do this.
Anybody can help me please ?
Thanks!
I try this :
objExcelworkbook.SaveAs("abc.xlw",....);
or
objExcel.ActiveWorkbook.SaveAs("abc.xlw",....);
And the file don't save with this name "abc.xlw".
So, I explain my problem:
I have a mini program that copy different excel files, but the file extension .xlw doesn't copy right. In the other hand, the file extension .xls, save right.
Try this:
objExcelworkbook.SaveAs("abc.xlw",....);
or
objExcel.ActiveWorkbook.SaveAs("abc.xlw",....);
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
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.