有没有一种简单的方法可以告诉 EPPlus 一行是标题?或者我应该通过使用 SelectedRange 指定范围来创建标题,将其从工作表中删除并迭代剩余的单元格?
我最终这样做了:
class Program
{
static void Main(string[] args)
{
DirectoryInfo outputDir = new DirectoryInfo(@"C:\testdump\excelimports");
FileInfo existingFile = new FileInfo(outputDir.FullName + @"\Stormers.xlsx");
Dictionary<string, string> arrColumnNames = new Dictionary<string,string>() { { "First Name", "" }, { "Last Name", "" }, { "Email Address", "" } };
using (ExcelPackage package = new ExcelPackage(existingFile))
{
ExcelWorksheet sheet = package.Workbook.Worksheets[1];
var q = from cell in sheet.Cells
where arrColumnNames.ContainsKey(cell.Value.ToString())
select cell;
foreach (var c in q)
{
arrColumnNames[c.Value.ToString()] = c.Address;
}
foreach (var ck in arrColumnNames)
{
Console.WriteLine("{0} - {1}", ck.Key, ck.Value);
}
var qValues = from r in sheet.Cells
where !arrColumnNames.ContainsValue(r.Address.ToString())
select r;
foreach (var r in qValues)
{
Console.WriteLine("{0} - {1}", r.Address, r.Value);
}
}
}
}