C# – ExcelDataReader – 是否可以在 DataSet 中包括空单元格?

huangapple go评论76阅读模式
英文:

C# - ExcelDataReader - Is it possible to include empty cells in DataSet?

问题

这是Excel文件:

当我读取单元格 'A1' 时,我得到 "Daniel"。

是否可以让ExcelDataReader包括空行/列单元格?

我希望 'A1' 单元格包含 null,而不是读取为 'Daniel',并且 'B2' 单元格包含 'Daniel'。

配置中,我看不到任何相关的设置:

DataSet result = excelReader.AsDataSet(new ExcelDataSetConfiguration() {
    ConfigureDataTable = (_) => new ExcelDataTableConfiguration() {
        UseHeaderRow = treatFirstRowAsRowHeader
    }
});
英文:

I have this excel file:

C# – ExcelDataReader – 是否可以在 DataSet 中包括空单元格?

When I read cell 'A1' I get "Daniel".

Is it possible to make ExcelDataReader include empty rows/columns cells?

Instead of reading the value of cell 'A1' -> 'Daniel'

I want 'A1' cell to contain null as the excel file presents

and cell 'B2' to contain 'Daniel'

I don't see anything relavant in configuration I can set:

        DataSet result = excelReader.AsDataSet(new ExcelDataSetConfiguration() {
            ConfigureDataTable = (_) => new ExcelDataTableConfiguration() {
                UseHeaderRow = treatFirstRowAsRowHeader
            }
        });

答案1

得分: 1

尝试这段代码

    using System;
    using System.Data;
    using ExcelDataReader;

    class Program
    {
        static void Main(string[] args)
        {
            using (var stream = File.Open("your_excel_file.xlsx", FileMode.Open, FileAccess.Read))
            {
                using (var reader = ExcelReaderFactory.CreateReader(stream))
                {
                    var dataSet = reader.AsDataSet(new ExcelDataSetConfiguration
                    {
                        // 在这里配置ExcelDataReader选项
                        // 如果需要的话(例如设置FirstRowAsColumnNames)
                    });
    
                    DataTable dataTable = dataSet.Tables[0];
    
                    // 将单元格值移动以包括空行/列
                    DataTable shiftedDataTable = ShiftData(dataTable);
    
                    // 访问所需的单元格
                    var cellA1Value = shiftedDataTable.Rows[0][0]; // 将是null
                    var cellB2Value = shiftedDataTable.Rows[1][1]; // 将是"Daniel"
    
                    Console.WriteLine("单元格 A1 值:" + cellA1Value);
                    Console.WriteLine("单元格 B2 值:" + cellB2Value);
                }
            }
        }
    
        static DataTable ShiftData(DataTable dataTable)
        {
            // 创建一个包含移动后单元格值的新DataTable
    
            DataTable shiftedTable = new DataTable();
    
            // 添加空列
            for (int i = 0; i < dataTable.Columns.Count; i++)
            {
                shiftedTable.Columns.Add();
            }
    
            // 添加空行
            for (int i = 0; i < dataTable.Rows.Count; i++)
            {
                shiftedTable.Rows.Add(shiftedTable.NewRow());
            }
    
            // 将单元格值复制到移动后的位置
            for (int i = 0; i < dataTable.Rows.Count; i++)
            {
                for (int j = 0; j < dataTable.Columns.Count; j++)
                {
                    shiftedTable.Rows[i + 1][j + 1] = dataTable.Rows[i][j];
                }
            }
    
            return shiftedTable;
        }
    }
英文:

try this code

using System;
using System.Data;
using ExcelDataReader;

class Program
{
    static void Main(string[] args)
    {
        using (var stream = File.Open(&quot;your_excel_file.xlsx&quot;, FileMode.Open, FileAccess.Read))
        {
            using (var reader = ExcelReaderFactory.CreateReader(stream))
            {
                var dataSet = reader.AsDataSet(new ExcelDataSetConfiguration
                {
                    // Configure the ExcelDataReader options here
                    // if needed (e.g., setting the FirstRowAsColumnNames)
                });

                DataTable dataTable = dataSet.Tables[0];

                // Shift the cell values to include empty rows/columns
                DataTable shiftedDataTable = ShiftData(dataTable);

                // Access the desired cells
                var cellA1Value = shiftedDataTable.Rows[0][0]; // will be null
                var cellB2Value = shiftedDataTable.Rows[1][1]; // will be &quot;Daniel&quot;

                Console.WriteLine(&quot;Cell A1 Value: &quot; + cellA1Value);
                Console.WriteLine(&quot;Cell B2 Value: &quot; + cellB2Value);
            }
        }
    }

    static DataTable ShiftData(DataTable dataTable)
    {
        // Create a new DataTable with shifted cell values
        DataTable shiftedTable = new DataTable();

        // Add empty columns
        for (int i = 0; i &lt; dataTable.Columns.Count; i++)
        {
            shiftedTable.Columns.Add();
        }

        // Add empty rows
        for (int i = 0; i &lt; dataTable.Rows.Count; i++)
        {
            shiftedTable.Rows.Add(shiftedTable.NewRow());
        }

        // Copy the cell values to the shifted positions
        for (int i = 0; i &lt; dataTable.Rows.Count; i++)
        {
            for (int j = 0; j &lt; dataTable.Columns.Count; j++)
            {
                shiftedTable.Rows[i + 1][j + 1] = dataTable.Rows[i][j];
            }
        }

        return shiftedTable;
    }
}

huangapple
  • 本文由 发表于 2023年6月26日 19:50:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76556410.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定