使用LINQ读取Excel [英] Read Excel using LINQ

查看:127
本文介绍了使用LINQ读取Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想阅读excel 2003(不能因为来自第三方而改变),并且在列表或词典中分组数据(我不是一个很好的
,例如下面(Excel格式)
图书数据[excel中的第一行和第一列]
第二行(无记录)
代码,名称,IBN [第三行(第二列,第三列)
Aust [第四行,第一列]
UX test1 34 [第五行(第二列,第三列)
...
....

I want to read excel 2003( cannot change as its coming from third party) and group data in List or Dictionary (I don't which one is good) for example below (Excel formatting ) Books Data [first row and first column in excel] second row( no records) Code,Name,IBN [third row (second column, third column] Aust [fourth row, first column] UX test1 34 [ fifth row (second column, third column] ...... ....

图书数据

     Code     Name     IBN

Aust

    UX         test1     34
   UZ         test2     345
   UN         test3     5654

US

   UX         name1     567
  TG         nam2      123
  UM         name3     234

我正在使用以下代码阅读excel数据(来自Google的一些帮助)

I am reading excel data using following code( some help from Google)

        string filename = @"C:\\" + "Book1.xls";
        string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
                                      "Data Source=" + filename + ";" +
                                      "Extended Properties=Excel 8.0;";

        OleDbDataAdapter dataAdapter = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", connectionString);
        DataSet myDataSet = new DataSet();
        dataAdapter.Fill(myDataSet, "BookInfo");
        DataTable dataTable = myDataSet.Tables["BookInfo"];


       var rows = from p in dataTable.AsEnumerable()
       where p[0].ToString() != null || p[0].ToString() != "" && p.Field<string>("F2") != null
       select new 
       { countryName= p[0],
           bookCode= p.Field<string>("F2"),
           bookName= p.Field<string>("F3")
      };

上面的代码不是很好,因为我使用F2和国家我正在使用p [0]。我应该用什么来获取每个国家的代码和名称。

The code above is not good as to get the "Code" I am using " F2" and for country I am using p[0].What should I use to get the code and name for each country.

此外,它给出了我想要的信息,但我不如何放入列表或字典或类中,我可以通过传递参数作为国家名称来获取数据。

Also it’s give the information I want but I don't how to put in list or dictionary or in class so I can get data by passing parameter as a country name.

首先,必须将所有数据放在列表或字典中然后可以通过列表或字典获取数据按国家/地区过滤。
谢谢

In short first it must put all data in list or dictionary and then you can call list or dictionary get data filter by country. Thanks

推荐答案

有两件事您需要做:

首先,您需要重新格式化电子表格,使第一行的列标题如下表所示:

First, you need to reformat the spreadsheet to have the column headers on the first row like the table below shows

| Country | Code | Name    | IBN  |
|---------|------|---------|------|
| Aust    | UX   | test1   | 34   |
| Aust    | UZ   | test2   | 345  |
| Aust    | UN   | test3   | 5654 |
| US      | UX   | name1   | 567  |
| US      | TG   | name2   | 123  |
| US      | UM   | name3   | 234  |

其次,使用 Linq to Excel 库来检索数据。它负责制作oledb连接并为您创建sql。以下是使用库的简单方式的一个例子。

Second, use the Linq to Excel library to retrieve the data. It takes care of making the oledb connection and creating the sql for you. Below is an example of how easy it is to use the library

var book = new ExcelQueryFactory("pathToExcelFile");
var australia = from x in book.Worksheet()
                where x["Country"] == "Aust"
                select new
                {
                   Country = x["Country"],
                   BookCode = x["Code"],
                   BookName = x["Name"]
                };

结帐 Linq to Excel介绍视频,了解有关开源项目的更多信息。

Checkout the Linq to Excel intro video for more information about the open source project.

这篇关于使用LINQ读取Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆