Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to filter hidden columns #631

Open
FreeVB opened this issue Mar 21, 2023 · 9 comments
Open

How to filter hidden columns #631

FreeVB opened this issue Mar 21, 2023 · 9 comments
Labels

Comments

@FreeVB
Copy link

FreeVB commented Mar 21, 2023

Excuse me, if a column in EXCEL is set to hide, how can I skip this column when reading it?

@andersnm
Copy link
Collaborator

Hi,

reader.GetColumnWidth() returns the width of a column in character units. May be 0 if the column is hidden.

@FreeVB
Copy link
Author

FreeVB commented Mar 21, 2023

Thank you. This function can be implemented as expected.

@FreeVB
Copy link
Author

FreeVB commented Mar 21, 2023

无标题1

hi,andersnm. Is there any good way to achieve the screenshot above? To remove the blank column and EXCEL header on the left

@andersnm
Copy link
Collaborator

Hi @FreeVB! The AsDataSet configuration object supports skipping columns using FilterColumn and skipping rows in two different ways using ReadHeaderRow and FilterRow. Please check the documentation at https://github.com/ExcelDataReader/ExcelDataReader for more details

@FreeVB
Copy link
Author

FreeVB commented Mar 23, 2023

The reason for raising this question is that the empty rows and columns on the top and left are not fixed. If they are fixed, you can use the method you mentioned earlier.

@FreeVB
Copy link
Author

FreeVB commented Mar 23, 2023

无标题2

using var stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); using var reader = ExcelReaderFactory.CreateReader(stream); // 使用AsDataSet扩展方法从读取器创建数据集 var dataSet = reader.AsDataSet(new ExcelDataSetConfiguration() { UseColumnDataType = false, ConfigureDataTable = (tableReader) => new ExcelDataTableConfiguration() { UseHeaderRow = firstRowNamesCheckBox.Checked, FilterColumn = (columnReader, columnIndex) => string.IsNullOrEmpty(reader.GetValue(columnIndex)?.ToString()) && reader.GetColumnWidth(columnIndex) > 0, //>0:不显示隐藏列 ReadHeaderRow = (rowReader) => { rowCount = rowReader.RowCount; for (int i = 0; i < rowCount; i++) { rowCount--; if (rowReader[i] == null || rowReader.GetValue(1).ToString().Length == 0) { rowReader.Read(); } } }, }, }); return dataSet.Tables[0];
Currently, I am doing this, but this is not the result I want: Although the title above was ignored, the empty column on the left did not succeed, and the column name is also incorrect.

@andersnm
Copy link
Collaborator

Hi @FreeVB,

Sounds like you need to do some heuristics on the data to determine the actual extents. This is specialized to your situation, unfortunately I cannot provide general programming support.

@FreeVB
Copy link
Author

FreeVB commented Mar 30, 2023

`
static int HeaderRowIndex { get; set; }
public static DataTable Read(string documentPath)
{
using (var stream = File.Open(documentPath, FileMode.Open, FileAccess.Read))
using (var reader = ExcelReaderFactory.CreateReader(stream))
{
DataSet dataSet = reader.AsDataSet(new ExcelDataSetConfiguration()
{
UseColumnDataType = true,
ConfigureDataTable = (tableReader) => new ExcelDataTableConfiguration()
{
//EmptyColumnNamePrefix = "Column ",
UseHeaderRow = true,
FilterColumn = (reader, index) =>
{
bool empty = false;
string sheet = reader.Name;
// Start reading the table from the beginning
reader.Reset();
// EDIT: Head over the our current excel sheet
while (reader.Name != sheet)
if (!reader.NextResult())
break;

                    // Head to the first row with content
                    int rowIndex = 0;
                    while (rowIndex < HeaderRowIndex)
                    {
                        reader.Read();
                        rowIndex++;
                    }

                    while (reader.Read())
                    {
                        // 确定当前列是否为空
                        if (reader[index] == null || string.IsNullOrEmpty(reader[index].ToString()))
                            continue;

                        empty = true;
                        break;
                    }

                    reader.Reset();
                    reader.Read();
                    int rowCount = reader.RowCount;
                    for (int i = 0; i < rowCount; i++)
                    {
                        rowCount--;
                        if (reader[i] == null || reader.GetString(1)?.ToString().Length == 0)
                        {
                            reader.Read();
                        }
                    }
                    return empty;
                }
            }
        });
        return dataSet.Tables[0];
    }        
}

`
hi,@andersnm,

The above code solves the problem I raised.
However, when reading a column that does not normally have an empty column, it prompts an error. Can you help to see why?
thank you!

@appel1
Copy link
Collaborator

appel1 commented May 18, 2023

What error do you get?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants