Writing and reading Excel files in C #

I am writing a program that takes data from a website through the selenium web driver. I am trying to create football equipment for our projects. So far, I have done to take the date and time, team names and scores from the website. Also still trying to write in a txt file, but it gets a little dirty when writing to a txt file

How to write to excel file and read? I want to write like this:

Date-Time     First-Team   Second-Team    Score    Statistics
28/07 19:00   AM           AVB            2-1      Shot 13123 Pass 65465 ...
28/07 20:00   BM           BVB            2-2      Shot 13123 Pass 65465 ...
28/07 20:00   CM           CVB            2-3      Shot 13123 Pass 65465 ...

And this is my part of my code:

StreamWriter file = new StreamWriter(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\Test" + "\\" + pathName + "\\" + subFile + "\\" + pathName + ".txt", false);

for (int k = 2; k > 1; k--)
{ 
     //doing some stuff
}

Writing Part:

for (int x = 0; x <dT.Count; x++)
{
     file.Write(dateTime[x] + " " + firstTeam[x] + " "
                       + secondTeam[x] + " " + firstHalf[x] + " " + secondHalf[x] + " ")
     for (int i = 0; i < total_FS.Count(); i++)
     {
           int index = total_FS[i].Length;
           if (total_FS[i][index-1].ToString() != " " && total_FS[i] != "-")
           {
                 file.Write(total_FS[i]);
           }
           else
           {
                 SpaceC++;
                 if (total_FS[i][index - 1].ToString() == " ")
                 file.Write(total_FS[i]);
           }
           if (SpaceC == 9)
           {
                 file.Write("\n");
                 SpaceC = 0;
                 break;
           }
      }


}
+6
source share
2 answers

, excel. / .

EPPlus .

Nopi

DocumentFormat.OpenXml , -, .

Open XML SDK 2.0 Microsoft Office / .

ClosedXML - OpenXML ClosedXML (.xlsx,.xlsm ..).

SpreadsheetGear * / Excel ASP.NET

+4

XLS CSV, Excel. , .

field11,field12
field21,field22

, .

"field11(row1,column1)", field12
field21, field22

, . CsvHelper . Nuget

PM > CsvHelper

, .

using(var textWriter = new StreamWriter(@"C:\mypath\myfile.csv")
{
    var writer = new CsvWriter(textWriter);
    writer.Configuration.Delimiter = ",";

    foreach (var item in list)
    {
        csv.WriteField("field11");
        csv.WriteField("field12");
        csv.NextRecord();
    }
}

.

+3

All Articles