简体   繁体   中英

reading xlsx with multi sheets into lists c#

I have a code where i read a file ( .csv ) and store its columns in lists.

var pathskill = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory.ToString(), "skill.csv");

using (var fs1 = File.OpenRead(pathskill))
using (var reader1 = new StreamReader(fs1))

while (!reader1.EndOfStream)
{
    var line = reader1.ReadLine();
    var values = line.Split(',');

    list_MainId.Add(Convert.ToDouble(values[0]));
    list_MainName.Add(values[1]);
    list_AmountMade.Add(Convert.ToInt32(values[2]));
    list_Level.Add(Convert.ToDouble(values[3]));
    list_Exp.Add(Convert.ToDouble(values[4]));
    list_MadeFrom_One_Id.Add(Convert.ToDouble(values[5]));
    list_Amount_MadeFrom_One.Add(Convert.ToInt32(values[6]));
    list_MadeFrom_Two_Id.Add(Convert.ToDouble(values[7]));
    list_Amount_MadeFrom_Two.Add(Convert.ToInt32(values[8]));

}

This code works great and i get 9 lists with values.

However, i have many .csv files and i think it will be better that each will be like a sheet in xlsx file and i can choose which one to read by its name.

For example to have a sheet called skill1 , skill2 and so on.

Is there a way to read a specific sheet from xlsx by its name and store it columns into lists?

Thank you

There are a lot of ways to do this. You could unzip xlsx files and read sheet XMLs directly, or you may use some library that can work with xlsx (eg EPPLUS that is freeware).

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.

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