//todo: to be tested
        public static void SaveAs <T>(this IEnumerable <T> data, ConstructionDataContext context, string filename,
                                      FileType type, string description, Action <T> add, ILogPrint print = null, IShowProgress showProgress = null) where T : class, IFile
        {
            var file = new File
            {
                FileName    = filename,
                Type        = type,
                Description = description
            };
            var id = file.Add(context);

            print?.PrintLog("Start add item");
            showProgress?.SetMaxValue(data.Count());
            int step = 0;

            foreach (var item in data)
            {
                item.FileId = id;
                add.Invoke(item);
                step++;
                showProgress?.SetCurrentValue(step);
            }

            context.SaveChanges();
            showProgress?.Done();
        }
示例#2
0
 public static void Export <T>(this IEnumerable <T> data, string filePath, string sheetName, ILogPrint print = null, IShowProgress showProgress = null) where T : IExport
 {
     using (var excel = new ExcelPackage())
     {
         if (!data.Any())
         {
             return;
         }
         //write header
         var sheet      = excel.Workbook.Worksheets.Add(sheetName);
         var isStartRow = true;
         var row        = 2;
         print?.PrintLog("Start writing entries to excel");
         showProgress?.SetMaxValue(data.Count());
         foreach (var item in data)
         {
             if (isStartRow)
             {
                 print?.PrintLog("Writing header");
                 var header = item.GetHeader();
                 sheet.WriteHeader(header);
                 isStartRow = false;
             }
             print?.PrintLog($"Writing row:{row}");
             sheet.WriteRow(item.GetRow(), row);
             showProgress?.SetCurrentValue(row - 1);
             row++;
         }
         showProgress?.Done();
         print?.PrintLog("Saving file");
         excel.SaveAs(new FileInfo(filePath));
         print?.PrintLog($"Exported excel to {filePath}");
     }
 }
        public static void SaveWithFileId <T>(this IEnumerable <T> data, ConstructionDataContext context, int fileId, ILogPrint print = null, IShowProgress showProgress = null)
            where T : class, IFile
        {
            print?.PrintLog($"The file id is {fileId}");
            showProgress?.SetMaxValue(data.Count());
            int i = 0;

            foreach (var item in data)
            {
                item.FileId = fileId;
                context.Entry(item).State = EntityState.Modified;
                showProgress?.SetCurrentValue(i);
                i++;
            }
            print?.PrintLog("Saving data to database.");
            context.SaveChanges();
            showProgress?.Done();
        }
        public static bool SaveAs <T>(this IEnumerable <T> data, ConstructionDataContext context, string filename,
                                      out int existsFileId, string description, ILogPrint print = null, IShowProgress showProgress = null) where T : class, IFile
        {
            existsFileId = 0;

            var enumerable = data as T[] ?? data.ToArray();


            var type = ConvertFileType(GetName(enumerable.First()));

            print?.PrintLog("Find the existing files.");
            var file = File.Select(context, filename, type);

            if (file != null)
            {
                existsFileId = file.Id;
                return(false);
            }

            file = new File
            {
                FileName    = filename,
                Description = description,
                Type        = type
            };
            var id = file.Add(context);

            print?.PrintLog($"The file id is {id}");
            showProgress?.SetMaxValue(enumerable.Length);
            int i = 0;

            foreach (var item in enumerable)
            {
                showProgress?.SetCurrentValue(i);
                item.FileId = id;
                i++;
            }
            print?.PrintLog("Saving data to database.");
            context.SaveChanges();
            showProgress?.Done();
            return(true);
        }