【发布时间】:2019-02-28 03:16:38
【问题描述】:
我想要一种方法将数据表数据写入 .xls、.xlsx 或 .csv,该方法基于与分隔符一起提供的输入作为输入
public class DataTableExtensions
{
/*Input Params : Datatable input
fileFormat(.xls,.csv,.xlsx)
delimeter('\t' (tabSpace) or ,(comma) or | (pipe Symbol)
filepath - Any local folder*/
public void WriteToCsvFile(DataTable dataTable,string fileFormat,string delimeter, string filePath)
{
//Code to convert file based on the input
//Code to create file
System.IO.File.WriteAllText(filePath, fileContent.ToString());
}
}
【问题讨论】:
-
您选择的 DBMS 应该具有“导出到 CSV”功能。我对批量操作的建议是始终在 DBMS 中进行。在代码中做这些事情只会让它变得更慢、更需要内存并且更容易出错。无论您编写什么代码,都将不如已经存在的代码和大量额外的工作。
-
@Christopher - 这是一个控制台作业,它会在 2 小时的时间范围内频繁运行一次。根据表中处理的数据和配置,我需要根据配置导出,即提供给方法的输入。我们甚至有数据的限制,我们可以接收处理的最大数据是 1000 行。
-
有很多写excel的例子:stackoverflow.com/questions/23041021/…
标签: c# file-io console-application export-to-csv export-to-excel