有没有一种简单的方法告诉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);
}
}
}
}
答案 0 :(得分:14)
我需要通过标头枚举并向我的最终用户显示所有列标题。我将Muhammad Mubashir代码作为基础并将其更改/转换为扩展方法并从中删除了硬编码的数字。
public static class ExcelWorksheetExtension
{
public static string[] GetHeaderColumns(this ExcelWorksheet sheet)
{
List<string> columnNames = new List<string>();
foreach (var firstRowCell in sheet.Cells[sheet.Dimension.Start.Row, sheet.Dimension.Start.Column, 1, sheet.Dimension.End.Column])
columnNames.Add(firstRowCell.Text);
return columnNames.ToArray();
}
}
答案 1 :(得分:3)
我有类似的问题。以下是一些可能有用的代码:
using (var package = new ExcelPackage(fileStream))
{
// Get the workbook in the file
var workbook = package.Workbook;
if (workbook != null && workbook.Worksheets.Any())
{
// Get the first worksheet
var sheet = workbook.Worksheets.First();
// Get header values
var column1Header = sheet.Cells["A1"].GetValue<string>();
var column2Header = sheet.Cells["B1"].GetValue<string>();
// "A2:A" means "starting from A2 (1st col, 2nd row),
// get me all populated cells in Column A" (yes, unusual range syntax)
var firstColumnRows = sheet.Cells["A2:A"];
// Loop through rows in the first column, get values based on offset
foreach (var cell in firstColumnRows)
{
var column1CellValue = cell.GetValue<string>();
var column2CellValue = cell.Offset(0, 1).GetValue<string>();
}
}
}
如果有人知道比cell.Offset
更优雅的方式,请告诉我。
答案 2 :(得分:2)
var pck = new OfficeOpenXml.ExcelPackage();
pck.Load(new System.IO.FileInfo(path).OpenRead());
var ws = pck.Workbook.Worksheets["Worksheet1"];
DataTable tbl = new DataTable();
var hasHeader = true;
foreach (var firstRowCell in ws.Cells[1, 1, 1, ws.Dimension.End.Column]){
tbl.Columns.Add(hasHeader ? firstRowCell.Text : string.Format("Column {0}", firstRowCell.Start.Column));
}
var startRow = hasHeader ? 2 : 1;
for (var rowNum = startRow; rowNum <= ws.Dimension.End.Row; rowNum++){
var wsRow = ws.Cells[rowNum, 1, rowNum, ws.Dimension.End.Column];
var row = tbl.NewRow();
foreach (var cell in wsRow){
row[cell.Start.Column - 1] = cell.Text;
}
tbl.Rows.Add(row);
}
答案 3 :(得分:1)
我刚刚使用ndd代码并使用System Linq转换它。
using System.Linq;
using OfficeOpenXml;
namespace Project.Extensions.Excel
{
public class ExcelWorksheetExtension
{
/// <summary>
/// Get Header row with EPPlus.
/// <a href="https://stackoverflow.com/questions/10278101/epplus-reading-column-headers">
/// EPPlus Reading Column Headers
/// </a>
/// </summary>
/// <param name="sheet"></param>
/// <returns>Array of headers</returns>
public static string[] GetHeaderColumns(this ExcelWorksheet sheet)
{
return sheet.Cells[sheet.Dimension.Start.Row, sheet.Dimension.Start.Column, 1, sheet.Dimension.End.Column]
.Select(firstRowCell => firstRowCell.Text).ToArray();
}
}
}