Exemple #1
0
 public void SaveToExcel(string path)
 {
     try
     {
         using (var workbook = new XLWorkbook(XLEventTracking.Disabled))
         {
             var worksheet = workbook.AddWorksheet("Каталог");
             using (var writer = new CsvWriter(new ExcelSerializer(worksheet)))
             {
                 if (_isHarvest)
                 {
                     ResultItemHarvestMap.initConfiguration(writer.Configuration);
                 }
                 else
                 {
                     ResultItemMap.initConfiguration(writer.Configuration);
                 }
                 var result = new List <ResultItem>(_process.Result.ToArray()).ToList();
                 if (Settings.Default.RemoveDublicate)
                 {
                     result = result.Distinct(new ResultItemComparer()).ToList();
                 }
                 writer.WriteRecords(result);
             }
             workbook.SaveAs(path);
         }
     }
     catch (Exception ex)
     {
         Logger.Error(ex);
         new ExceptionViewer("SaveToExcel", ex).ShowDialog();
     }
 }
Exemple #2
0
        public void AutoSaveToExcel(string catalogName = "")
        {
            try
            {
                if (string.IsNullOrWhiteSpace(catalogName))
                {
                    catalogName = "Catalog";
                }
                if (string.IsNullOrWhiteSpace(_sessionFileName))
                {
                    _sessionFileName = $"{catalogName.VaildFileName()}-{DateTime.Now:dd-MM-yyyy HH-mm-ss}";
                }
                using (var workbook = new XLWorkbook(XLEventTracking.Disabled))
                {
                    var worksheet = workbook.AddWorksheet("Каталог");
                    using (var writer = new CsvWriter(new ExcelSerializer(worksheet)))
                    {
                        if (_isHarvest)
                        {
                            ResultItemHarvestMap.initConfiguration(writer.Configuration);
                        }
                        else
                        {
                            ResultItemMap.initConfiguration(writer.Configuration);
                        }

                        var result = new List <ResultItem>(_process.Result.ToArray()).ToList();
                        if (Settings.Default.RemoveDublicate)
                        {
                            result = result.Distinct(new ResultItemComparer()).ToList();
                        }
                        writer.WriteRecords(result);
                    }
                    var path = Path.Combine(Environment.CurrentDirectory, $"{_sessionFileName}.xlsx");
                    if (File.Exists(path))
                    {
                        try
                        {
                            File.Delete(path);
                        }
                        catch
                        {
                            _sessionFileName = $"{catalogName.VaildFileName()}-{DateTime.Now:dd-MM-yyyy HH-mm-ss}";
                            path             = Path.Combine(Environment.CurrentDirectory, $"{_sessionFileName}.xlsx");
                        }
                    }
                    workbook.SaveAs(path);
                }
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
            }
        }