I need to export a lot (nearly a million) of data everyday from SQLServer to Excel. The data is being processed through a stored procedure then I put them on the DataSet and tried to export using this code:
`
Private Sub ExportToExcel(ByVal dtTemp As System.Data.DataTable, ByVal filepath As String)
Dim strFileName As String = filepath
Dim _excel As New Excel.Application
Dim wBook As Excel.Workbook
Dim wSheet As Excel.Worksheet
wBook = _excel.Workbooks.Add()
wSheet = wBook.ActiveSheet()
Dim dt As System.Data.DataTable = dtTemp
Dim dc As System.Data.DataColumn
Dim dr As System.Data.DataRow
Dim colIndex As Integer = 0
Dim rowIndex As Integer = 0
For Each dc In dt.Columns
colIndex = colIndex + 1
wSheet.Cells(1, colIndex) = dc.ColumnName
Next
For Each dr In dt.Rows
rowIndex = rowIndex + 1
colIndex = 0
For Each dc In dt.Columns
colIndex = colIndex + 1
wSheet.Cells(rowIndex + 1, colIndex) = dr(dc.ColumnName)
Next
Next
wSheet.Columns.AutoFit()
wBook.SaveAs(strFileName)
ReleaseObject(wSheet)
wBook.Close(False)
ReleaseObject(wBook)
_excel.Quit()
ReleaseObject(_excel)
GC.Collect()
End Sub`
Is there any faster way for this?
How about DataSet to Clipboard then paste it to excel?
One way is to save the DataSet as an XML file:
myDataSet.WriteXml("c:\file.xml")
There is a much faster way involving SQL Data Pump (Import Export DTS). You can save a DTS Package (that exports data from SQL to Excel about 1000 rows per second) then run that package using SQL Server Agent. This way, you don't have to iterate to all the rows and columns one by one and you don't need to have a VB code.
Imports System.Data.Sql
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Linq
Imports Excel = Microsoft.Office.Interop.Excel
Imports Microsoft.Office.Core
Imports Microsoft.VisualBasic.DateAndTime
Private Sub Btn_Add_Click(sender As Object, e As EventArgs) Handles btn_Add.Click
DataSetToExcel(ReturnDS("Select * from Membership"), "Test")
End Sub
Public Shared Function DataSetToExcel(myDS As DataSet, fileName As String)
Try
Dim Excel As Object = CreateObject("Excel.Application")
With Excel
.SheetsInNewWorkbook = 1
.Workbooks.Add()
.Worksheets(1).Select()
Dim i As Integer = 1
For col = 0 To myDS.Tables(0).Columns.Count - 1
.Cells(1, i).value = myDS.Tables(0).Columns(col).ColumnName
.Cells(1, i).EntireRow.Font.Bold = True
i += 1
Next
i = 2
Dim j As Integer = 1
For col = 0 To myDS.Tables(0).Columns.Count - 1
i = 2
For row = 0 To myDS.Tables(0).Rows.Count - 1
.Cells(i, j).Value = myDS.Tables(0).Rows(row).ItemArray(col)
i += 1
Next
j += 1
Next
Dim MyTime As String
MyTime = System.DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss")
.Application.DisplayAlerts = False
.ActiveCell.Worksheet.SaveAs(fileName & "_" & MyTime & ".xlsx")
.Workbooks.Close()
End With
Catch ex As Exception
End Try
Return ""
End Function
Public Shared Function ReturnDS(Query As String) As DataSet
Dim con = New SqlConnection(Crypto.ConString)
Dim cmd As New SqlCommand()
Dim myDA As New SqlDataAdapter()
Dim myDS As New DataSet 'The DataSet you created.
cmd.Connection = con
cmd.CommandText = Query
cmd.CommandType = CommandType.Text
myDA.SelectCommand = cmd
myDA.Fill(myDS)
Return myDS
End Function
Related
This may have been asked and answered several times, but I have been unable to find the answer.
In CbProdukt1_SelectedIndexChanged(), I need the associated value from an array from another sub:
Private Sub CbProdukt1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CbProdukt1.SelectedIndexChanged
With Me.LblEnhed1
.Text = Enhed(CbProdukt1.SelectedIndex)
.Location = New Point(230, 150)
End With
Me.Controls.Add(LblEnhed1)
End Sub
This would be easy enough if Enhed() had been public. But it is not as it is dimensioned and retrieved from Sqlite in another sub:
Dim count As Integer
sql = "SELECT COUNT(varenr) AS varenrcount FROM '" & Varedatabase & "'"
cmd = New SQLiteCommand(sql, conn)
count = cmd.ExecuteScalar
sql = "SELECT * FROM '" & Varedatabase & "'"
cmd = New SQLiteCommand(sql, conn)
reader = cmd.ExecuteReader
Dim a = 0
Dim Varenr(count + 5) As String
Dim Varenavn(count + 5) As String
Dim Enhed(count + 5) As String
While (reader.Read())
Varenr(a) = reader("varenr")
Varenavn(a) = reader("Varenavn")
Enhed(a) = reader("Enhed")
CbProdukt1.Items.Add(varenavn(a))
a += 1
End While
Public declarations are only allowed prior to subs at the top of the class.
So how do I get the Enhed(a) from sub to sub?
My output appends the current column in datagridview whereas i wanted to view the output from excel file to the datagridview in series.What shall i do?
Can i get the solution for the same as i cant import excel file in new datagridview the requirement is to import all excel cells to corresponding in the current datagridview.
Private Sub Button2_Click_1(sender As Object, e As EventArgs) Handles Button2.Click
Dim excel As String
Dim OpenFileDialog As New OpenFileDialog
Dim conn As OleDbConnection
Dim dta As OleDbDataAdapter
DataGridView2.Rows.Clear()
Dim dts As DataSet
OpenFileDialog.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.Desktop
OpenFileDialog.Filter = "All Files (*.*)|*.*|Excel files (*.xlsx)|*.xlsx|CSV Files (*.csv)|*.csv|XLS Files (*.xls)|*xls"
If (OpenFileDialog.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK) Then
Dim fi As New FileInfo(OpenFileDialog.FileName)
Dim FileName As String = OpenFileDialog.FileName
excel = fi.FullName
conn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excel + ";Extended Properties=Excel 12.0;")
dta = New OleDbDataAdapter("Select * From [Sheet1$]", conn)
dts = New DataSet
dta.Fill(dts, "[Sheet1$]")
DataGridView2.DataSource = dts
DataGridView2.DataMember = "[Sheet1$]"
conn.Close()
With DataGridView2
.RowHeadersVisible = False
.Columns(0).HeaderCell.Value = "Enrollment No"
.Columns(1).HeaderCell.Value = "Name"
.Columns(2).HeaderCell.Value = "Sub Name"
.Columns(3).HeaderCell.Value = "Examination"
.Columns(4).HeaderCell.Value = "Semester"
.Columns(5).HeaderCell.Value = "Out of"
.Columns(6).HeaderCell.Value = "Marks Obtained"
.Columns(7).HeaderCell.Value = "Grade"
End With
DataGridView2.Columns.Item(0).Width = 100
DataGridView2.Columns.Item(1).Width = 120
DataGridView2.Columns.Item(2).Width = 100
DataGridView2.Columns.Item(3).Width = 70
DataGridView2.Columns.Item(4).Width = 90
DataGridView2.Columns.Item(5).Width = 50
DataGridView2.Columns.Item(6).Width = 70
DataGridView2.Columns.Item(7).Width = 70
DataGridView2.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
With DataGridView2
.RowHeadersVisible = False
.Columns(0).HeaderCell.Value = "Enrollment No"
.Columns(1).HeaderCell.Value = "Name"
.Columns(2).HeaderCell.Value = "Sub Name"
.Columns(3).HeaderCell.Value = "Examination"
.Columns(4).HeaderCell.Value = "Semester"
.Columns(5).HeaderCell.Value = "Out of"
.Columns(6).HeaderCell.Value = "Marks Obtained"
.Columns(7).HeaderCell.Value = "Grade"
End With
DataGridView2.Columns.Item(0).Width = 100
DataGridView2.Columns.Item(1).Width = 120
DataGridView2.Columns.Item(2).Width = 100
DataGridView2.Columns.Item(3).Width = 70
DataGridView2.Columns.Item(4).Width = 90
DataGridView2.Columns.Item(5).Width = 50
DataGridView2.Columns.Item(6).Width = 70
DataGridView2.Columns.Item(7).Width = 70
DataGridView2.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
With Me.DataGridView2
.RowsDefaultCellStyle.BackColor = Color.WhiteSmoke
.AlternatingRowsDefaultCellStyle.BackColor = Color.Lavender
End With
MsgBox("Importing Data has been Cancelled")
End If
End Sub
Since the excel file appears to be what you want to display in the datagrid I think that you should be able to do like the below. Make the Datatable the datasource for your datagrid. IE your datagridview is bound to your datatable.
Private Sub Button2_Click_1(sender As Object, e As EventArgs) Handles Button2.Click
Dim excel As String
Dim OpenFileDialog As New OpenFileDialog
Dim conn As OleDbConnection
Dim dta As OleDbDataAdapter
DataGridView2.Rows.Clear()
Dim dt As New DataTable
OpenFileDialog.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.Desktop
OpenFileDialog.Filter = "All Files (*.*)|*.*|Excel files (*.xlsx)|*.xlsx|CSV Files (*.csv)|*.csv|XLS Files (*.xls)|*xls"
If (OpenFileDialog.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK) Then
Dim fi As New FileInfo(OpenFileDialog.FileName)
Dim FileName As String = OpenFileDialog.FileName
excel = fi.FullName
dim conn as New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excel + ";Extended Properties=Excel 12.0;")
dim dta as new New OleDbDataAdapter("Select * From [Sheet1$]", con
da.Fill(dt)
DataGridView2.Datasource = dt
End Sub
I have a database in Microsoft SQL with 3 columns. The last column is a date which has the format: dd-mm-yyyy hh:mm-ss (ex: 20-May-15 22:31:38).
I managed to export the other 2 columns in Excel, but when I try to import the last column it gives me the next error: "Run-time error '3265': Application-defined or object-defined error".
My vba script is:
Private Sub CommandButton1_Click()
Dim con As ADODB.Connection
Dim rs As ADODB.Recordset
Dim r As Integer
Set con = New ADODB.Connection
Dim s As String
con.ConnectionString = "Provider=SQLOLEDB;Data Source=AEG-PC\WINCC; Initial Catalog=DBRapPlopeni; User ID=AEGRap; Password=1qaz2wsx; Trusted_Connection=yes"
con.Open
Set rs = New ADODB.Recordset
rs.ActiveConnection = con
rs.Open "EXEC SotoredProcedure 20,05,2015"
r = 5
Do While Not rs.EOF
Cells(r, 2) = rs.Fields(0)
Cells(r, 3) = rs.Fields(1)
Cells(r, 4) = rs.Fields(2) ' this field of date which cannot be imported
rs.MoveNext
r = r + 1
Loop
End Sub
I tried to convert date field in a string format but it didn't work for me.
Does anyone know how to solve this problem?
Many thanks in advance!
Had a similar issue and I used the GetRows method to store the data in an array and manipulate them from there.
Something like the below:
Replace:
Do While Not rs.EOF
Cells(r, 2) = rs.Fields(0)
Cells(r, 3) = rs.Fields(1)
Cells(r, 4) = rs.Fields(2) ' this field of date which cannot be imported
rs.MoveNext
r = r + 1
Loop
with
dim ar() as variant
ar = rs.GetRows
and then do a for loop to store the array data in the required cells.
for r =0 to ubound(ar,2) 'your array has 0 to ubound(ar,2) rows
cells(r,2) = ar(1,r)
cells(r,3) = ar(2,r)
cells(r,4) = ar(3,r)
next r
Hope this helps.
I'm trying to import Excel in datagridview using this methods in the code below. But I have an error in this line "invalidOperationException" can't get the data to show up
cnnExcel.Open()
and here is the whole code
comboBox as cmbExcel
Having some if condition for supporting depend on excel version (2003 - 2013)
Imports System.Data.OleDb
Public Class Form1
Dim cnnExcel As New OleDbConnection
Public Function GetExccelSheetNames() As String()
Dim ConStr As String = ""
Dim dt As DataTable = Nothing
Dim opExcel As New OpenFileDialog
opExcel.Filter = "(*.xlsx)|*.xlsx|(*.xls)|*.xls"
opExcel.ShowDialog()
Dim pathExcel As String = opExcel.FileName
If pathExcel.Trim = "" Then
MessageBox.Show("Please Select Excel File !")
Return Nothing
Else
Dim Ext As String = pathExcel.Substring(pathExcel.LastIndexOf(".") + 1)
If Ext.Length = 3 Then
ConStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + pathExcel + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1';"
ElseIf Ext.Length = 4 Then
ConStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + pathExcel + ";Extended Properties='Excel 12.0 xml;HDR=YES';"
End If
cnnExcel = New OleDbConnection(ConStr)
cnnExcel.Open()
dt = cnnExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
If dt Is Nothing Then
Return Nothing
End If
Dim excelSheetNames As [String]() = New [String](dt.Rows.Count - 1) {}
Dim i As Integer = 0
For Each row As DataRow In dt.Rows
excelSheetNames(i) = row("TABLE_NAME").ToString()
i += 1
Next
cnnExcel.Close()
Return excelSheetNames
End If
End Function
Added a Button as btnBrows to brows excel file from any location in local drive
Private Sub btnBrowse_Click(sender As Object, e As EventArgs) Handles btnBrowse.Click
cmbsheet.DataSource = GetExccelSheetNames()
End Sub
Dim dt As New DataTable
Then Finally having a button to view the excel in datagridview
Private Sub btnShow_Click(sender As Object, e As EventArgs) Handles btnShow.Click
Dim cmd As New OleDbCommand("Select * from [" + cmbsheet.SelectedValue.ToString() + "]", cnnExcel)
cnnExcel.Open()
dt.Clear()
dt.Load(cmd.ExecuteReader())
cnnExcel.Close()
DataGridView1.DataSource = dt
End Sub
End Class
Use this as a reference. It's 100% working. I'm using this in one of my applications. Put this code on your import button.
Dim result As DialogResult = OpenFileDialog1.ShowDialog()
Dim path As String = OpenFileDialog1.FileName
Me.TextBox1.Text = path.ToString
Try
Me.dgvFile.DataSource = Nothing
Dim MyConnection As System.Data.OleDb.OleDbConnection
Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
MyConnection = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & Me.TextBox1.Text & "';Extended Properties=Excel 8.0;")
MyCommand = New System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", MyConnection)
MyCommand.TableMappings.Add("Table", "Net-informations.com")
DtSet = New System.Data.DataTable
MyCommand.Fill(DtSet)
Me.dgvFile.DataSource = DtSet
MyConnection.Close()
MessageBox.Show("File successfully imported")
Catch ex As Exception
MessageBox.Show("Error")
End Try
I am writing an application which uses the Microsoft.Office.Interop.Excel assembly to export data to an Excel spreadsheet.
However, when I perform the export - I get the following error:
"method's type signature is not interop compatible"
Here is my code.
Imports System.Data.SqlClient
Imports System.Data
Imports System.IO.Directory
Imports Microsoft.Office.Interop.Excel
Imports Microsoft.Office.Interop
Public Class ViewBillForm
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim dataAdapter As New SqlClient.SqlDataAdapter()
Dim dataSet As New DataSet
Dim command As New SqlClient.SqlCommand
Dim datatableMain As New System.Data.DataTable()
Dim connection As New SqlClient.SqlConnection
connection.ConnectionString = "server=xxx\SQLEXPRESS; database=xxx; integrated security=yes"
command.Connection = connection
command.CommandType = CommandType.Text
command.CommandText = "Select * from [dbo].[xxx]"
dataAdapter.SelectCommand = command
Dim f As FolderBrowserDialog = New FolderBrowserDialog
Try
If f.ShowDialog() = DialogResult.OK Then
System.Threading.Thread.CurrentThread.CurrentCulture = _
System.Globalization.CultureInfo.CreateSpecificCulture("en-US")
Dim oExcel As Excel.Application
Dim oBook As Excel.Workbook
Dim oSheet As Excel.Worksheet
oExcel = CreateObject("Excel.Application")
oBook = oExcel.Workbooks.Add(Type.Missing)
oSheet = oBook.Worksheets(1)
Dim dc As System.Data.DataColumn
Dim dr As System.Data.DataRow
Dim colIndex As Integer = 0
Dim rowIndex As Integer = 0
connection.Open()
dataAdapter.Fill(datatableMain)
connection.Close()
For Each dc In datatableMain.Columns
colIndex = colIndex + 1
oSheet.Cells(1, colIndex) = dc.ColumnName
Next
For Each dr In datatableMain.Rows
rowIndex = rowIndex + 1
colIndex = 0
For Each dc In datatableMain.Columns
colIndex = colIndex + 1
oSheet.Cells(rowIndex + 1, colIndex) = dr(dc.ColumnName)
Next
Next
Dim fileName As String = "\Export" + ".xls"
Dim finalPath = f.SelectedPath + fileName
Textbox1.Text = finalPath
oSheet.Columns.AutoFit()
oBook.SaveAs(finalPath, XlFileFormat.xlWorkbookNormal, Type.Missing, _
Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlExclusive, _
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing)
ReleaseObject(oSheet)
oBook.Close(False, Type.Missing, Type.Missing)
ReleaseObject(oBook)
oExcel.Quit()
ReleaseObject(oExcel)
'Some time Office application does not quit after automation:
'so i am calling GC.Collect method.
GC.Collect()
MessageBox.Show("Export done successfully!")
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK)
End Try
End Sub
Private Sub ReleaseObject(ByVal o As Object)
Try
While (System.Runtime.InteropServices.Marshal.ReleaseComObject(o) > 0)
End While
Catch
Finally
o = Nothing
End Try
End Sub
End Class
Can anyone help me out with where I am going wrong?
Thanks,