Solved to excel in (urgent)

Posted on 2004-08-10
Last Modified: 2008-03-10
I was trying to show the results of a query in excel file. I was trying to bind the search results to excel file but some how i could not do that. In the following code, to the datatable i assigned  ds.Tables.Item("QueryResults"), then i was trying to put the contents of datatable in excel file with the following lines:
  For Each row In dt.Rows
                    For n = 1 To dt.Columns.Count
                        oSheet.Cells(cellnumber, n).value = row.Item(n - 1)
                    cellnumber += 1

Because of too many rows it is taking long time to write to the excel file. Is there a simple way of directly binding the data in datatable in excel file. Thanks


dim sqlconnection as new sqlconnection(strconnstring)
   Dim da = New SqlClient.SqlDataAdapter
            ds = New DataSet
            With da
                .SelectCommand = New SqlClient.SqlCommand(QueryStmt, SqlConnection)
                .Fill(ds, "QueryResults")
            End With
            dt = ds.Tables.Item("QueryResults")
          Dim EXL As New Excel.Application
            If EXL Is Nothing Then
                MsgBox("Could not start excel")
                Exit Sub
            End If
            Dim wSheet As New Excel.Worksheet
            Dim oExcel As Object
            Dim oBook As Object

  'Dim oSheet As Object
            Dim oSheet As Excel.Worksheet

            oExcel = CreateObject("Excel.Application")
            oBook = oExcel.Workbooks.Add
            oSheet = oBook.Worksheets(1)
            Dim n As Int32
            For n = 1 To dt.Columns.Count
                oSheet.Cells(1, n).Value = dt.Columns.Item(n - 1).ColumnName

            Dim row As DataRow
            Dim cellnumber As Integer = 2
            If dt.Rows.Count > 0 Then
                For Each row In dt.Rows
                    For n = 1 To dt.Columns.Count
                        oSheet.Cells(cellnumber, n).value = row.Item(n - 1)
                    cellnumber += 1

                oBook.SaveAs(System.Windows.Forms.Application.StartupPath & "\" & Format(Now, "yyyyMMddHHmmss") & ".xls")

            End If
Question by:raghu11
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
LVL 25

Accepted Solution

RonaldBiemans earned 250 total points
ID: 11770649

Author Comment

ID: 11775173
I am dynamically building the excel sheet. I mean i give the option for the users to select the fields from list box and based on the fields select query is executed and a dataset is created. I implemented the following code but some how it creates excel file with column headings but no data in the excel. can someone help me

            Dim sConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
                        "Data Source=" & System.Windows.Forms.Application.StartupPath & _
                        "\Book7.xls;Extended Properties=Excel 8.0;"
            Dim objConn As New System.Data.OleDb.OleDbConnection(sConnectionString)

            Dim objCmd As New System.Data.OleDb.OleDbCommand
            objCmd.Connection = objConn
            objCmd.CommandText = "create table test  ("

            For n = 1 To dt.Columns.Count
                 If n = dt.Columns.Count Then
                    objCmd.CommandText = objCmd.CommandText & "[" & dt.Columns.Item(n - 1).ColumnName & "] varchar(255)" & ")"
                    objCmd.CommandText = objCmd.CommandText & "[" & dt.Columns.Item(n - 1).ColumnName & "] varchar(255)" & ","
                End If

            Dim parametersstring As String
            For n = 0 To dt.Columns.Count - 1
                If n = dt.Columns.Count - 1 Then
                    parametersstring = parametersstring & "?"
                    parametersstring = parametersstring & "?,"
                End If

            objCmd.CommandText = "Insert into test values(" & parametersstring & ")"
            With objCmd.Parameters
                For n = 0 To dt.Columns.Count - 1
                    .Add("@" & dt.Columns.Item(n).ColumnName, OleDb.OleDbType.VarChar, 255, dt.Columns.Item(n).ColumnName)
            End With
            Dim daole As New System.Data.OleDb.OleDbDataAdapter
            daole.InsertCommand = objCmd

Author Comment

ID: 11775423
I used daole.update(ds) but it was throwing exception because of which i used datatable dt

Author Comment

ID: 11775648
adding this statement helped in solving the issue.  da.AcceptChangesDuringFill = False

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In a recent question ( here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question