Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
For example , I have two tables in my database:

CustomerTable
Id - PK
Name
Age

OrderTable
OrderId - PK
Product
Qty
Id- FK


I wanted to export the data in excel. with the format like this:

---[new row]------1 Justin 24y/o
---[new row]----------------- 1 , squash , 3
---[new row]----------------- 2 , melon , 2
[space]
---[new row]------2 Kristy 34y/o
---[new row]-----------------4 , apple , 3
---[new row]----------------- 2 , papaya , 2
[space]


The only way I know in exporting is just like this:


---[new row]------1 Justin 24y/o 1 , squash , 3
---[new row]------1 Justin 24y/o 2 , melon , 2
---[new row]-------2 Kristy 34y/o, 4 , apple , 3
---[new row]--------2 Kristy 34y/o, 2 , papaya , 2

This is the code I am using:

On Pageload()

(JOIN QUERY HERE : Select Buyer_Table.*, Order_Table.*)
buyer_table.datasource = ds
buyer_table.databind()



If ((Buyer_TableDataGridView.Columns.Count = 0) Or (Buyer_TableDataGridView.Rows.Count = 0)) Then
         Exit Sub
     End If

Dim dset As New DataSet

     dset.Tables.Add()

  For i As Integer = 0 To Buyer_TableDataGridView.ColumnCount - 1
         dset.Tables(0).Columns.Add(Buyer_TableDataGridView.Columns(i).HeaderText)
     Next
     'add rows to the table
     Dim dr1 As DataRow
     For i As Integer = 0 To Buyer_TableDataGridView.RowCount - 1
         dr1 = dset.Tables(0).NewRow
         For j As Integer = 0 To Buyer_TableDataGridView.Columns.Count - 1
             dr1(j) = Buyer_TableDataGridView.Rows(i).Cells(j).Value
         Next
         '    dset.Tables(0).Rows.Add(dr1)
     Next


     Dim excel As New Microsoft.Office.Interop.Excel.Application
     Dim wBook As Microsoft.Office.Interop.Excel.Workbook
     Dim wSheet As Microsoft.Office.Interop.Excel.Worksheet

     wBook = excel.Workbooks.Add()
     wSheet = wBook.ActiveSheet()

     Dim dt As System.Data.DataTable = dset.Tables(0)
     Dim dc As System.Data.DataColumn
     Dim dr As System.Data.DataRow
     Dim colIndex As Integer = 0
     Dim rowIndex As Integer = 0

     For Each dc In dt.Columns
         colIndex = colIndex + 1
         excel.Cells(1, colIndex) = dc.ColumnName
     Next

     For Each dr In dt.Rows
         rowIndex = rowIndex + 1
         colIndex = 0
         For Each dc In dt.Columns
             colIndex = colIndex + 1
             excel.Cells(rowIndex + 1, colIndex) = dr(dc.ColumnName)

         Next
     Next


can someone has a useful code to make this kind of output in excel using two tables?
Posted
Updated 7-Sep-14 19:42pm
v4
Comments
Sergey Alexandrovich Kryukov 2-Sep-14 23:29pm    
It's unclear how those lines with repeated words "parent" and "child" explains what you want to achieve.
—SA
NekoNao 2-Sep-14 23:43pm    
Example.
Customer Name Age Address Gender Order1 Order1 Order1
Order1 Order1 Order1
Order1 Order1 Order1
Order1 Order1 Order1
Customer Name Age Address Gender Order1 Order1 Order1
Order1 Order1 Order1
Order1 Order1 Order1
Order1 Order1 Order1
Sergey Alexandrovich Kryukov 3-Sep-14 1:03am    
I can only repeat my comment above...
—SA
Gihan Liyanage 2-Sep-14 23:34pm    
Do you want Nested Grid view ?
NekoNao 2-Sep-14 23:40pm    
something like that but I need to export it to excel with the likes of a nested datagridview format

1 solution

If you are thinking about nested grid view, here is an example with a demo.

http://www.aspsnippets.com/Articles/Nested-GridView-Example-in-ASPNet-using-C-and-VBNet.aspx[^]
 
Share this answer
 
Comments
NekoNao 2-Sep-14 23:41pm    
but i need it in VB.Net not in ASP.Net, will it be the same? And how about in exporting it?
Sergey Alexandrovich Kryukov 3-Sep-14 1:08am    
There is not such thing as "VB.NET not in ASP.NET". Do you really understand what those terms mean?
This is like saying, "I want a car, not Ford". Then you should say: I want System.Windows.Forms.DataGridView.
But you are right on one thing: the answer is off-topic; Gihan should have noticed that from the question.
—SA
NekoNao 3-Sep-14 3:19am    
I am really quite new about programming so I guess I have my fault there. So do you have any code or links that can export NESTED DATAGRIDVIEWS?

tried this but not working, http://lakshmik.blogspot.com/2006/04/aspnet-export-to-excelword-from-nested.html
Gihan Liyanage 3-Sep-14 1:14am    
You are correct Sergey, I have just answered, not thought about windows forms & web. Any way thanx for advising. This is lesson for me from you, as usual. I accepted your contention.
Sergey Alexandrovich Kryukov 3-Sep-14 3:24am    
Thank you for understanding.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900