我有一个 Excel,
A B C D
-- --- --- ---
3 4
7 8 9 10
在 Interop.Excel 中我使用。
xlSheet.Cells(1, 3).Formula.ToString
获取第1行第3列的值,结果为3 或
xlSheet.Cells(1, 2).Formula.ToString
获取第 1 行第 2 列的值,结果将是一个空字符串
如何使用 Open XML 解决此问题?
var value = AllCells.Where(t => t.CellReference != "AC1" && string.Join("", t.CellReference.Value.ToString().Where(e => char.IsLetter(e))。 ToList()) == "AC" && t.CellValue != null).GroupBy(g => g.CellValue.Text).Select(x => x.FirstOrDefault()).Select(s => new CellModel( ) { CellReference = s.CellReference, DataType = s.DataType != null ? s.DataType.Value : CellValues.Error, CellValue = s.CellValue }).ToList();
使用 openxml,您可以使用 .RowId 和 .ColumnId 来选择特定单元格,然后使用 LINQ 应该允许您随意操作数据。 请参阅此http://openxmldeveloper.org/blog/b/openxmldeveloper/archive/2011/07/18/query-open-xml-spreadsheets-in-vb-net-using-linq.aspx了解详细示例
再次查看后,这个解决方案应该有效
Using doc As SpreadsheetDocument = SpreadsheetDocument.Open("Data.xlsx", False)
Dim worksheet As WorksheetPart = doc.WorkbookPart.GetPartById(0)
Dim cell as Cell = worksheet.rows(yourRow).columns(yourColumn)
return cell.value
End using
如果没有,也许您将不得不进行 foreach 循环,但如果您有大量数据,则不太建议这样做