var options = new ExcelExportingOptions { ExcelVersion = ExcelVersion.Excel2013, };
//不須要導出的字段 options.ExcludeColumns.Add("InspectionRecordId"); options.ExcludeColumns.Add("PartName"); options.ExportUnBoundRows = false; var excelEngine = dataGrid.ExportToExcel(dataGrid.View, options); //.ExportToExcel(dataGrid.View, options); var workBook = excelEngine.Excel.Workbooks[0]; Microsoft.Win32.SaveFileDialog saveFileDialog = new Microsoft.Win32.SaveFileDialog(); saveFileDialog.Filter = "Excel Office 2013|*.xlsx"; //設置默認文件名(能夠不設置) saveFileDialog.FileName = $"巡檢記錄({DateTime.Now:yy-MM-dd})"; saveFileDialog.DefaultExt = "xlsx"; saveFileDialog.AddExtension = true; //保存對話框是否記憶上次打開的目錄 bool? result = saveFileDialog.ShowDialog(); //點了保存按鈕進入 if (result == true) { workBook.SaveAs(saveFileDialog.FileName); }