简体   繁体   English

用多张纸将xlsx读入列表C#

[英]reading xlsx with multi sheets into lists c#

I have a code where i read a file ( .csv ) and store its columns in lists. 我有一个代码,我在其中读取文件( .csv )并将其列存储在列表中。

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. 这段代码很好用,我得到了9个带有值的列表。

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. 但是,我有许多.csv文件,我认为每个文件都像xlsx文件中的一张纸会更好,并且我可以选择要读取的文件名称。

For example to have a sheet called skill1 , skill2 and so on. 例如,有一个名为skill1skill2等的工作表。

Is there a way to read a specific sheet from xlsx by its name and store it columns into lists? 是否可以通过名称从xlsx读取特定工作表并将其列存储到列表中?

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). 您可以解压缩xlsx文件并直接读取工作表XML,或者您可以使用一些可用于xlsx的库(例如,免费的EPPLUS)。

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

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