简体   繁体   English

在C#中编写和读取excel文件

[英]Writing and Reading excel files in C#

I am writing a program that takes data from a website via selenium web driver. 我正在编写一个程序,通过selenium web驱动程序从网站获取数据。 I am trying to create football fixture for our projects. 我正在尝试为我们的项目创建足球装备。 I am so far, I accomplished to take date and time, team names and scores from the website. 到目前为止,我完成了从网站上获取日期和时间,团队名称和分数 Also still trying writing on txt file, but it gets little messy while writing on txt file 还在尝试在txt文件上编写,但在txt文件上写入时会变得很乱

How do I accomplish writing on excel file, and reading? 如何在excel文件上完成写作并阅读? I want to write like that 我想这样写

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;
           }
      }


}

There are few cool libraries that you can use to easy read and write excel files. 您可以使用很少的酷库来轻松读取和写入Excel文件。 You can reference them in your project and easily create/modify spreadsheets. 您可以在项目中引用它们,并轻松创建/修改电子表格。

EPPlus Very developer friendly and easy to use. EPPlus非常开发人员友好且易于使用。

NOPI NOPI

DocumentFormat.OpenXml It provides strongly typed classes for spreadsheet objects and seems to be fairly easy to work with. DocumentFormat.OpenXml它为电子表格对象提供强类型类,并且似乎相当容易使用。

Open XML SDK 2.0 for Microsoft Office Provides strongly typed classes / easy to work with. 适用于Microsoft Office的Open XML SDK 2.0提供强类型类/易于使用。

ClosedXML - The easy way to OpenXML ClosedXML makes it easier for developers to create (.xlsx, .xlsm, etc) files. ClosedXML - OpenXML的简便方法 ClosedXML使开发人员可以更轻松地创建(.xlsx,.xlsm等)文件。

SpreadsheetGear *Paid - library to import / export Excel workbooks in ASP.NET SpreadsheetGear *付费 - 用于在ASP.NET中导入/导出Excel工作簿的库

Instead of creating XLS file, make a CSV text file that can be opened with Excel. 创建可以使用Excel打开的CSV文本文件,而不是创建XLS文件。 Fields are comma separated and each line represents a record. 字段以逗号分隔,每行代表一条记录。

field11,field12
field21,field22

If a field contains inner commas, it needs to be wrapped in double quotation marks. 如果字段包含内部逗号,则需要用双引号括起来。

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

If a field contains double quotation marks, they need to be escaped. 如果字段包含双引号,则需要对其进行转义。 But you can use CsvHelper to do the job. 但是你可以使用CsvHelper来完成这项工作。 Grab it from Nuget 从Nuget抓住它

PM> Install-Package CsvHelper PM> Install-Package CsvHelper

An example on how to use it. 关于如何使用它的一个例子。

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();
    }
}

Full documentation can be found here . 完整的文档可以在这里找到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM