【问题标题】:Convert a Datatable to .xls,.xlsx,.csv by the given delimeter in input通过输入中的给定分隔符将数据表转换为 .xls、.xlsx、.csv
【发布时间】: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());
    }
}

【问题讨论】:

  • 使用CsvHelper
  • 您选择的 DBMS 应该具有“导出到 CSV”功能。我对批量操作的建议是始终在 DBMS 中进行。在代码中做这些事情只会让它变得更慢、更需要内存并且更容易出错。无论您编写什么代码,都将不如已经存在的代码和大量额外的工作。
  • @Christopher - 这是一个控制台作业,它会在 2 小时的时间范围内频繁运行一次。根据表中处理的数据和配置,我需要根据配置导出,即提供给方法的输入。我们甚至有数据的限制,我们可以接收处理的最大数据是 1000 行。
  • 有很多写excel的例子:stackoverflow.com/questions/23041021/…

标签: c# file-io console-application export-to-csv export-to-excel


【解决方案1】:

您说 cmets 每 2 小时只有 1000 行。这是 C# 程序可接受的数据量。我想说剩下的大问题是您使用的输出格式。

.CSV 是最简单的一种。这种格式可以通过 File.WriteLine() 和一些字符串连接来完成。我知道在 C# 中没有构建 CSV 解析器或编写器代码,但有很多第 3 方代码。

.XLS 需要 (t)rusty Office COM Interop。这需要安装 office,并且不能通过非交互式会话(如 Windows 服务)工作。除了使用 COM 互操作的所有正常问题。

现有类有一个奇怪的“导出到 XLS”功能,但这些功能很少见,介于两者之间,几乎与你所获得的一切有关。不幸的是,由于我们总是将 COM 互操作作为后备,我们从未完全开发过一个独立的库来处理 .XLS。具有讽刺意味的是,在 C#/.NET 中使用这种旧格式比在 Java 中更难。

.XLSX 但是更容易。它可以使用OpenXML SDK 编写。或者 XML 编写器和 ZipArchive 类:所有 ???x 格式的核心都是重命名的 .ZIP 容器中的一堆 .XML 文件。甚至应该有 3rd 方代码,以便更轻松地使用 SDK。

.CSV 是最小的公分母,并且可能是最容易创建的。但是,如果用户要打开此文档,则缺少格式可能会成为问题。

.XSLX 如果您需要用户打开它,我会选择它。

.XSL 我会像一群愤怒的蜜蜂一样避开。

【讨论】:

  • 感谢您的回复。它有帮助,让我跳入代码并尝试这些。我虽然会有一个通用服务将数据表转换为所有 excel 格式(CSV、.xls、.xlsx)
【解决方案2】:
    I have written this  Program to convert Xls,XLSx using console application with 
    Datatable as input and for text file I have written a simple stream writer logic.This works good. Initially I have installed package manage console  and below code 
    using expertXLs package.I am not sure wheather I can share the key of that 
    or not.Please search the key and give in config before running it


     Package Manage Console - Install-Package ExpertXls.ExcelLibrary -Version 5.0.0


     Code :
      --------

private static void GenerateTxtFileFromDataTable(DataTable sampleDataTable,string delimiter)
 {
                 var _expertxlsLK = ConfigurationManager.AppSettings["ExpertxlsLK"];
                 //GetKey Value from config

                 // Create the workbook in which the data from the DataTable will be loaded 0 for 2003 Excel(xls),1 for 2007 Excel(xlsx)
                ExcelWorkbookFormat workbookFormat = ExcelWorkbookFormat.0;

                // create the workbook in the desired format with a single worksheet
                ExcelWorkbook workbook = new ExcelWorkbook(workbookFormat);
                workbook.EnableFormulaCalculations();

                workbook.LicenseKey = _expertxlsLK;

               // get the first worksheet in the workbook
               ExcelWorksheet worksheet = workbook.Worksheets[0];

             // set the default worksheet name
              worksheet.Name = "ClaimInformation";

            // load data from DataTable into the worksheet
           worksheet.LoadDataTable(sampleDataTable, 1, 1, true);
           worksheet.Workbook.EnableFormulaCalculations();
          workbook.Save(@"M:\Rupesh\test.xlsx");
          workbook.Close();

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-11
    • 2012-09-06
    • 1970-01-01
    相关资源
    最近更新 更多