I am trying to export my DataGrid to Excel.
I found this on the internet, but the author used a DataGridView.
- Code: Select all
privatevoid btnSaveFile_Click(object sender, EventArgs e)
{
string fileName;
Spire.DataExport.XLS.CellExport cellExport = new Spire.DataExport.XLS.CellExport();
Spire.DataExport.XLS.WorkSheet worksheet1 = new Spire.DataExport.XLS.WorkSheet();
worksheet1.DataSource = Spire.DataExport.Common.ExportSource.DataTable;
worksheet1.DataTable = this.dataGridView1.DataSource asDataTable;
worksheet1.StartDataCol = ((System.Byte)(0));
cellExport.Sheets.Add(worksheet1);
cellExport.ActionAfterExport = Spire.DataExport.Common.ActionType.OpenView;
fileName = txtFileName.Text.ToString() + ".xls";
cellExport.SaveToFile(fileName);
}
i was trying to write it down for a DataGrid.
- Code: Select all
privatevoid btnSaveFile_Click(object sender, EventArgs e)
{
string fileName;
Spire.DataExport.XLS.CellExport cellExport = new Spire.DataExport.XLS.CellExport();
Spire.DataExport.XLS.WorkSheet worksheet1 = new Spire.DataExport.XLS.WorkSheet();
worksheet1.DataSource = Spire.DataExport.Common.ExportSource.DataTable;
worksheet1.DataTable = this.dataGridView1.ItemsSource asDataTable;
worksheet1.StartDataCol = ((System.Byte)(0));
cellExport.Sheets.Add(worksheet1);
cellExport.ActionAfterExport = Spire.DataExport.Common.ActionType.OpenView;
fileName = txtFileName.Text.ToString() + ".xls";
cellExport.SaveToFile(fileName);
}
but now it crashes with the error:
System.Exception: "WorkSheet Sheet 1 with error: Data source (DataTable) property has not been initialized..
TextExport::SaveToFile,var:"
Can anyone help me with this?