Spire.XLS is a professional Excel API that enables developers to create, manage, manipulate, convert and print Excel worksheets. Get free and professional technical support for Spire.XLS for .NET, Java, Android, C++, Python.

Fri May 10, 2019 2:24 pm

Hello,
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?

N0m4n904
 
Posts: 5
Joined: Fri May 10, 2019 2:17 pm

Mon May 13, 2019 12:45 pm

Hello,

Thanks for your inquiry and sorry for late reply as weekend.
According to the code you provided, I knew you were going to use our Spire.DataExport in WPF project, right? Sorry to tell that our Spire.DataExport doesn't support WPF platform at present. However, our Spire.XLS could achieve your requirement well in WPF project. Please download the latest Spire.XLS Pack(Hotfix) Version:9.5.4 and use the following code to have a test. If there is any question, please feel free to write back.
Code: Select all
Workbook book = new Workbook();
Worksheet sheet = book.Worksheets[0];
//convert the datagrid source to datatable
DataTable table = ((DataView)this.dataGrid1.ItemsSource).Table;
//insert the table into Excel
sheet.InsertDataTable(table, true, 1, 1);
book.SaveToFile("exportDataGridToExcel.xlsx", ExcelVersion.Version2013);

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1187
Joined: Tue Sep 27, 2016 1:06 am

Thu May 16, 2019 1:41 am

Hi,

Greetings from E-iceblue!
Did we fix your issue?
Thanks in advance for your valuable feedback and time.

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1187
Joined: Tue Sep 27, 2016 1:06 am

Mon May 20, 2019 8:44 am

Nina.Tang wrote:Hello,

Thanks for your inquiry and sorry for late reply as weekend.
According to the code you provided, I knew you were going to use our Spire.DataExport in WPF project, right? Sorry to tell that our Spire.DataExport doesn't support WPF platform at present. However, our Spire.XLS could achieve your requirement well in WPF project. Please download the latest and use the following code to have a test. If there is any question, please feel free to write back.
Code: Select all
Workbook book = new Workbook();
Worksheet sheet = book.Worksheets[0];
//convert the datagrid source to datatable
DataTable table = ((DataView)this.dataGrid1.ItemsSource).Table;
//insert the table into Excel
sheet.InsertDataTable(table, true, 1, 1);
book.SaveToFile("exportDataGridToExcel.xlsx", ExcelVersion.Version2013);

Sincerely,
Nina
E-iceblue support team


Hi,

I am trying to use your Code, but i get an error, that I can't convert a Generic List to a DataView,

could you please help me with this?

N0m4n904
 
Posts: 5
Joined: Fri May 10, 2019 2:17 pm

Mon May 20, 2019 9:13 am

N0m4n904 wrote:
Nina.Tang wrote:Hello,

Thanks for your inquiry and sorry for late reply as weekend.
According to the code you provided, I knew you were going to use our Spire.DataExport in WPF project, right? Sorry to tell that our Spire.DataExport doesn't support WPF platform at present. However, our Spire.XLS could achieve your requirement well in WPF project. Please download the latest and use the following code to have a test. If there is any question, please feel free to write back.
Code: Select all
Workbook book = new Workbook();
Worksheet sheet = book.Worksheets[0];
//convert the datagrid source to datatable
DataTable table = ((DataView)this.dataGrid1.ItemsSource).Table;
//insert the table into Excel
sheet.InsertDataTable(table, true, 1, 1);
book.SaveToFile("exportDataGridToExcel.xlsx", ExcelVersion.Version2013);

Sincerely,
Nina
E-iceblue support team


Hi,

I am trying to use your Code, but i get an error, that I can't convert a Generic List to a DataView,

could you please help me with this?



Ok I got it

N0m4n904
 
Posts: 5
Joined: Fri May 10, 2019 2:17 pm

Mon May 20, 2019 9:17 am

Hello,

Thanks for your quick reply.
Our Spire.Xls imports data via inserting DataTable. You just need to convert your Generic List to DataTable and then use our Spire.Xls to insert your data to Excel sheet. Here is a documentation, hope it could help you.
https://www.c-sharpcorner.com/blogs/converting-generic-list-into-datatable

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1187
Joined: Tue Sep 27, 2016 1:06 am

Mon May 20, 2019 9:19 am

Nina.Tang wrote:Hello,

Thanks for your quick reply.
Our Spire.Xls imports data via inserting DataTable. You just need to convert your Generic List to DataTable and then use our Spire.Xls to insert your data to Excel sheet. Here is a documentation, hope it could help you.
https://www.c-sharpcorner.com/blogs/converting-generic-list-into-datatable

Sincerely,
Nina
E-iceblue support team


Hi Nina,

this works fine, thank you.

Now i have a last question.

How can I export this datagrid to PDF?

N0m4n904
 
Posts: 5
Joined: Fri May 10, 2019 2:17 pm

Mon May 20, 2019 9:40 am

Hello,

Our Spire.PDF (Spire.PDF Pack(Hot Fix) Version:5.5.1) supports drawing Pdf table with the data of DataTable. Please refer to this tutorial: How to Draw PDF Table

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1187
Joined: Tue Sep 27, 2016 1:06 am

Mon May 20, 2019 10:04 am

Nina.Tang wrote:Hello,

Our Spire.PDF (Spire.PDF Pack(Hot Fix) Version:5.5.1) supports drawing Pdf table with the data of DataTable. Please refer to this tutorial: How to Draw PDF Table

Sincerely,
Nina
E-iceblue support team


Thank you ^^

N0m4n904
 
Posts: 5
Joined: Fri May 10, 2019 2:17 pm

Tue May 21, 2019 1:15 am

Hi,

You're welcome.
If you need assistance in the future, please do not hesitate to contact us.
Wish you a nice day!

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1187
Joined: Tue Sep 27, 2016 1:06 am

Return to Spire.XLS