使用ClosedXML从Excel文件读取 [英] Reading from Excel File using ClosedXML

查看:1104
本文介绍了使用ClosedXML从Excel文件读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Excel文件不在表格数据中。我正在尝试从Excel文件中读取。
我的excel文件中有表格形式的部分。

My Excel file is not in tabular data. I am trying to read from an excel file. I have sections within my excel file that are tabular.

我需要遍历表格形式的第3至20行并读取数据。

I need to loop through rows 3 to 20 which are tabular and read the data.

这是我的代码的一方:

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

如何遍历第3至20行并读取第3、4、6、7、8列?
另外,如果某行为空,那么如何确定该值,因此我可以跳过该行而无需读取给定行的每个列都有值。

How do I loop through rows 3 to 20 and read columns 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.

推荐答案

要访问行:

var row = ws1.Row(3);

要检查行是否为空:

bool empty = row.IsEmpty();

要连续访问单元格(列):

To access a cell (column) in a row:

var cell = row.Cell(3);

要从单元格获取值:

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

有关更多信息,请参见文档

For more information see the documentation.

这篇关于使用ClosedXML从Excel文件读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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