Reputation: 33
I'm trying to save the results of my computations concerning the analysis of text file to excel file. for each parsed file I want to create an excel file with a word on the first line of text for each cell (I already have this data) and lines below the values calculated by my program.
I read the previous posts by various methods to interact with Excel from C # but I did not understand the characteristics of each.
Thanks and regards
Alessandro
Upvotes: 1
Views: 1181
Reputation: 5489
There are various ways to work with Excel files in C#.
First two options are well described on csharp.net-informations.com site
If you don't need to perform any fancy Excel operations from your code I would recommand using third approach - write data to CSV file. It can be open in excel and it is easy to create CSV file.
You can use something like:
using(TextWriter tw = new StreamWriter("sample.csv") {
// Header
tw.WriteLine("X,Y");
foreach(var item in Data) {
tw.WriteLine(item.X.ToString(InvariantCulture) + "," + item.Y.ToString(InvariantCulture));
}
}
Upvotes: 1
Reputation: 4084
Besides using common 3-rd party libraries, the most fastest and easiest way of 'creating' excel files is to output them as html tables. Excel is able to import such.
<table>
<tr><td>my col1</td><td>my col 2</td></tr>
<tr><td>data1</td><td>data2</td></tr>
...
</table>
Store the file with an extension of 'xls'. The scheme is - of course - somehow unsafe. More complex scenarios would better utilize some sort of 'typesafe' library. But for most "we just need to export it as excel file" scenarios, this is sufficient. One can even format the tables via CSS commands and handle different encodings.
Upvotes: 0