We don’t allow questions seeking recommendations for software libraries, tutorials, tools, books, or other off-site resources. You can edit the question so it can be answered with facts and citations.
Closed 2 years ago.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(3)
下面的代码是我用来将
IEnumerable
转换为 DataTable 的代码,其中包含为每个属性命名的列,以及 IEnumerable 中每个项目的值(作为行)。从这里到将其保存为 CSV 文件只需很小的一步,因此,您可以编写以下代码将数据转换为数据表:
一旦它存在,将其写入 CSV 文件或任何其他格式的文件是相当简单的您的选择,因为从
IEnumerable
中提取数据的所有“棘手”反射工作都已完成。The code below is code I use to convert an
IEnumerable<T>
to a DataTable containing columns named for each property, along with the values of each item in the IEnumerable as rows. It's a very small hop from this to saving it as a CSV fileSo, you could write the following to convert your data to a datatable:
Once it's there, it's fairly trivial to write it out to a CSV file, or any other format of your choice, as all the "tricky" reflection work to extract the data from the
IEnumerable<T>
has been done.请查看此处的库部分:
从 C# 创建 Excel(.XLS 和 .XLSX)文件
在 C# 中创建 Excel 电子表格 .XLS 和 .XLSX
Take a look here for the library part:
Create Excel (.XLS and .XLSX) file from C#
Creating Excel spreadsheets .XLS and .XLSX in C#
我已经解决了与你所问的类似的问题。就像你有一个 IEnumerable
所以,如果你想从中创建一个 Excel,只需创建一个工作簿
并使用循环进行迭代,然后将其添加到工作表
这里我有另一个子列表,因此我使用内部循环进行迭代。按照你的选择去做。
I have solved a problem similar to what you asked. Like you have an IEnumerable
So if you want to make a excel from this just create a workbook
And iterate with a looping and then add it to the worksheet
Here I have another child list, therefore I have iterated using a inner loop. Do as your choice.