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:
Hi everyone!

I have the following problem:
I have to fill an Excel Sheet from a DataSet.
The DataSet is filled by a MS SQL Server 2008. (That's not the problem..)

At first, here is my sourcecode:

 Dim sqlConnection As SqlClient.SqlConnection = New SqlClient.SqlConnection(readSettings.getSqlCon(False))
     Dim sqlcmd As SqlCommand
     Dim dsleistungen As DataSet
     Try
         sqlConnection.Open()

         Dim cmd As String = "select * from dbo.Leistungen order by beginn"
         sqlcmd = New SqlClient.SqlCommand(cmd, sqlConnection)

         Dim da As New SqlDataAdapter(sqlcmd)

         dsleistungen = New DataSet
         da.Fill(dsleistungen)

     Catch ex As Exception
         MessageBox.Show(Convert.ToString(ex))
     Finally
         sqlConnection.Close()
     End Try

'Here is the section with the "Excel-Fill-Code"

     Dim objBooks As Excel.Workbooks
     Dim objSheets As Excel.Sheets
     Dim objSheet As Excel._Worksheet
     Dim range As Excel.Range

     ' Create a new instance of Excel and start a new workbook.
     objApp = New Excel.Application()
     objBooks = objApp.Workbooks
     objBook = objBooks.Add
     objSheets = objBook.Worksheets
     objSheet = objSheets(1)

     'Get the range where the starting cell has the address
     'm_sStartingCell and its dimensions are m_iNumRows x m_iNumCols.
     range = objSheet.Range("A1", Reflection.Missing.Value)
     range = range.Resize(dsleistungen.Tables(0).Rows.Count - 1, dsleistungen.Tables(0).Columns.Count - 1)

     'Create an array.
     Dim saRet(dsleistungen.Tables(0).Rows.Count - 1, dsleistungen.Tables(0).Columns.Count - 1) As String

     'Fill the array.
     Dim iRow As Integer
     Dim iCol As Integer

     For iRow = 0 To dsleistungen.Tables(0).Rows.Count - 1
         For iCol = 0 To dsleistungen.Tables(0).Columns.Count - 1

             saRet(iRow, iCol) = dsleistungen.Tables(0).Rows(iRow).Item(iCol)
         Next iCol
     Next iRow

     'Set the range value to the array.
     range.Value = saRet

     'Return control of Excel to the user.
     objApp.Visible = True
     objApp.UserControl = True

     'Clean up a little.
     range = Nothing
     objSheet = Nothing
     objSheets = Nothing
     objBooks = Nothing


I have the code from Microsoft, here is a Link: KB302094

I edited the Code. Excel should be filled up with my DataSet (a Table of my Database).
Instead I get one half-finished line with useless entrys. The "Select * from dbo.Leistungen" retourns only 13 colums with 5 rows.

What's wrong with my code?^^

>> Thanks!
Posted

[Moved from Answers]

Hi.
The meaning is that my Sql-Command is working.
But when I look at my Excel sheet, it isn't filled up.

thatraja wrote:
what's the meaning? I think if the select query against sql database returns 13 columns with 5 rows then the problem is not in excel/vb.net things. Your database may contains only 13 columns with 5 rows.


My Database only contains 13 colums and 5 rows (the structur..). The select is working fine. When I look into my DataSet i see all database entrys (from my Select...).

But when i write the Excel Sheet with this Code, I can only see 1 half-filled row.
When I look at the String-Arry saRet, it "contains" my DataSet:
saRet(X,Y) -> X is the row and Y is the Column. From my side, it looks pretty good!...

----------------------------------------------------------------------
Now the smaple form Microsoft doesn't work any more.
-> Now the code also writes only one line...

Maybe there is a compatiblity problem? -> I am using Microsoft Office 2010 and Windows 7 x64...?
----------------------------------------------------------------------

Solution

Hi.

Okay, i have solved the problem:
I defined the wrong range...

For everyone who want to do the same example, here is my correct source-code!

(You need to add some references to your project... you can get the infos here: http://support.microsoft.com/kb/302094/en[^]


Imports System.Data.SQLClient
Imports Microsoft.Office.Interop

        Dim sqlConnection As SqlClient.SqlConnection = New SqlClient.SqlConnection("Connection-String")
        
Dim sqlcmd As SqlCommand

        Dim ds As DataSet



        Try
            sqlConnection.Open()

            Dim cmd As String = ""  'Put your Select-cmd here!


            sqlcmd = New SqlClient.SqlCommand(cmd, sqlConnection)

            Dim da As New SqlDataAdapter(sqlcmd)

            ds = New DataSet
            da.Fill(ds)

        Catch ex As Exception
            MessageBox.Show(Convert.ToString(ex))
        Finally
            sqlConnection.Close()
        End Try


        Dim objApp As Excel.Application
        Dim objBook As Excel._Workbook

        Dim objBooks As Excel.Workbooks
        Dim objSheets As Excel.Sheets
        Dim objSheet As Excel._Worksheet
        Dim range As Excel.Range


        objApp = New Excel.Application()
        objBooks = objApp.Workbooks
        objBook = objBooks.Add
        objSheets = objBook.Worksheets
        objSheet = objSheets(1)
        range = objSheet.Range("A1", Reflection.Missing.Value)  'Starting-Cell

        Dim RowCount, ColumCount As Integer
        RowCount = ds.Tables(0).Rows.Count
        ColumCount = ds.Tables(0).Columns.Count

        range = range.Resize(RowCount, ColumCount)


        RowCount = RowCount - 1
        ColumCount = ColumCount - 1

        'Create an array.
        Dim saRet(RowCount, ColumCount) As String



        ''Fill the array.
        Dim iRow As Integer
        Dim iCol As Integer

        For iRow = 0 To RowCount
            For iCol = 0 To ColumCount
                saRet(iRow, iCol) = ds.Tables(0).Rows(iRow).Item(iCol)
            Next iCol
        Next iRow


        range.Value = saRet

        objApp.Visible = True
        objApp.UserControl = True

        'Clean up a little.
        range = Nothing
        objSheet = Nothing
        objSheets = Nothing
        objBooks = Nothing


Thanks thatraja for your help!
Z
 
Share this answer
 
v3
Comments
thatraja 26-Dec-10 6:08am    
I'm Glad you solved that. Have a 5.
BTW I have modified your answer message with previous 2 answer messages. Actually use comment for non-answer messages. cheers. :-)
The.Z wrote:
The "Select * from dbo.Leistungen" retourns only 13 colums with 5 rows.


what's the meaning? I think if the select query against sql database returns 13 columns with 5 rows then the problem is not in excel/vb.net things. Your database may contains only 13 columns with 5 rows.

So
check the database values (Columns & Rows),
dataset (Columns & Rows).

Let us know the things again.
 
Share this answer
 

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