简体   繁体   English

ExcelDataReader 读取所有不可见的工作表

[英]ExcelDataReader reads all invisible sheets

In Excel there is a feature to hide some worksheets.在 Excel 中,有一个功能可以隐藏一些工作表。 I am reading a document which contains these kind of sheets and I want to ignore them.我正在阅读包含此类表格的文档,我想忽略它们。

This is the place which I can hide, or unhide worksheets:这是我可以隐藏或取消隐藏工作表的地方:

  1. On the Home tab, in the Cells group, click Format.在“开始”选项卡的“单元格”组中,单击“格式”。
  2. Under Visibility, click Hide & Unhide, and then click Unhide Sheet.在可见性下,单击隐藏和取消隐藏,然后单击取消隐藏工作表。

How to get list of ONLY Excel VISIBLE worksheet names in Excel using ExcelDataReader ?如何使用ExcelDataReader在 Excel 中获取仅 Excel VISIBLE 工作表名称的列表?

If using the reader interface, the IExcelDataReader.VisibleState property returns the visibility state of the currently read sheet.如果使用阅读器接口,则IExcelDataReader.VisibleState属性返回当前读取工作表的可见性状态。

If using .AsDataSet() , the same value can be retreived from DataTable.ExtendedProperties["visiblestate"]如果使用.AsDataSet() ,可以从DataTable.ExtendedProperties["visiblestate"]中获取相同的值

How to get list of visible worksheet names in Excel using ExcelDataReader ?如何使用ExcelDataReader获取 Excel 中可见工作表名称的列表?

// Prepare your reader by 
var stream = File.Open(yourExcelFilename, FileMode.Open, FileAccess.Read);
var excelDataReader = ExcelDataReader.ExcelReaderFactory.CreateOpenXmlReader(stream);

// This variable will store visible worksheet names
List<string> visibleWorksheetNames;

// Use a loop to read workbook    
visibleWorksheetNames = new List<string>();
for (var i = 0; i < excelDataReader.ResultsCount; i++)
{
    // checking visible state
    if (excelDataReader.VisibleState == "visible")
    {
        visibleWorksheetNames.Add(excelDataReader.Name);
    }

    excelDataReader.NextResult();
}

Read only visible sheets to DataSet:只读数据集的可见工作表:

using (var stream = File.Open("test.xlsx", FileMode.Open, FileAccess.Read))
{
    using (var reader = ExcelReaderFactory.CreateReader(stream))
    {
        var ds = reader.AsDataSet(new ExcelDataSetConfiguration()
        {
            FilterSheet = (tableReader, sheetIndex) => tableReader.VisibleState == "visible",
        });
    }
}

Use reader.RowHeight .使用reader.RowHeight Setting RowHeight = 0 results in a hidden row.设置RowHeight = 0会导致隐藏行。

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

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