How To Export Rows To Excel From Data Table By selecting different columns?

Shahab a 241 Reputation points

Hi all
I Created a Dataset and a Data Table which I Added two Rows by this codes...
Please note that my Data table has three columns (ID, Name and Address)
I have two problems
1- Now I want to send only the records of the name column to Excel (Export To Excel)
2- I want ID to be placed in column A of Excel and Address to be placed in column B of Excel

How do I do this?

    Dim Ds As New DataSet  
        Dim Dt As DataTable  
        Dim idCoulumn As DataColumn  
        Dim NameCoulumn1 As DataColumn  
        Dim NameCoulumn2 As DataColumn  
        Dim I As Integer  
        Dim Dr As DataRow  
        Dt = New DataTable()  
        idCoulumn = New DataColumn("ID", Type.GetType("System.Int32"))  
        NameCoulumn1 = New DataColumn("Name", Type.GetType("System.String"))  
        NameCoulumn2 = New DataColumn("Address", Type.GetType("System.String"))  
        Dr = Dt.NewRow()  
        Dr("ID") = 1  
        Dr("Name") = "Name1"  
        Dr("Address") = "Address1"  
        Dr = Dt.NewRow()  
        Dr("ID") = 2  
        Dr("Name") = "Name2"  
        Dr("Address") = "Address2"  
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,600 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 27,001 Reputation points Microsoft Vendor

    Hi @Shahab a ,
    You can refer to the following code to output the data of a specific column to excel by IO stream.

    You can refer to the following code to output the data of column ID and Address by using Microsoft Excel 16.0 Object Library.
    Best Regards.
    Jiachen Li


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Shahab a 241 Reputation points

    Thanks @Jiachen Li-MSFT

    I Change Your Codes...
    See it And if it is good, confirm it

         Dim dtb As New DataTable  
            dtb = Dt.Copy  
                Dim oExcel As New Excel.Application  
                Dim obook As Excel.Workbook  
                Dim oSheet As Excel.Worksheet  
                    obook = oExcel.Workbooks.Add '.Open(file)  
                    oSheet = obook.Worksheets(1)  
                    oSheet.Name = "SheetExcel"  
                    Dim r As Integer, c As Integer  
                    Dim rCount As Integer  
                    Dim cCount As Integer  
                    '  Dim str As String  
                    rCount = dtb.Rows.Count  
                    cCount = dtb.Columns.Count()  
                    For c = 1 To cCount  
                    oSheet.Cells(1, c) = dtb.Columns(c - 1).Caption '设置列标题     
                    c = 0 : r = 0  
                    For r = 1 To rCount  
                        For c = 1 To cCount  
                            oSheet.Cells(r + 1, c) = CStr(dtb.Rows(r - 1)(c - 1).ToString)  
                    oExcel.Visible = True  
                Catch ex As Exception  
                End Try  
                oExcel = Nothing  
                obook = Nothing  
                oSheet = Nothing