Syncfusion SfDataGrid 导出Excel

时间:2024-04-03 19:05:38
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[]; 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);
}