简体   繁体   English

使用 ClosedXML 从 Excel 文件中读取

[英]Reading from Excel File using ClosedXML

My Excel file is not in tabular data.我的 Excel 文件不在表格数据中。 I am trying to read from an excel file.我正在尝试从 excel 文件中读取数据。 I have sections within my excel file that are tabular.我的excel文件中有表格的部分。

I need to loop through rows 3 to 20 which are tabular and read the data.我需要遍历表格的第 3 到 20 行并读取数据。

Here is party of my code:这是我的代码的一部分:

     string fileName = "C:\\Folder1\\Prev.xlsx";
     var workbook = new XLWorkbook(fileName);
     var ws1 = workbook.Worksheet(1); 

How do I loop through rows 3 to 20 and read columns 3,4, 6, 7, 8?如何遍历第 3 到 20 行并读取第 3、4、6、7、8 列? Also if a row is empty, how do I determine that so I can skip over it without reading that each column has a value for a given row.此外,如果一行是空的,我如何确定这一点,以便我可以跳过它而不读取每一列都有给定行的值。

To access a row:要访问一行:

var row = ws1.Row(3);

To check if the row is empty:要检查该行是否为空:

bool empty = row.IsEmpty();

To access a cell (column) in a row:要连续访问一个单元格(列):

var cell = row.Cell(3);

To get the value from a cell:要从单元格中获取值:

object value = cell.Value;
// or
string value = cell.GetValue<string>();

For more information see the documentation .有关详细信息,请参阅文档

Here's my jam.这是我的果酱。

var rows = worksheet.RangeUsed().RowsUsed().Skip(1); // Skip header row
foreach (var row in rows)
{
    var rowNumber = row.RowNumber();
    // Process the row
}

If you just use .RowsUsed() , your range will contain a huge number of columns.如果您只使用.RowsUsed() ,您的范围将包含大量列。 Way more than are actually filled in!比实际填写的要多得多!

So use .RangeUsed() first to limit the range.所以首先使用.RangeUsed()来限制范围。 This will help you process the file faster!这将帮助您更快地处理文件!

You can also use .Skip(1) to skip over the column header row (if you have one).您还可以使用.Skip(1)跳过列标题行(如果有的话)。

I prefer using RowsUsed() method to get a list of only those rows which are non-empty or has been edited by the user.我更喜欢使用RowsUsed()方法来获取仅包含非空行或已由用户编辑的行的列表。 This way I can avoid making emptiness check while processing each row.这样我可以避免在处理每一行时进行空位检查。

I'm not sure if this solution will solve OP's problem.我不确定这个解决方案是否能解决 OP 的问题。 But this code snippet can process 3rd to 20th row numbers out of all the non-empty rows as I've filtered the empty rows already before starting the process.但是这个代码片段可以处理所有非空行中的第 3 到第 20 行编号,因为我在开始该过程之前已经过滤了空行。 Filtering the non-empty rows before processing can affect the count of rows which will get processed.在处理之前过滤非空行会影响将被处理的行数。

But I feel that RowsUsed() method is very helpful in any general scenario when you are processing the rows of an excel sheet.但我觉得RowsUsed()方法在处理 Excel 工作表的行时在任何一般情况下都非常有用。

string fileName = "C:\\Folder1\\Prev.xlsx";
using (var excelWorkbook = new XLWorkbook(fileName))
{
    var nonEmptyDataRows = excelWorkbook.Worksheet(1).RowsUsed();

    foreach (var dataRow in nonEmptyDataRows)
    {
       //for row number check
       if(dataRow.RowNumber() >=3 && dataRow.RowNumber() <= 20)
       {
           //to get column # 3's data
           var cell = dataRow.Cell(3).Value;
       }
    }
}

It works easily它很容易工作

 XLWorkbook workbook = new XLWorkbook(FilePath);
 var rowCount = workbook.Worksheet(1).LastRowUsed().RowNumber();
 var columnCount = workbook.Worksheet(1).LastColumnUsed().ColumnNumber();
 int column = 1;
 int row = 1;
 List<string> ll = new List<string>();
 while (row <= rowCount)
 {
      while (column <= columnCount)
      {
         string title = workbook.Worksheets.Worksheet(1).Cell(row, column).GetString();
                ll.Add(title);
                column++;
       }

 row++;
 column = 1;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM