使用 C# 和 Open XML 从 Excel 到 DataTable
我使用的是 Visual Studio 2008,需要使用 Open XML SDK 2.0 从 Excel 工作表创建一个 DataTable
。我需要使用工作表第一行的 DataTable 列创建它,并使用其余值完成它。
有谁有示例代码或链接可以帮助我做到这一点?
I'm using Visual Studio 2008 and I need create a DataTable
from a Excel Sheet using the Open XML SDK 2.0. I need to create it with the DataTable columns with the first row of the sheet and complete it with the rest of values.
Does anyone have a example code or a link that can help me to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
我知道这个帖子开始已经很久了。然而,上述解决方案对我来说都不起作用。空单元格问题等。
我在 GitHub 上找到了一个非常好的带有“MIT”许可证的解决方案:
https://github.com/ExcelDataReader/ExcelDataReader
这对于 C# 和 VBnet 应用程序都适用。
来自 VBNET 的示例调用(C# 的示例代码位于 GitHub 上):
结果是一个数据集,工作簿中的每个工作表都有一个表。
我真的很喜欢自己编译用 C# 编写的 dll,而不是使用现成的 dll。这样我就可以控制向客户交付的内容。
I know it is a long time ago since this thread started. However, none of the solutions above did not really work for me. Empty cells issue and others.
I found a very good solution with 'MIT' license on GitHub:
https://github.com/ExcelDataReader/ExcelDataReader
This worked for me for both C# and VBnet applications.
Sample call from VBNET (the sample code for c# is on GitHub) :
The result was a dataset with one table for each sheet in the workbook.
An I really like to compile the dll made in C# by myself rather then using a ready dll. So I can control what I am delivering to customers.
根据我的要求,我从 DLMAN 的最佳答案中修改了“ExcelUtility”Read() 的几部分代码。
还添加了 saveDataTablesToExcel() 和 ExportDataSet() 方法以在 xlsx 文件中保存多个 DataTable。
以下是新“ExcelUtility”类的完整代码及其用法。
用法 :
As per my requirements, I have modified few part of code of 'ExcelUtility' Read() from the best answer by D.L.MAN.
Also added saveDataTablesToExcel() and ExportDataSet() method to save multiple DataTables in xlsx file.
Following is the full code of new 'ExcelUtility' class and it's usage.
Usage :
如果行值为 null 或空,则获取值错误。
如果工作正常,所有列都会填充数据。但也许所有行都不是
if rows value is null or empty get values wrong work.
all columns filled with data if it is working true. but maybe all rows not
我认为这应该满足你的要求。另一个函数只是为了处理您是否有共享字符串,我假设您在列标题中这样做。不确定这是否完美,但我希望它有所帮助。
I think this should do what you're asking. The other function is there just to deal with if you have shared strings, which I assume you do in your column headers. Not sure this is perfect, but I hope it helps.
您好上面的代码工作正常,除了一个更改
将下面的代码行替换
为
如果您使用 (i-1) 它将引发异常:
Hi The above code is working fine except one change
replace the below line of code
with
If you use (i-1) it will throw an exception:
此解决方案适用于没有空单元格的电子表格。
要处理空单元格,您需要将此行:替换
为类似以下内容:
并添加此方法:
This solution works for spreadsheets without empty cells.
To handle empty cells, you will need to replace this line:
with something like this:
And add this method:
这是我的完整解决方案,其中还考虑了空单元格。
This is my complete solution where empty cell is also taken into consideration.
首先将ExcelUtility.cs添加到您的项目中:
ExcelUtility.cs
用法:
然后享受它!
First Add ExcelUtility.cs to your project :
ExcelUtility.cs
Usage :
then enjoy it!