private async void ReadExcelFileInfo(string path) { try { string ext = System.IO.Path.GetExtension(path).ToLower(); if (!(ext == ".xls" || ext == ".xlsx" || ext == ".xlsm")) { throw new Exception($"Это не файл Excel! Расширение - {ext}.\n{path}"); } ExcelFileInfo result = await Task <ExcelFileInfo> .Factory.StartNew(() => { using (Office.UsingExcel excel = new Office.UsingExcel()) return(excel.ReadExcelFileInfo(path)); }); this.OpenedExcelFile = result; } catch (Exception ex) { string message = ex.Message; if (ex.InnerException != null) { message += $"\n{ex.InnerException.Message}"; message += $"\nStackTrace:\n{ex.InnerException.StackTrace}"; } MessageBox.Show(message, "Ошибка чтения Excel", MessageBoxButtons.OK, MessageBoxIcon.Error); this.OpenedExcelFile = null; } }
private void OpenFile(object sender, EventArgs e) { if (this.manager != null && manager.IsBusy) { MessageBox.Show("Дождитесь завершения операции.", "Программа в процессе..", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } string path = null; using (OpenFileDialog f = new OpenFileDialog()) { f.Filter = "Файлы Excel|*.xls;*.xlsx;*.xlsm"; f.Title = "Выберите файл"; if (f.ShowDialog(this) == DialogResult.OK) { path = f.FileName; } } if (!string.IsNullOrEmpty(path)) { ReadExcelFileInfo(path); } else { OpenedExcelFile = null; } }
public SplitExcel() { InitializeComponent(); this.Icon = Properties.Resources.Excel_icon; OpenedExcelFile = null; _updater = new Updater.Updater(this); }
internal ExcelFileInfo ReadExcelFileInfo(string fileName) { try { InitializeExcelApplication(); this.xlWorkBook = IExcelProcessor.OpenWorkbook(this.xlApp, fileName, true); List <ExcelSheet> sheets = new List <ExcelSheet>(); Excel.Sheets xlWorkSheets = xlWorkBook.Sheets; foreach (Excel.Worksheet sheet in xlWorkSheets) { sheets.Add(GetSheetInfo(sheet)); } ExcelFileInfo result = new ExcelFileInfo(fileName, sheets); xlWorkBook.Close(); ExitExcelApplication(); return(result); } catch { if (xlWorkBook != null) { xlWorkBook.Close(); } if (xlApp != null) { ExitExcelApplication(); } throw; } }