通过输入中的给定分隔符将数据表转换为.xls,.xlsx,.csv

问题描述 投票:0回答:2

我想要一个方法将数据表数据写入.xls,.xlsx或.cv,根据提供的输入和分隔符作为输入

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());
    }
}
c# file-io console-application export-to-csv export-to-excel
2个回答
0
投票

你说在评论中每2小时只有1000行。这是C#程序可接受的数据量。我会说剩下的大问题是你使用的输出格式。

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

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

在现有的课程中有一个奇怪的“导出到XLS”功能,但这些很少见,远远介于你所得到的一切。不幸的是,由于我们总是使用COM Interop作为后备,我们从未开发过一个独立的库来处理.XLS。具有讽刺意味的是,使用这种旧格式比C#/ .NET更难,然后它将来自Java。

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

.CSV是最低的共同点,可能是最容易创建的。但是,如果用户应该打开此文档,则缺少格式化可能会成为问题。

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

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


0
投票
    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();

}

© www.soinside.com 2019 - 2024. All rights reserved.