英文:
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:
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("your_excel_file.xlsx", 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 "Daniel"
Console.WriteLine("Cell A1 Value: " + cellA1Value);
Console.WriteLine("Cell B2 Value: " + 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 < dataTable.Columns.Count; i++)
{
shiftedTable.Columns.Add();
}
// Add empty rows
for (int i = 0; i < dataTable.Rows.Count; i++)
{
shiftedTable.Rows.Add(shiftedTable.NewRow());
}
// Copy the cell values to the shifted positions
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;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论