async Task ReadExcel(string excelFilename) { if (!System.IO.File.Exists(excelFilename)) { return; } _busy.BusyContent = "正在加载文档,请稍候..."; _busy.IsBusy = true; await Task.Run(() => { using (ExcelUnit excel = new ExcelUnit(excelFilename)) { this.Dispatcher.InvokeAsync(() => { Person = excel.Read(); }).Completed += (o, oe) => { _busy.IsBusy = false; //自动设置为当前日期 Person.Date = System.DateTime.Now.ToShortDateString(); }; } }); }
void readCmdBinding_Executed(object sender, ExecutedRoutedEventArgs e) { _busy.IsBusy = true; _busy.BusyContent = "正在读取文档..."; string fname = this.oriExcelFile.Text; string cell = this.g1.Text; var t = Task.Run(() => { using (ExcelUnit excel = new ExcelUnit(fname)) { Person.DiaryContent = excel.ReadCell(cell); } }); t.GetAwaiter().OnCompleted(() => { this.Dispatcher.Invoke(() => { _busy.IsBusy = false; }); }); e.Handled = true; }
async Task SaveAsExcel(string filename) { _busy.BusyContent = "正在保存文档,请稍候..."; _busy.IsBusy = true; string oriFilename = this.oriExcelFile.Text; await Task.Factory.StartNew(() => { using (ExcelUnit excel = new ExcelUnit(oriFilename)) { this.Dispatcher.InvokeAsync(() => { excel.SaveAs(Person, filename); }).Completed += (o, oe) => { _busy.IsBusy = false; }; } }); }