read an xslx file and convert to List

拜拜、爱过 提交于 2019-12-25 05:06:19

问题


I have a xslx file with following data

    www.url.com
    www.url.com
    www.url.com
    www.url.com
    www.url.com
    www.url.com
    www.url.com
    www.url.com
    ...

Like you can see I have only 1 column used and a lot of rows. I need to read that column from the xslx file somehow and convert it to List<string>.

Any help?

Thanks!


回答1:


You can use EPPlus, it's simple, something like this :

  var ep = new ExcelPackage(new FileInfo(excelFile));
  var ws = ep.Workbook.Worksheets["Sheet1"];

  var domains = new List<string>();
  for (int rw = 1; rw <= ws.Dimension.End.Row; rw++)
  {
    if (ws.Cells[rw, 1].Value != null)
     domains.Add(ws.Cells[rw, 1].Value.ToString());
  }



回答2:


the easiest way is to use OleDb, you can do something like this:

List<string> values = new List<string>();

string constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\your\\path\\file.xlsx;Extended Properties=\"Excel 12.0 Xml;HDR=NO;\"";
using (OleDbConnection conn = new OleDbConnection(constr))
{
    conn.Open();
    OleDbCommand command = new OleDbCommand("Select * from [SheetName$]", conn);
    OleDbDataReader reader = command.ExecuteReader();
    if (reader.HasRows)
    {
        while (reader.Read())
        {
            // this assumes just one column, and the value is text
            string value = reader[0].ToString();
            values.Add(value);
        }
    }
}

foreach (string value in values)
    Console.WriteLine(value);



回答3:


You can use OOXML to read the file and this library simplify your work http://simpleooxml.codeplex.com.



来源:https://stackoverflow.com/questions/10704582/read-an-xslx-file-and-convert-to-list

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!