Answer the question
In order to leave comments, you need to log in
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;
}
}
Answer the question
In order to leave comments, you need to log in
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);
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; }
}
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());
foreach(var q in qs)
{
Console.WriteLine($"{q.Question}");
Console.WriteLine($"{q.Theme}");
Console.WriteLine($"{q.Description}");
Console.WriteLine(new string('-', Console.WindowWidth));
}
Индейцы в знак примирения хлопали в ладоши
history
Они закапывали топор войны
----------------------------------------------------------------------------------------------------
Моряки пропитывали свою одежду смолой, чтобы она не рвалась
history
Чтобы она не пропускала воду
----------------------------------------------------------------------------------------------------
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question