C# File open / convert and save - c#

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.

Related

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.

How I can save a .xlw format on excel with c#

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",....);

Convert xlsx to xls issue

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

Making a pdf copy of a csv file programmatically in C#

I have a csv file and I need to make a pdf copy in the console application that creates the csv. All I have found 3rd party libraries for working with pdf but I don't want to use that. Can I just make a copy and save it as a pdf? If so, how?
Also, I'm using C#, in case you didn't read the title.
Try the below link hope it will help you.
http://sourceforge.net/project/platformdownload.php?group_id=72954
You can open the csv with excel.
than use office PIA (interop) to save as PDF.
see:
Can I use Microsoft Office 2010: Primary Interop Assemblies to convert word,excel to PDF
first read the csv files using c# code, and load that data's into DataTable,
write the datatable values into pdf file using c#
may this idea will help 4 u

Creating Excel File

Well, I didn't find any libs to create Excel file in Windows Phone 7 and the default libs for Excel are not working because they weren't compile for it.
Does any of you guys know how to do this?
Excel is able to open many different kinds of files beyond the .xls or .xlsx. Most common is CSV; it's dead simple but not very capable, and I would avoid it for all but the simplest applications.
A format I've used successfully is the Symbolic Link (SYLK) format. The .slk files open directly in Excel, and you can include cell formatting and formulas. It's easy to save out a file from Excel itself and use it as a template for creating your own files.
You're going to struggle to find a library to do this simply because WP (as of 7.1) doesn't include the System.IO.Packaging namespace, which most libraries will depend on to read/write docx/xlsx/etc files.

Categories

Resources