We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

How to export only specific columns to Excel from DataTable VB.Net?

Hello Experts,
I am working with default DataGridView, I load the data from datatable and exported same to Excel, but what if I want to export only selected columns to Excel for Ex. My DataTabke is having 20 Column, Now I want to give selection to User's hand that he/she can select the columns which they want to export I can do that by just loading the Titles in CheckListBox but after that How can I export only those columns which are selected?
 Solution I found by myself
>I can copy the selected columns to new DataTable and Export New DataTable : Which I feel not good option.

Is there any direct way of doing it?

2nd Question

I want to merge the cells to a specific number 
For Example : This is what we do to merge the Cells 

ws.Range("A1:AE1").Merge()
ws.Range("A1:AE1").Text = "Daily Report"

But as i asked in first question what if user is selecting 3 columns instead of 20 Columns.
Is ther any way I can merget the columns like ws.Range(1:30).Merge() ? Kind of option?


Thank You in Advance


6 Replies

SS Sridhar Sukumar Syncfusion Team June 8, 2017 12:33 PM UTC

Hi Dhairya, 
 
Thank you for contacting Syncfusion support. 
 
Please find the details for the queries from below table. 
 
Query 
Response 
I am working with default DataGridView, I load the data from datatable and exported same to Excel, but what if I want to export only selected columns to Excel for Ex. My DataTabke is having 20 Column, Now I want to give selection to User's hand that he/she can select the columns which they want to export I can do that by just loading the Titles in CheckListBox but after that How can I export only those columns which are selected? 
 Solution I found by myself 
>I can copy the selected columns to new DataTable and Export New DataTable : Which I feel not good option. 
 
Is there any direct way of doing it? 
 
You can achieve your requirement by using TemplateMarkers in XlsIO. We have prepared a sample for this which can be downloaded from the following location. 
 
Sample link: 
 
To know more about TemplateMarkers, please refer the following UG documentation link for your reference. 
 
UG Documentation link: 
 
I want to merge the cells to a specific number  
For Example : This is what we do to merge the Cells  
 
ws.Range("A1:AE1").Merge() 
ws.Range("A1:AE1").Text = "Daily Report" 
 
But as i asked in first question what if user is selecting 3 columns instead of 20 Columns. 
Is ther any way I can merget the columns like ws.Range(1:30).Merge() ? Kind of option? 
 
You can specify row and column to get specific range from worksheet. For example, if you want to merge range from 1st column to 30th column then you can get range using following code example. 
 
Code snippet: 
int firstRow = 1; 
int firstColumn = 1; 
int lastRow = 1; 
int lastColumn = 30; 
 
worksheet.Range[firstRow, firstColumn, lastRow, lastColumn].Merge(); 

Kindly refer the below UG documentation to know more about accessing a cell or range in XlsIO.

UG Documentation link:
 
  
Regards, 
Sridhar. 



DJ Dhairya Joshi June 9, 2017 06:19 AM UTC

thanks you it worked ! 


SS Sridhar Sukumar Syncfusion Team June 12, 2017 07:38 AM UTC

Hi Dhairya, 
 
Thank you for updating us.  
 
We are glad that your requirement is fulfilled. Please let us know if you need any further assistance. 
 
Regards, 
Sridhar. 



BR Bernadus Rangga Kresna Waskita June 22, 2022 06:55 AM UTC

Hello, Can u help me provide for C# format? I want the user can choose which column he want to export freely too.. but in c# format ty



BR Bernadus Rangga Kresna Waskita June 22, 2022 08:00 AM UTC

guys i just found out that we can use columnChooser, thanks



RS Ramya Sivakumar Syncfusion Team June 23, 2022 02:00 PM UTC

Hi Dhairya,


Thanks for sharing the solution. Please feel free to contact us if you need any further assistance.


Regards,

Ramya.


Loader.
Live Chat Icon For mobile
Up arrow icon