Solved

ado.net to excel in vb.net (urgent)

Posted on 2004-08-10
4
1,251 Views
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)
                    Next
                    cellnumber += 1
  Next

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


CODE:


dim sqlconnection as new sqlconnection(strconnstring)
   Dim da = New SqlClient.SqlDataAdapter
            ds = New DataSet
            SqlConnection.Open()
            With da
                .SelectCommand = New SqlClient.SqlCommand(QueryStmt, SqlConnection)
                .Fill(ds, "QueryResults")
            End With
            SqlConnection.Close()
            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
            Next

            'oSheet.Range("A2").CopyFromRecordset(dt)
            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)
                    Next
                    cellnumber += 1
                Next

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

            End If
0
Comment
Question by:raghu11
  • 3
4 Comments
 
LVL 25

Accepted Solution

by:
RonaldBiemans earned 250 total points
ID: 11770649
0
 

Author Comment

by:raghu11
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
Thanks

            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)
            objConn.Open()

            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)" & ")"
                Else
                    objCmd.CommandText = objCmd.CommandText & "[" & dt.Columns.Item(n - 1).ColumnName & "] varchar(255)" & ","
                End If

            Next
            objCmd.ExecuteNonQuery()
            Dim parametersstring As String
            For n = 0 To dt.Columns.Count - 1
                If n = dt.Columns.Count - 1 Then
                    parametersstring = parametersstring & "?"
                Else
                    parametersstring = parametersstring & "?,"
                End If
            Next

            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)
                Next
            End With
            Dim daole As New System.Data.OleDb.OleDbDataAdapter
            daole.InsertCommand = objCmd
            daole.Update(dt)
            objConn.Close()
0
 

Author Comment

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

Author Comment

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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

713 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