我想使用EPPlus库中的ExcelWorksheet对象,并检索所有具有值及其位置的单元格。
在一天结束时,我想要一本类似的词典。
Dictionary<KeyValuePair<int, int>, object> values.
我正在寻找一种漂亮而简洁的方法来将单元格值提取到此字典中以供使用。
我非常清楚您可以直接引用单元格值,如下所示,但是有一种很好的方法可以提取所有数据吗?
worksheet.Cells[row, column];
答案 0 :(得分:6)
EPPlus中没有将单元格集合直接转换为另一个集合的本机函数,但它确实实现了IEnumerable
,因此您可以使用工作表上的Linq
轻松地完成此操作。这很好用,因为cells
只包含包含值的单元格引用,因此您甚至不需要指定行/列限制:
[TestMethod]
public void Cells_To_Dictionary_Test()
{
//http://stackoverflow.com/questions/32066500/get-all-the-cell-values-from-excel-using-epplus
//Throw in some data
var datatable = new DataTable("tblData");
datatable.Columns.AddRange(new[] { new DataColumn("Col1", typeof(int)), new DataColumn("Col2", typeof(int)), new DataColumn("Col3", typeof(object)) });
for (var i = 0; i < 10; i++)
{
var row = datatable.NewRow();
row[0] = i;
row[1] = i * 10;
row[2] = Path.GetRandomFileName();
datatable.Rows.Add(row);
}
//Create a test file
var existingFile = new FileInfo(@"c:\temp\Grouped.xlsx");
if (existingFile.Exists)
existingFile.Delete();
using (var pck = new ExcelPackage(existingFile))
{
var worksheet = pck.Workbook.Worksheets.Add("Sheet1");
worksheet.Cells.LoadFromDataTable(datatable, true);
pck.Save();
}
//Load a dictionary from a file
using (var pck = new ExcelPackage(existingFile))
{
var worksheet = pck.Workbook.Worksheets["Sheet1"];
//Cells only contains references to cells with actual data
var cells = worksheet.Cells;
var dictionary = cells
.GroupBy(c => new {c.Start.Row, c.Start.Column})
.ToDictionary(
rcg => new KeyValuePair<int, int>(rcg.Key.Row, rcg.Key.Column),
rcg => cells[rcg.Key.Row, rcg.Key.Column].Value);
foreach (var kvp in dictionary)
Console.WriteLine("{{ Row: {0}, Column: {1}, Value: \"{2}\" }}", kvp.Key.Key, kvp.Key.Value, kvp.Value);
}
}
在输出中产生这个:
{ Row: 1, Column: 1, Value: "Col1" }
{ Row: 1, Column: 2, Value: "Col2" }
{ Row: 1, Column: 3, Value: "Col3" }
{ Row: 2, Column: 1, Value: "0" }
{ Row: 2, Column: 2, Value: "0" }
{ Row: 2, Column: 3, Value: "o103ujwu.hmq" }
{ Row: 3, Column: 1, Value: "1" }
{ Row: 3, Column: 2, Value: "10" }
{ Row: 3, Column: 3, Value: "awyajw2t.lwa" }
{ Row: 4, Column: 1, Value: "2" }
{ Row: 4, Column: 2, Value: "20" }
{ Row: 4, Column: 3, Value: "mjhlre2d.nh2" }
{ Row: 5, Column: 1, Value: "3" }
{ Row: 5, Column: 2, Value: "30" }
{ Row: 5, Column: 3, Value: "dypjvona.m3w" }
{ Row: 6, Column: 1, Value: "4" }
{ Row: 6, Column: 2, Value: "40" }
{ Row: 6, Column: 3, Value: "s5oushbr.sax" }
{ Row: 7, Column: 1, Value: "5" }
{ Row: 7, Column: 2, Value: "50" }
{ Row: 7, Column: 3, Value: "1vqjxxky.2gn" }
{ Row: 8, Column: 1, Value: "6" }
{ Row: 8, Column: 2, Value: "60" }
{ Row: 8, Column: 3, Value: "2dw00hi0.pjp" }
{ Row: 9, Column: 1, Value: "7" }
{ Row: 9, Column: 2, Value: "70" }
{ Row: 9, Column: 3, Value: "qgottyza.vnu" }
{ Row: 10, Column: 1, Value: "8" }
{ Row: 10, Column: 2, Value: "80" }
{ Row: 10, Column: 3, Value: "yx4tmvdp.2pq" }
{ Row: 11, Column: 1, Value: "9" }
{ Row: 11, Column: 2, Value: "90" }
{ Row: 11, Column: 3, Value: "jutk2r0v.1yx" }