Click here to Skip to main content
15,908,437 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Everyone Good Morning
I have a Program in VB.Net that will Populate Data from Mysql into the Datagridview.
and I have also a button called Export and It will Export Datagridview Data in Excel Format like this.


http://s32.postimg.org/xr4suyzz9/image.jpg[^]

but my our Prof. like this Format.


http://s32.postimg.org/e9x4bqlyd/image.jpg[^]


How can I achive that? like

1. Put a Center Header

2. Put a .00 at the End of the Number of a Number Column

3. Find the Last Cell in a Column and Sum It.

I hope someone would help me


Here is my code in Export Button

VB.NET
If DataGridView1.Rows.Count = 0 Then
              MsgBox("Nothing to Export")
          Else
              Dim ExcelApp As Object, ExcelBook As Object
              Dim ExcelSheet As Object
              Dim i As Integer
              Dim j As Integer

              ExcelApp = CreateObject("Excel.Application")
              ExcelBook = ExcelApp.WorkBooks.Add
              ExcelSheet = ExcelBook.WorkSheets(1)
              With ExcelSheet
                  For Each column As DataGridViewColumn In DataGridView1.Columns
                      .cells(1, column.Index + 1) = column.HeaderText
                  Next
                  For i = 1 To Me.DataGridView1.RowCount
                      .cells(i + 1, 1) = Me.DataGridView1.Rows(i - 1).Cells("ItemCode").Value
                      For j = 1 To DataGridView1.Columns.Count - 1
                          .cells(i + 1, j + 1) = DataGridView1.Rows(i - 1).Cells(j).Value

                      Next


                  Next
              End With
              ExcelApp.Visible = True
              ExcelSheet = Nothing
              ExcelBook = Nothing
              ExcelApp = Nothing

          End If


What I have tried:

As of Now Nothing Happens and I tried Everuthing
Posted
Updated 11-May-16 9:30am
Comments
Richard MacCutchan 11-May-16 3:11am    
and I tried Everuthing
Well obviously not, as your program is not yet working. You need to read the documentation on the Excel Interop (Google for it) to see what methods to call in order to get the results you are asked for.
Paul Edward Pagente 11-May-16 4:41am    
Sir this is my First Time to do this

1 solution

you wrong, your 2 pictures do not show the same data, we can't device any thing with this.
Advice: use pictures in questions only for graphics, not fir text. Type the examples of data.
Explain the goal and what is wrong in your code(don't say it is not working, we already know it). "this x is on wrong position", "this y needs 2 decimals"
.
If you can do spmething directly in excel, use the macro recorder to see how Excel translate your actions in VB.

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

Use the debugger to see what your code is doing.

Quote:
I tried Everuthing
I can insure you that you didn't
 
Share this answer
 
v2

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