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
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));
}
}
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.