5

有没有一种简单的方法可以告诉 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);
            }
        }
    }
}
4

4 回答 4

16

我需要枚举标题并将所有列标题显示给我的最终用户。我以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();
    }
}
于 2012-10-31T16:20:28.713 回答
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,请告诉我。

于 2012-10-09T19:47:42.220 回答
3
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);
}
于 2012-08-07T05:55:06.507 回答
1

我只是使用了ndd代码并使用 System Linq 对其进行了转换。

using System.Linq;
using OfficeOpenXml;

namespace Project.Extensions.Excel
{
    public static 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();
        }
    }
}
于 2017-11-25T08:01:05.637 回答