使用 exceldatareader 库读取 excel 文件,通过反射将每一行的数据映射到 c# 对象。具体做法是:定义一个包含属性与列名匹配的实体类,调用 asdataset() 获取数据表,遍历 datatable.rows 并使用 setvalue 填充对象。若遇到列名不匹配的情况,需确保实体属性名与 excel 表头完全一致,否则对应字段将为默认值。

如何根据 Excel 表头自动填充对象属性?
利用 System.Reflection 中的 PropertyInfo 和 SetValue 方法实现自动映射。在代码中,先获取实体类型的属性列表,然后遍历 Excel 的每一行,查找与属性名相同的列索引,找到后赋值。这样无需手动编写每列的转换逻辑,即可处理任意结构的表格数据。

using System;
using System.Data;
using System.IO;
using System.Reflection;
using ExcelDataReader;
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
class Program
{
static void Main()
{
// 假设已创建 test.xlsx,第一行为 Name, Age
using (var stream = File.Open("test.xlsx", FileMode.Open, FileAccess.Read))
using (var reader = ExcelReaderFactory.CreateReader(stream))
{
var result = reader.AsDataSet();
DataTable table = result.Tables[0];
foreach (DataRow row in table.Rows)
{
Person p = new Person();
Type type = typeof(Person);
for (int i = 0; i <p>运行上述代码,控制台将输出 Excel 中每一行对应的 <code>Name</code> 和 <code>Age</code> 值。如果某列在实体类中不存在,该列数据会被忽略;如果实体属性存在但 Excel 列为空,则保持默认值。</p><h2>如何处理 Excel 中的日期类型映射?</h2><p>当 Excel 单元格包含日期时,<code>ExcelDataReader</code> 默认返回 <code>DateTime</code> 或 <code>double</code> 类型(取决于格式)。在映射过程中,需特别注意类型转换。如果实体属性是 <code>DateTime</code>,直接赋值即可;如果是字符串,需显式转换。此外,若 Excel 单元格为空,<code>row[i]</code> 为 <code>DBNull.Value</code>,必须跳过以避免异常。</p><div class="aritcle_card flexRow artxards">
<div class="artcardd flexRow">
<a class="aritcle_card_img" rel="nofollow" href="/xiazai/skill12858" title="Excel公式工具(专业版)"><img
src="https://img.php.cn/upload/skill/001/246/273/6adaf20dee630206.jpg" alt="Excel公式工具(专业版)" onerror="this.onerror='';this.src='/static/lhimages/moren/morentu.png'" ></a>
<div class="aritcle_card_info flexColumn">
<a rel="nofollow" href="/xiazai/skill12858" title="Excel公式工具(专业版)" class="overflowclass">Excel公式工具(专业版)</a>
<p class="overflowclass">从自然语言描述生成Excel公式,诊断表格错误,支持VLOOKUP、条件求和等常用函数。</p>
</div>
<a rel="nofollow" href="/xiazai/skill12858" title="Excel公式工具(专业版)" class="aritcle_card_btn flexRow flexcenter"><b></b><span>下载</span>
</a>
</div>
</div><pre class="brush:php;toolbar:false;">using System;
using System.Data;
using System.IO;
using System.Reflection;
using ExcelDataReader;
public class EventRecord
{
public string Title { get; set; }
public DateTime Date { get; set; }
}
class Program
{
static void Main()
{
using (var stream = File.Open("events.xlsx", FileMode.Open, FileAccess.Read))
using (var reader = ExcelReaderFactory.CreateReader(stream))
{
var result = reader.AsDataSet();
DataTable table = result.Tables[0];
foreach (DataRow row in table.Rows)
{
EventRecord rec = new EventRecord();
Type type = typeof(EventRecord);
for (int i = 0; i <p>此段代码展示了如何处理日期列。如果 Excel 中的日期是以序列号形式存储的(如 <code>44562.0</code>),代码会将其转换为标准的 <code>DateTime</code> 对象。若单元格为空,程序不会抛出异常,而是跳过该属性的设置,保持 <code>EventRecord</code> 中 <code>Date</code> 的默认值(即 <code>DateTime.MinValue</code>)。</p><h2>当 Excel 列名与属性名不一致时怎么办?</h2><p>如果 Excel 表头是 <code>User_Name</code> 而实体属性是 <code>Name</code>,直接映射会失败。此时需要在代码中添加一个映射字典,或者在遍历时进行名称修正。例如,可以定义一个静态字典 <code>ColumnMapping</code>,将 Excel 列名映射到实体属性名。在循环中,先检查当前列名是否在字典中,如果在,则使用映射后的属性名进行赋值。这种方法提高了代码的灵活性,允许 Excel 结构变化而不必修改实体类。</p><pre class="brush:php;toolbar:false;">using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Reflection;
using ExcelDataReader;
public class User
{
public string Name { get; set; }
public string Email { get; set; }
}
class Program
{
private static readonly Dictionary<string string> ColumnMapping = new Dictionary<string string>
{
{ "User_Name", "Name" },
{ "Email_Address", "Email" }
};
static void Main()
{
using (var stream = File.Open("users.xlsx", FileMode.Open, FileAccess.Read))
using (var reader = ExcelReaderFactory.CreateReader(stream))
{
var result = reader.AsDataSet();
DataTable table = result.Tables[0];
foreach (DataRow row in table.Rows)
{
User u = new User();
Type type = typeof(User);
for (int i = 0; i <p>通过引入映射字典,即使 Excel 列名发生变化,只要更新字典即可适配。这种方案避免了硬编码列索引,使得代码更易于维护。同时,它确保了只有匹配的列才会被赋值,未匹配的列将被忽略,从而防止错误的数据进入对象。</p></string></string>










