Using LINQ with Open XML (Excel)

白昼怎懂夜的黑 提交于 2019-12-24 08:24:13

问题


I have an excel sheet and I am processing it by using Open XML SDK 2.0. The scenario is, There is a column which contains date in my excel sheet. I need to get the maximum and minimum date from that column.

I can do this by looping and reaching to that cell, doing comparisons and finding desired answer.

But due to optimality I want to do this by using LINQ to get Minimum and maximum dates.

Is it possible to do so? If yes, then how?


回答1:


You can see how to get IEnumerable of all cells from column there:Read excel sheet data in columns using OpenXML, and use Max() on it.




回答2:


You will want to take a look at the LINQ Min() and Max() functions. If you need to return the entire Cell object, you can use OrderByDescending().




回答3:


You could read this post Open XML SDK and LINQ to XML by Eric White (Eric wrote a lot of posts about OpenXML and LINQ). And then you will be able to query your Excel file data with LINQ. You might want to see the spreadsheet objects structure in The Open XML SDK Productivity Tool, which can generate the source code for you. You could use this code to understand how to programmatically access the data you need.




回答4:


Thanks to all

I have used like this

IEnumerable<Cell> cells = workSheetPart.Worksheet.Descendants<Cell>().Where(c => string.Compare(GetColumnName(c.CellReference.Value), strIndex, false) == 0).OrderBy(c => c.CellValue.Text);

And getting min and max values like this

int cellCount = cells.Count();
Cell MaxCell = cells.ToArray()[0];
Cell MinCell = cells.ToArray()[cellCount - 1];


来源:https://stackoverflow.com/questions/11722808/using-linq-with-open-xml-excel

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