在特定行的LinqToExcel GetColumnNames [英] LinqToExcel GetColumnNames at specific Row

查看:94
本文介绍了在特定行的LinqToExcel GetColumnNames的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用LinqToExcel库读取我的mvc4项目中的excel文件.我的问题是当我尝试读取第4行的标题时...我该怎么做?

I am using the library LinqToExcel to read excel files in my mvc4 project. My problem is when I try to read the headers at row 4... How I can do this?

在项目中,存在一个返回所有列名称的函数,但我想这些列必须位于第0行.

In project, exists a function that returns all the column names, but I suppose that the columns need to be at row 0.

    // Summary:
    //     Returns a list of columns names that a worksheet contains
    //
    // Parameters:
    //   worksheetName:
    //     Worksheet name to get the list of column names from
    public IEnumerable<string> GetColumnNames(string worksheetName);

谢谢.

推荐答案

不幸的是,GetColumnNames()方法仅在标题行位于第1行时有效.

Unfortunately the GetColumnNames() method only works when the header row is on row 1.

但是,应该可以使用WorksheetRangeNoHeader()方法获取列名.

However, it should be possible to get the column names by using the WorksheetRangeNoHeader() method.

看起来像这样

var excel = new ExcelQueryFactory("excelFileName");
// Only select the header row
var headerRow = from c in excel.WorksheetRangeNoHeader("A4", "Z4")
                select c;

var columnNames = new List<string>();
foreach (var headerCell in headerRow)
  columnNames.Add(headerCell.ToString());

这篇关于在特定行的LinqToExcel GetColumnNames的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆