K
K
kititnik12021-03-28 11:20:35
excel
kititnik1, 2021-03-28 11:20:35

How to import excel spreadsheet into c#?

I have an Excel spreadsheet and there is such a simple program

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Questions[] q;


        }
    }
    class Questions
    {
        public string question;
        public string theme;
        public string description;
        public string questionEn;
        public string descrEn;
    }
}

How can I, for example, transfer all the data from column A to the variable question, B - theme, C - description?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
Foggy Finder, 2021-03-28
@kititnik1

There are many libraries for working with Excel. Which one to choose depends on the task (for example, you only need to read the data or you need to generate a full-fledged report, with styles, and so on).
Judging by the description, here you only need to read, and with support for the outdated xls format , so I recommend the ExcelDataReader
library. If you have a .Net Core application, do not forget to add the line

Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

And now an example.
1. In C#, it is not customary to use public fields for classes, so let's rewrite them as properties
public class Questions
{
    public string Question { get; set; }
    public string Theme { get; set; }
    public string Description { get; set; }
    public string QuestionEn { get; set; }
    public string DescrEn { get; set; }
}

2. Reading itself
const string FilePath = "sample.xls";
var qs = new List<Questions>();

using var stream = File.Open(FilePath, FileMode.Open, FileAccess.Read);
using var reader = ExcelReaderFactory.CreateReader(stream);
do
{
    while (reader.Read())
    {
        var question = reader.GetString(0);
        var theme = reader.GetString(1);
        var description = reader.GetString(2);

        qs.Add(new Questions()
        {
            Question = question,
            Theme = theme,
            Description = description
        });
    }
} while (reader.NextResult());

3. Displaying the result
foreach(var q in qs)
{
    Console.WriteLine($"{q.Question}");
    Console.WriteLine($"{q.Theme}");
    Console.WriteLine($"{q.Description}");
    Console.WriteLine(new string('-', Console.WindowWidth));
}

4. Launch, check
Индейцы в знак примирения хлопали в ладоши
history
Они закапывали топор войны
----------------------------------------------------------------------------------------------------
Моряки пропитывали свою одежду смолой, чтобы она не рвалась
history
Чтобы она не пропускала воду
----------------------------------------------------------------------------------------------------

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question