public void DoImport() { var fmpr = new Form_Progress(); fmpr.Message = "Importējam datus ..."; fmpr.MaxProgress = WorkBookConfig.RowCount; WorkBookConfig.ProgressChanged += (sender, arg) => { fmpr.Progress = WorkBookConfig.RowsDone; }; fmpr.OnCancel = () => { WorkBookConfig.Cancel = true; }; ActOnRowUpdated = () => { WorkBookConfig.RowsDone++; fmpr.Progress = WorkBookConfig.RowsDone; }; string er = null; fmpr.RunThis = () => { Task.Factory.StartNew(() => { er = WorkBookConfig.ImportXLS(); if (er != "OK") { return; } fmpr.Message = "Numurējam ierakstus ..."; SetNewIDs(myAdapterManager1); fmpr.Message = "Saglabājam izmaiņas ..."; WorkBookConfig.RowsDone = 0; fmpr.Progress = 0; fmpr.MaxProgress = WorkBookConfig.GetUpdateCount(); WireAdapterEvents(); myAdapterManager1.UpdateAll(true); }).ContinueWith((t) => { UnWireAdapterEvents(); ActOnRowUpdated = null; fmpr.Close(); if (WorkBookConfig.Cancel) { MyMainForm.ShowInfo("Datu imports pātraukts."); RejectChanges(); } else if (t.IsFaulted) { var me = new MyException("Datu imports neizdevās.", t.Exception); Form_Error.ShowException(me); RejectChanges(); } else if (er != "OK") { var me = new MyException("Datu imports neizdevās.", new Exception(er)); Form_Error.ShowException(me); RejectChanges(); } else { MyMainForm.ShowInfo("Datu imports pabeigts."); WorkBookConfig.ImportDone = true; } }, TaskScheduler.FromCurrentSynchronizationContext()); }; fmpr.ShowDialog(MyMainForm); }
public void DoTestRun() { FileStream fs = null; try { fs = new FileStream(FileName, FileMode.Open); } catch (Exception) { MyMainForm.ShowWarning("Neizdevās atvērt failu:\n" + FileName); return; } if (WorkBookConfig != null) { bsCount.DataSource = null; WorkBookConfig.WB.Close(); WorkBookConfig.Stream.Close(); } WorkBookConfig = new WorkBookConfig(fs); int rowscount = WorkBookConfig.GetFirstCount(); ErrorList = null; var fmpr = new Form_Progress(); fmpr.Message = "Pārbaudam XLS failu ..."; fmpr.MaxProgress = rowscount; WorkBookConfig.ProgressChanged += (sender, arg) => { fmpr.Progress = WorkBookConfig.RowsDone; }; fmpr.OnCancel = () => { WorkBookConfig.Cancel = true; }; fmpr.RunThis = () => { Task.Factory.StartNew(() => { ErrorList = WorkBookConfig.TestXLS(); }).ContinueWith((t) => { fmpr.Close(); GetCounts(); SetSkipDocs(false); if (WorkBookConfig.Cancel) { MyMainForm.ShowInfo("XLS faila pārbaude pātraukts."); } else if (t.IsFaulted) { var me = new MyException("XLS faila pārbaude neizdevās.", t.Exception); Form_Error.ShowException(me); } else { MyMainForm.ShowInfo("XLS faila pārbaude pabeigta."); } }, TaskScheduler.FromCurrentSynchronizationContext()); }; fmpr.ShowDialog(MyMainForm); }
public void Export() { var fd = new OpenFileDialog(); fd.CheckFileExists = false; fd.Title = "Eksportēt uz XLS failu"; fd.Filter = "Excel files (*.xls)|*.xls"; var rt = fd.ShowDialog(MyMainForm); if (rt != DialogResult.OK) { return; } var filename = fd.FileName; if (File.Exists(filename)) { try { File.Delete(filename); } catch (Exception) { MyMainForm.ShowWarning("Neizdevās izdzēst failu:\n" + filename); return; } } FileStream fs = null; try { fs = new FileStream(filename, FileMode.CreateNew); } catch (Exception) { MyMainForm.ShowWarning("Neizdevās izveidot failu:\n" + filename); return; } void InvokeAction(Action act) { if (InvokeRequired) { Invoke(act); } else { act(); } } WorkBookConfig = new WorkBookConfig(); WorkBookConfig.InitCellStyles(); var fmpr = new Form_Progress(); fmpr.Message = "Notiek datu eksports ..."; fmpr.MaxProgress = RowsData.TotalRowCount; WorkBookConfig.ProgressChanged += (sender, arg) => { fmpr.Progress = WorkBookConfig.RowsDone; }; fmpr.OnCancel = () => { WorkBookConfig.Cancel = true; }; fmpr.RunThis = () => { Task.Factory.StartNew(() => { RowsData.ExportToXLS(WorkBookConfig); if (WorkBookConfig.Cancel) { return; } WorkBookConfig.WB.Write(fs); }).ContinueWith((t) => { WorkBookConfig.WB.Close(); fs.Close(); fmpr.Close(); if (WorkBookConfig.Cancel) { MyMainForm.ShowInfo("Datu eksports pātraukts."); } else if (t.IsFaulted) { var me = new MyException("Datu eksports neizdevās.", t.Exception); Form_Error.ShowException(me); } else { MyMainForm.ShowInfo("Darīts!"); } }, TaskScheduler.FromCurrentSynchronizationContext()); }; fmpr.ShowDialog(MyMainForm); }