Importing data with LinqToExcel

早过忘川 提交于 2019-12-24 11:57:17

问题


I have an XLSX spreadsheet that I am trying to import into a IList(of FinancialResults) collection using VB.NET.

The spreadsheet is in the below format where I need to map the cells from the appropriate columns into a class.

Spreadsheet

Model

Public Class FinancialResults
    Friend Property MonthBudget As Single
    Friend Property MonthActual As Single
    Friend Property YearToDateBudget As Single
    Friend Property YearToDateActual As Single
    Friend Property FullYearForcastBudget As Single
    Friend Property FullYearForcastActual As Single
    Friend Property NextMonthBudget As Single
    Friend Property NextMonthActual As Single
End Class

Code

My initial view was to use LinqToExcel which I installed via NuGet but I am having problems loading the data using the library. From reading the documentation on the website I found that this functionality can be provided by the AddMapping method

ExcelEngine.AddMapping(Of FinancialResults)(Function(m) m.MonthBudget, "D")
ExcelEngine.AddMapping(Of FinancialResults)(Function(m) m.MonthActual, "E")
ExcelEngine.AddMapping(Of FinancialResults)(Function(m) m.YearToDateBudget, "H")
ExcelEngine.AddMapping(Of FinancialResults)(Function(m) m.YearToDateActual, "I")
ExcelEngine.AddMapping(Of FinancialResults)(Function(m) m.FullYearForcastBudget, "L")
ExcelEngine.AddMapping(Of FinancialResults)(Function(m) m.FullYearForcastActual, "M")
ExcelEngine.AddMapping(Of FinancialResults)(Function(m) m.NextMonthBudget, "P")
ExcelEngine.AddMapping(Of FinancialResults)(Function(m) m.NextMonthActual, "Q")

Then loading the data using the below Linq statement.

Dim var = (From r In ExcelEngine.Worksheet(Of FinancialResults)(excelPageName)
           Select r).ToList

Results

The code compiles and runs but the results are not loaded from the spreadsheet. All instances of the FinacialResults class are populated with 0.0

Figuring that the column mapping maybe at fault I queried the columns collection of the ExcelQueryFactory using

Dim cols = ExcelEngine.GetColumnNames(excelPageName)

Which returned the below.

I changed the column mappings to reflect these but again the results come back as 0.0 for all instances.

ExcelEngine.AddMapping(Of FinancialResults)(Function(m) m.MonthBudget, "F4")
ExcelEngine.AddMapping(Of FinancialResults)(Function(m) m.MonthActual, "F5")

Question

Is this possible using LinqToExcel given the format of this spreadsheet? If so what am I doing wrong? Should I be using the WorksheetRange methods or loading each cell individually?

Is there another preferable alternative?


回答1:


It's not possible for LinqToExcel to read the spreadsheet you have because of its layout.

The easiest way to read it is to change the layout so the first row contains the column names like the picture below shows



来源:https://stackoverflow.com/questions/17042571/importing-data-with-linqtoexcel

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