Read Excel using LINQ

后端 未结 3 1749
自闭症患者
自闭症患者 2020-12-28 17:48

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 belo

相关标签:
3条回答
  • 2020-12-28 18:26

    There's two things you need to do:

    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  |
    

    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"]
                    };
    

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

    0 讨论(0)
  • 2020-12-28 18:41

    Suggestion 1

    Checkout THIS link......as AKofC suggests, creating a class to hold your data would be your first port of call. The link I have posted has a small example of the sort of idea we are proposing.

    Suggestion 2 with example...

    The obvious thing to do from the code you have posted would be to create a new class to store your book information in.

    Then you simply define which fields from your excel document it is that you want to pass into the new instance of your bookinformation class.

    New Book Information Class:

    class MyBookInfo
    {
        public string CountryName { get; set; }
        public string BookCode { get; set; }
        public string BookName { get; set; }
    }
    

    Method To Retrieve Info:

    public void GetMyBookInfoFromExcelDocument()
            {
                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 MyBookInfo
                           {
                               CountryName = p.Field<string>("InsertFieldNameHere"),
                               BookCode = p.Field<string>("InsertFieldNameHere"),
                               BookName = p.Field<string>("InsertFieldNameHere")
                           };
            }
    
    0 讨论(0)
  • 2020-12-28 18:48

    From what I understand, I suggest creating a BookData class containing the properties you need, in this case Country, Code, Name, and IBN.

    Then once you've filled your DataSet with the Excel stuff, create a new List, and loop through the DataRows in the DataSet adding the Excel values to the List.

    Then you can use Linq on the List like so:

     List<BookData> results = from books in bookList
                                           where books.country == 'US'
                                           select books;
    

    Or something like that. I don't have Visual Studio on me, and Intellisense has spoiled me, so yeah. >__>

    0 讨论(0)
提交回复
热议问题