Exemplo n.º 1
0
        static void CreateCsv(CsvWriter csvWriter)
        {
            TargetType targetType = TargetType.Client;

            string serverFolderPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, serverFolder);

            if (!Directory.Exists(serverFolderPath))
            {
                Directory.CreateDirectory(serverFolderPath);
            }

            string clientFolderPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, clientFolder);

            if (!Directory.Exists(clientFolderPath))
            {
                Directory.CreateDirectory(clientFolderPath);
            }

            string srcFolderPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, srcFolder);

            if (!Directory.Exists(clientFolderPath))
            {
                Directory.CreateDirectory(clientFolderPath);
            }

            DirectoryInfo di = new DirectoryInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, srcFolder));

            foreach (FileInfo srcFile in di.GetFiles())
            {
                if (srcFile.Extension.ToLower().CompareTo(".xlsx") == 0)
                {
                    using (ExcelPackage excelPackage = new ExcelPackage(srcFile))
                    {
                        //excelPackage.ConvertToCsv(Path.Combine(csvFolderPath, "test"));
                        var format = new ExcelOutputTextFormat();
                        format.Encoding = Encoding.UTF8;

                        for (int cnt = 0; cnt < excelPackage.Workbook.Worksheets.Count; cnt++)
                        {
                            ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets[cnt];

                            int totalColumn = worksheet.Dimension.End.Column;
                            for (int i = totalColumn; i > 0; i--)
                            {
                                string targetStr = worksheet.Cells[3, i].Text.ToLower();

                                if (targetStr.CompareTo("nodata") == 0)
                                {
                                    worksheet.DeleteColumn(i);
                                }

                                if (targetType == TargetType.Client && targetStr.CompareTo("server") == 0)
                                {
                                    worksheet.DeleteColumn(i);
                                }

                                if (targetType == TargetType.Server && targetStr.CompareTo("client") == 0)
                                {
                                    worksheet.DeleteColumn(i);
                                }
                            }
                            worksheet.DeleteRow(3);


                            for (int i = 0; i < worksheet.Dimension.End.Row; i++)
                            {
                                List <string> strList = new List <string>();
                                for (int j = 0; j < worksheet.Dimension.End.Column; j++)
                                {
                                    strList.Add(worksheet.Cells[i + 1, j + 1].Text);
                                }
                                csvWriter.AddRow(strList.ToArray());
                            }

                            //Path.Combine(csvFolderPath, worksheet.Name)


                            string dstPath = Path.Combine(targetType == TargetType.Server ? serverFolderPath : clientFolderPath, $"{worksheet.Name}.csv");
                            //worksheet.Cells[1, 1, worksheet.Dimension.End.Row, worksheet.Dimension.End.Column].SaveToText(dstFile, format);
                            string csv = csvWriter.Write();
                            File.WriteAllText(dstPath, csv, Encoding.UTF8);
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
        static void WriteCsv2()
        {
            var writer = new CsvWriter();

            CreateCsv(writer);
        }