Error with exporting Excel file from Database (vb.net) - mysql

System.Runtime.InteropServices.COMException: 'Retrieving the COM class
factory for component with CLSID
{00020819-0000-0000-C000-000000000046} failed due to the following
error: 80040154 Class not registered (Exception from HRESULT:
0x80040154 (REGDB_E_CLASSNOTREG)).'
I was just trying to export a Database using vb.net to an Excel file, I have tried to install and reinstall microsoft excel, adding and readding the imports of microsoft. can someone please helpppp
code:
Imports Excel = Microsoft.Office.Interop.Excel
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
Dim cnn As SqlConnection
Dim cnnst As String
Dim sql As String
Dim i, j As Integer
Dim xlapp As New Excel.Application
Dim xlworkbook As New Excel.Workbook
Dim xlworksheet As New Excel.Worksheet
Dim misvalue As Object = Reflection.Missing.Value
xlapp = New Excel.Application
xlworkbook = xlapp.Workbooks.Add(misvalue)
xlworksheet = xlworkbook.Sheets("sheet1")
cnnst = "datasource=localhost;port=3306;username=root;password=;database=myconnector"
cnn = New SqlConnection(cnnst)
cnn.Open()
sql = "select * from attendance"
Dim cmd As New SqlDataAdapter(sql, cnn)
Dim ds As New DataSet
cmd.Fill(ds)
For i = 0 To ds.Tables(0).Rows.Count - 1
For j = 0 To ds.Tables(0).Columns.Count - 1
xlworksheet.Cells(i + 1, j + 1) = ds.Tables(0).Rows(i).Item(j)
Next
Next
xlworksheet.SaveAs("D:\Report.xlsx")
xlworkbook.Close()
xlapp.Quit()
Myobject(xlapp)
Myobject(xlworkbook)
Myobject(xlworksheet)
cnn.Close()
MsgBox("success", "Exported", MessageBoxButtons.OK)
End Sub

CLSID {00020819-0000-0000-C000-000000000046} is the WorkBook Class.
Do not try to instantiate a Workbook by using the New keyword in the variable declaration statement.
Dim xlworkbook As New Excel.Workbook
should be
Dim xlworkbook As Excel.Workbook
The same also applies to the Worksheet variable declaration.

Related

Export Excel values to HTML using VB.Net

I have a Windows Forms with button click operation.
On button click the specified columns in excel should be exported to html.
I used "SaveAs" method. But not able to open the file format in any browser. Please help me how to export specific columns('B','D') to hmtl with cell color.
Dim xlApp As Excel.Application
Dim xlWB As Excel.WorkBook
Dim xlSH As Excel.WorkSheet
Dim str_File As String = "C:\Sample.xlsx"
xlApp = New Excel.Application
xlWB = xlApp.Workbooks.Open(str_File)
xlSH = xlWB.WorkSheets("merge")
xlSH.SaveAs("C:\Sample.html")
Just add the FileFormat paramater to the .SaveAs method and Bob's your uncle. I also closed and quit the Workbook and Application respectively. I added the GC code to the calling method to get rid of ghost Excel when debugging.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
OpCode()
GC.Collect()
GC.WaitForPendingFinalizers()
End Sub
Private Sub OpCode()
Dim str_File As String = "C:\Users\xxx\Documents\Excel\Politics\2020 Election.xlsx"
Dim xlApp As New Excel.Application
Dim xlWB As Excel.Workbook = xlApp.Workbooks.Open(str_File)
Dim xlSH As Excel.Worksheet = CType(xlWB.Worksheets(1), Excel.Worksheet)
xlSH.SaveAs("C:\Users\xxx\Documents\Excel\Sample.html", Excel.XlFileFormat.xlHtml)
xlWB.Close()
xlApp.Quit()
End Sub

Unable to pass Excel worksheet to a function in VBA Access

I am using VBA in an access application to create an Excel file. I am creating the excel application and worksheet just fine but when I try to pass that worksheet to another function, I get this error:
Run-time error '438':
Object doesn't support this property or method
Here is the code where I set up my excel application:
Public Sub setUpExcel()
Dim XLAPP As Excel.Application
Set XLAPP = New Excel.Application
XLAPP.Visible = True
Dim WKB As Excel.Workbook
Dim WKS As Excel.Worksheet, WKS2 As Excel.Worksheet
Set WKB = XLAPP.Workbooks.Open(excelFile)
Set WKS = WKB.ActiveSheet '.Worksheets(1)
WKS.Range("A1").Value = "Test Value"
WKS.Range("A1").Select
WKS.Range("A1").Value = ""
makeMasterPage (WKS) '<--WHERE I GET ERROR
End Sub
The call to makeMasterPage is where I get my error. Here is that function:
Private Sub makeMasterPage(ByRef WKS As Excel.Worksheet)
...
End Sub

VB - connect to local SQL server and load data to table from excel

I want to connect with VB code to my local SQL Server and load data into a table from my Excel file. This is what I got so far with my code that is incomplete. The field of the table (me_table) I created in the SQL database are z, ad, ag, retd, to, wg and in Excel sheet1 are column with data that reflects the field in the table. Thanks
Please advise
Imports System.Data
Imports System.Data.SqlClient
Module Module1
Dim myconnection As SqlConnection
Dim mycommand As SqlCommand
Dim dr As SqlDataReader
Dim dr1 As SqlDataReader
Dim ra As Integer
Sub Main()
Dim connectionString As String = "Server=DER7D;Database=testDB;User Id=DER7D\Der;Password="
myconnection = New SqlConnection("server=DER7D;uid=root;pwd=;database=simple")
'you need to provide password for sql server
myconnection.Open()
End Sub
End Module
Not taking good design into consideration, this is what you need. Just update the SqlConnection string. If you decide you want to use multiple sheets, just add them to the worksheets variable.
Private Sub SaveDataFromSpreadsheet()
Dim filePath = "directory\me_spreadsheet.xlsx"
Dim connectionString = String.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0}; Extended Properties=Excel 12.0;", filePath)
Dim worksheets As New List(Of String)() From {
"Sheet1"
}
For i As Integer = 0 To worksheets.Count - 1
Dim worksheetName As String = worksheets(i)
Dim adapter = New OleDbDataAdapter(String.Format("SELECT * FROM [{0}$]", worksheetName), connectionString)
Dim ds = New DataSet()
adapter.Fill(ds, "me_table")
Dim data As DataTable = ds.Tables("me_table")
For x As Integer = 0 To data.Rows.Count - 1
Dim z As String = If(String.IsNullOrEmpty(data.Rows(x).ItemArray(0).ToString()), "", data.Rows(x).ItemArray(0).ToString())
Dim ad As String = If(String.IsNullOrEmpty(data.Rows(x).ItemArray(1).ToString()), "", data.Rows(x).ItemArray(1).ToString())
Dim ag As String = If(String.IsNullOrEmpty(data.Rows(x).ItemArray(2).ToString()), "", data.Rows(x).ItemArray(2).ToString())
Dim retd As String = If(String.IsNullOrEmpty(data.Rows(x).ItemArray(3).ToString()), "", data.Rows(x).ItemArray(3).ToString())
Dim wg As String = If(String.IsNullOrEmpty(data.Rows(x).ItemArray(4).ToString()), "", data.Rows(x).ItemArray(4).ToString())
Using myconnection As New SqlConnection("Data Source=Your-Server;Initial Catalog=me_database;Integrated Security=True")
myconnection.Open()
Dim mycommand As New SqlCommand("INSERT INTO me_Table(z, ad, ag, retd, wg) VALUES(#z, #ad, #ag, #retd, #wg)", myconnection)
mycommand.Parameters.Add(New SqlParameter("#z", z))
mycommand.Parameters.Add(New SqlParameter("#ad", ad))
mycommand.Parameters.Add(New SqlParameter("#ag", ag))
mycommand.Parameters.Add(New SqlParameter("#retd", retd))
mycommand.Parameters.Add(New SqlParameter("#wg", wg))
mycommand.ExecuteNonQuery()
myconnection.Close()
End Using
Next
Next
End Sub

Error while using MysqlDataAdapter

I was following a tutorial on the internet ( link ) to create a a picture gallery. I got it all working, but i changed:
Private Sub CreateGallery()
i = 0
RemoveControls()
If Directorypath IsNot Nothing Then
Dim di As New IO.DirectoryInfo(Directorypath)
Dim diar1 As IO.FileInfo() = di.GetFiles("*.jpg").Concat(di.GetFiles("*.bmp")).Concat(di.GetFiles("*.png")).Concat(di.GetFiles("*.gif")).ToArray
Dim dra As IO.FileInfo
For Each dra In diar1
DrawPictureBox(dra.FullName, dra.Name)
Next
End If
End Sub
to:
Private Sub CreateGallery()
Dim table = New DataTable
Using Connection = New MySqlConnection("Server=localhost;User Id=root;Password=barra;Database=pap")
Using da = New MySqlDataAdapter("SELECT * FROM filme", Connection)
da.Fill(table)
End Using
End Using
i = 0
RemoveControls()
For Each row As DataRow In table.Rows
Try
Dim bytes() As Byte
bytes = (row("imagem"))
Dim memStream As New MemoryStream(bytes)
DrawPictureBox(memStream, row("titulo"))
Catch ex As Exception
End Try
Next
End Sub
It was working fine, the way i wanted, when i tried to do it on another project it gives me an error on 'MySqlDataAdapter':
Using da = New MySqlDataAdapter("SELECT * FROM filme", Connection)
the erros says : Overload resolution failed because no accessible 'New' can be called with these arguments
I tried almost everything i can't make it work.
convert your function as class and call :
dim new_creator as yourclass
...
Try This
Private Sub CreateGallery()
Dim table As New DataTable
Dim Conn As New SqlConnection
Conn = New SqlConnection("Initial Catalog=<DataBase>;User ID=sa;password=<password>;Data Source=<ServerName>"")
Dim com As String = ""
com = "SELECT * FROM Table "
Dim getComm As New SqlDataAdapter(com, Conn)
getComm.Fill(table)
i = 0
RemoveControls()
For Each row As DataRow In table.Rows
Dim bytes() As Byte
bytes = (row("Imagee"))
Dim memStream As New MemoryStream(bytes)
DrawPictureBox(memStream, row("Bro_Path"))
End Sub
and change ByVal _filename As MemoryStream,
Private Sub DrawPictureBox(ByVal _filename As MemoryStream, ByVal _displayname As String)

VBA conversion Office 2007 to Office 2010

I have a function in an Access 2007 database, which has been working fine until my PC was upgraded to Office 2010. The procedure is below and the offending line is that where 'originalFolder' is set:
Function ExportToSharePoint()
Dim oFs As New FileSystemObject
Dim originalFolder As Folder
Dim destinationPath As String
Dim ofile As file
Dim XLApp As Excel.Application
Dim xlwb As Excel.Workbook
Dim strFileName As String
Dim oFolder As String
oFolder = "//chs114file1/dovpasres/Public/Script/InfoCentre/Delays"
Set oFs = CreateObject("Scripting.FileSystemObject")
Set XLApp = New Excel.Application
Kill "K:\Public\Script\InfoCentre\Delays\*.xlk"
Set originalFolder = oFs.GetFolder(oFolder)
destinationPath = "https://companyname.sharepoint.com/PRR/Documents/"
For Each ofile In originalFolder.Files
strFileName = oFs.GetFileName(ofile)
Set xlwb = XLApp.Workbooks.Open(ofile)
xlwb.SaveAs (destinationPath + strFileName)
Next
xlwb.Close True
XLApp.Quit
Set xlwb = Nothing
Set XLApp = Nothing
End Function
The error I'm getting is:
Error 13: data type mismatch
I'm mystified as this is a string, as required?
The Microsoft documentation lists the GetFolder return type as being Folder, but I suspect your problem is being caused by mixing late binding (via CreateObject) with early binding (via the strongly-typed Folder variable).
Either import the reference to avoid the CreateObject call, or change the type to a variant, or perhaps object:
Dim originalFolder As variant