Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27016

VS 2010 Send DataSet Table to Excel Sheet via Excel server and via OleDBConnection

$
0
0
Hi

im having a problem doing bulk transfers, i need a couple of solutions for different situations

i can open excel using interop without a problem and i can open a ole connection to an Excel file no problem, but please inform me if its done wrong or not coded well.

Problem 1:

How can i write the whole dataset table to an excel sheet in 1 move when using the excel server/program ie. interop



Problem 2:

How can i write the whole dataset table to an XLS file using ole db connection



ive tried various ways but i just cant get it

Code:

Code:

Imports System.IO
Imports Microsoft.Office.Interop
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim TestFile As String = My.Computer.FileSystem.SpecialDirectories.Desktop & "test.xlsx"
        Dim ConnectionString As String = " Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Main Admin\\Desktop\\Test.xlsx;Extended Properties=""Excel 12.0 Xml;HDR=No"";"
        Try
            Dim MyConnection As System.Data.OleDb.OleDbConnection
            Dim DtSet As System.Data.DataSet
            Dim MyCommand As System.Data.OleDb.OleDbDataAdapter

            MyConnection = New System.Data.OleDb.OleDbConnection(ConnectionString)
            MyCommand = New System.Data.OleDb.OleDbDataAdapter("select * from [test$]", MyConnection)
            MyCommand.TableMappings.Add("Table", "TestTable")
            DtSet = New System.Data.DataSet
            MyCommand.Fill(DtSet)
            DataGridView1.DataSource = DtSet.Tables(0)
            MyConnection.Close()

            Dim xlApp As New Excel.Application
            Dim xlSheet As Excel.Worksheet
            xlApp.Visible = True
            xlApp.Workbooks.Add()
            xlSheet = xlApp.ActiveSheet

           
            xlSheet.Range("C3").CopyFromRecordset(DatagridviewToDataset(DataGridView1).Tables(0).DataSet)

        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
End Sub

DatagridviewToDataset is a function i found here in vb forums, credit to 'dminder'
it puts data from datagridview to a dataset

thanks

Viewing all articles
Browse latest Browse all 27016

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>