public static List <ThrowList> ExcelPackageToList(ExcelPackage excelPackage) //эксель в лист { ExcelPackage.LicenseContext = LicenseContext.NonCommercial; List <ThrowList> dt = new List <ThrowList>(); ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets["Sheet"]; try { if (worksheet.Dimension == null) { return(dt); } for (int i = 3; i <= worksheet.Dimension.End.Row; i++) { ThrowList newThrow = new ThrowList(); newThrow.Id = worksheet.Cells[i, 1].Value.ToString(); newThrow.Name = worksheet.Cells[i, 2].Value.ToString(); newThrow.Description = worksheet.Cells[i, 3].Value.ToString(); newThrow.Source = worksheet.Cells[i, 4].Value.ToString(); newThrow.ObjectOfInfluence = worksheet.Cells[i, 5].Value.ToString(); newThrow.PrivacyPolicy = worksheet.Cells[i, 6].Value.ToString(); newThrow.Integrity = worksheet.Cells[i, 7].Value.ToString(); newThrow.Availability = worksheet.Cells[i, 8].Value.ToString(); dt.Add(newThrow); } } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error); return(dt); } return(dt); }
private void Button_save_as_Click(object sender, RoutedEventArgs e) //сохранить как { var dialog = new SaveFileDialog(); dialog.Filter = "(*.xlsx)|*.xlsx"; if (dialog.ShowDialog() == true) { using (ExcelPackage excelPackage = new ExcelPackage()) { try { var new_xlsx_file = new FileInfo(dialog.FileName); ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets.Add("Sheet"); int j = 2; ThrowList title_throw = new ThrowList(); worksheet.Cells[j, 1].Value = title_throw.Id; worksheet.Cells[j, 2].Value = title_throw.Name; worksheet.Cells[j, 3].Value = title_throw.Description; worksheet.Cells[j, 4].Value = title_throw.Source; worksheet.Cells[j, 5].Value = title_throw.ObjectOfInfluence; worksheet.Cells[j, 6].Value = title_throw.PrivacyPolicy; worksheet.Cells[j, 7].Value = title_throw.Integrity; worksheet.Cells[j, 8].Value = title_throw.Availability; int index = 0; for (int i = 3; i < throws.Count; i++) { ThrowList _throw = throws[index]; worksheet.Cells[i, 1].Value = _throw.Id; worksheet.Cells[i, 2].Value = _throw.Name; worksheet.Cells[i, 3].Value = _throw.Description; worksheet.Cells[i, 4].Value = _throw.Source; worksheet.Cells[i, 5].Value = _throw.ObjectOfInfluence; worksheet.Cells[i, 6].Value = _throw.PrivacyPolicy; worksheet.Cells[i, 7].Value = _throw.Integrity; worksheet.Cells[i, 8].Value = _throw.Availability; index++; } excelPackage.SaveAs(new_xlsx_file); } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error); return; } } } }