コード例 #1
0
ファイル: Program.cs プロジェクト: AdhamAwadhi/DataConnectors
        public static void Sample_ReadExcel_WriteCsv()
        {
            string sampleDataPath = @"..\..\Samples\";
            var    watch          = new Stopwatch();

            using (var reader = new ExcelNativeAdapter())
            {
                reader.FileName  = sampleDataPath + @"cd-Daten.xls";
                reader.SheetName = "Tabelle1";
                reader.Connect();

                using (var writer = new CsvAdapter())
                {
                    writer.FileName = Path.Combine(sampleDataPath, "cd-Daten_FromXls.csv");

                    watch.Start();
                    int lineCount = 0;

                    reader.ReadData(30)
                    .ForEach(x =>
                    {
                        Console.WriteLine("Tablename=" + x.TableName + ", Count=" + x.Rows.Count);
                        lineCount += x.Rows.Count;
                    })
                    .Do(x => writer.WriteData(x, false));

                    watch.Stop();
                    Console.WriteLine("lineCount=" + lineCount + ", Time=" + watch.Elapsed);
                    Console.ReadLine();
                }

                reader.Disconnect();
            }
        }
コード例 #2
0
        public void Test_Csv_Excel_Csv()
        {
            using (var csvReader = new CsvAdapter())
                using (var excelWriter = new ExcelNativeAdapter())
                    using (var excelReader = new ExcelNativeAdapter())
                        using (var csvWriter = new CsvAdapter())
                        {
                            csvReader.Enclosure = "\"";
                            csvReader.FileName  = this.testDataPath + @"cd-Daten.txt";
                            var csvData = csvReader.ReadAllData();

                            excelWriter.FileName  = Path.Combine(this.resultPath, "cd-Daten.xls");
                            excelWriter.SheetName = "Tabelle1";
                            excelWriter.CreateNewFile();
                            excelWriter.Connect();
                            excelWriter.WriteAllData(csvData);
                            excelWriter.Disconnect();

                            excelReader.FileName  = excelWriter.FileName;
                            excelReader.SheetName = excelWriter.SheetName;
                            excelReader.Connect();
                            var accessData = excelReader.ReadAllData();

                            csvWriter.Encoding  = csvReader.Encoding;
                            csvWriter.Enclosure = csvReader.Enclosure;
                            csvWriter.FileName  = this.resultPath + @"cd-Daten_ExcelRoundtrip.csv";
                            csvWriter.WriteAllData(accessData);

                            if (!FileUtil.CompareFiles(csvReader.FileName, csvWriter.FileName))
                            {
                                throw new Exception("Original and copied file do not match");
                            }
                        }
        }