public void WriteExcel() { Excel.Application newApp = new Excel.Application(); SetBarInvoke setBar = currentForm.SetBarSettings; currentForm.BeginInvoke(setBar, result.Count); try { newApp.Workbooks.Add(Type.Missing); Excel.Worksheet currSheet = (Excel.Worksheet)newApp.Worksheets.Item[1]; IncBarInvoke incBar = currentForm.IncOpeningBarValue; int i = 1; foreach (User u in result) { int j = 1; currSheet.Cells[i, j++] = u.Id; if (selectedFields[0]) { currSheet.Cells[i, j++] = u.date; } if (selectedFields[1]) { currSheet.Cells[i, j++] = u.firstName; } if (selectedFields[2]) { currSheet.Cells[i, j++] = u.lastName; } if (selectedFields[3]) { currSheet.Cells[i, j++] = u.surName; } if (selectedFields[4]) { currSheet.Cells[i, j++] = u.city; } if (selectedFields[5]) { currSheet.Cells[i, j++] = u.country; } i++; currentForm.Invoke(incBar); } currSheet.SaveAs(fileName); newApp.Quit(); SetLabelInvoke setLabel = currentForm.SetWaitLabelValue; currentForm.BeginInvoke(setLabel, "Файл успешно записан!"); } catch (ThreadAbortException ex) { } catch (Exception ex) { MessageBox.Show("Ошибка. " + ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); newApp.Quit(); } }
public void ReadingFromFile() { try { try { SetLabelInvoke setLabelInvoke = currentForm.SetWaitLabelValue; currentForm.WaitLabel.BeginInvoke(setLabelInvoke, "Открытие файла ..."); stream = new FileStream(fileName, FileMode.Open, FileAccess.Read); reader = new StreamReader(stream, Encoding.GetEncoding(1251)); string curLine; string[] fields; int lineNumber = 1; FileInfo file = new FileInfo(fileName); int linesCount = (int)(file.Length / 68.5); SetBarInvoke setBarInvoke = currentForm.SetBarSettings; currentForm.OpeningBar.BeginInvoke(setBarInvoke, linesCount); UserContext db = new UserContext(); User user = new User(); if (workState == 2) { db.Users.RemoveRange((from p in db.Users select p)); } while (!reader.EndOfStream) { IncBarInvoke incBarInvoke = currentForm.IncOpeningBarValue; curLine = reader.ReadLine(); fields = curLine.Split(';'); try { DateTime.Parse(fields[0]); user.SetFields(DateTime.Parse(fields[0]), fields[1], fields[2], fields[3], fields[4], fields[5]); db.Users.Add(user); db.SaveChanges(); lineNumber++; } catch (FormatException) { if (MessageBox.Show(fields[0] + "\nНеверная дата в " + lineNumber + " строке. Загрузка будет остановлена. Сохранить загруженные ранее данные?", "Ошибка", MessageBoxButtons.YesNo, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1) == DialogResult.Yes) { db.SaveChanges(); } incBarInvoke = currentForm.StopThread; currentForm.BeginInvoke(incBarInvoke); } currentForm.OpeningBar.Invoke(new Action(() => { currentForm.OpeningBar.Value++; })); } db.SaveChanges(); setLabelInvoke = currentForm.SetWaitLabelValue; currentForm.WaitLabel.BeginInvoke(setLabelInvoke, "Файл успешно загружен!"); reader.Close(); stream.Close(); } catch (FileNotFoundException) { throw (new Exception("Файл '" + fileName + "' не найден. Проверьте существование файла.")); } catch (ThreadAbortException) { throw (new Exception("Выполнено принудительное завершение работы потока.")); } catch (Exception ex) { throw (new Exception("Возникла ошибка. " + ex.Message + " " + ex.InnerException)); } } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); IncBarInvoke incBarInvoke = currentForm.StopThread; currentForm.BeginInvoke(incBarInvoke); } }