读取 Excel 文件 (.xls/.xlsx) 的最佳方式 [英] Optimal way to Read an Excel file (.xls/.xlsx)

查看:49
本文介绍了读取 Excel 文件 (.xls/.xlsx) 的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有多种方法可以读取 Excel 文件:

I know that there are different ways to read an Excel file:

  • Iterop
  • Oledb
  • 打开 Xml SDK

兼容性不是问题,因为程序将在受控环境中执行.

Compatibility is not a question because the program will be executed in a controlled environment.

我的要求:
将文件读取到 DataTable/CUstom Entities(我不知道如何为对象创建动态属性/字段[列名将在 Excel 文件中变化])

My Requirement :
Read a file to a DataTable / CUstom Entities (I don't know how to make dynamic properties/fields to an object[column names will be variating in an Excel file])

使用 DataTable/Custom Entities 使用其数据执行一些操作.

Use DataTable/Custom Entities to perform some operations using its data.

使用操作结果更新DataTable

写回excel文件.

哪个更简单.

如果可能的话,还建议我自定义实体(动态地向对象添加属性/字段)

Also if possible advice me on custom Entities (adding properties/fields to an object dynamically)

推荐答案

看一看 Linq-to-Excel.挺好看的.

var book = new LinqToExcel.ExcelQueryFactory(@"File.xlsx");

var query =
    from row in book.Worksheet("Stock Entry")
    let item = new
    {
        Code = row["Code"].Cast<string>(),
        Supplier = row["Supplier"].Cast<string>(),
        Ref = row["Ref"].Cast<string>(),
    }
    where item.Supplier == "Walmart"
    select item;

它还允许强类型行访问.

It also allows for strongly-typed row access too.

这篇关于读取 Excel 文件 (.xls/.xlsx) 的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆