Excel Connection String - asp.net

I am trying to read Excel, the code I am using runs in one project and is giving error "External table is not in the expected format." for same Excel Input in another project. What may be the issue for code to fail?- My connection string is " strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties=\"Excel 8.0;HDR=" + HDR + ";IMEX=0\"";"

You need to put single quotes around the Extended properties value.
from here
1. conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.15.0;Data Source=C:\Users\Input.xlsx;Extended Properties='Excel 15.0 Xml'");
2. conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.14.0;Data Source="+FilePath+";Extended Properties='Excel 14.0 Xml'");
3. conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Input.xlsx;Extended Properties='Excel 12.0 Xml'");

Related

Error in Export data from excel to sql server database

I have try to copy data from excel sheet to sql server using sql bulk copy, but
I got error whenever try to open connection of excel sheet database I mean Microsoft ace oledb connection.
I have try "Enable 32-Bit Application = true" in Application Pool then It works fine, but I don't want to set it true.
How I can fix it??
I have share sample code in this excel_con.Open() comes error this point
'Upload and save the file
Dim excelPath As String = Server.MapPath("~/Files/") + Path.GetFileName(FileUpload1.PostedFile.FileName)
FileUpload1.SaveAs(excelPath)
Dim connString As String = String.Empty
Dim extension As String = Path.GetExtension(FileUpload1.PostedFile.FileName)
Select Case extension
Case ".xls"
'Excel 97-03
connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & excelPath & ";Extended Properties=""Excel 8.0;HDR=YES;IMEX=1"""
Exit Select
Case ".xlsx"
'Excel 07 or higher
connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & excelPath & ";Extended Properties=""Excel 12.0 Xml;HDR=YES;IMEX=1"""
Exit Select
End Select
Using excel_con As New OleDbConnection(connString)
excel_con.Open()
Dim sheet1 As String = excel_con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing).Rows(0)("TABLE_NAME").ToString()
Dim dtExcelData As New DataTable()
This is the error screen shot

VB.net reading Excel page

I am trying to read an Excel page but I get the following error:
Input string was not in a correct format.Couldn't store <555-555-5555> in PHONE_NUM Column. Expected type is Double.
It seems that the oledb command is expecting the column to be of type double (but, in fact, it is a string for phone numbers). I think I read somewhere that this happens because the command looks as the first couple of items in the column and determines what the type should be based off of that. Is there anyway to always read all values in as a string?
This is the code I am using:
Dim connection As OleDb.OleDbConnection
Dim command As OleDb.OleDbCommand
Dim adapter As New OleDb.OleDbDataAdapter
Dim connString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + myFile + ";Extended Properties=Excel 12.0;"
Dim pgOne As String = "Page1$"
Dim pgTwo As String = "Page2$"
Dim selectOne As String = "SELECT * FROM [" & pgOne & "] WHERE COL1<>''"
Dim selectTwo As String = "SELECT * FROM [" & pgTwo & "]"
connection = New OleDb.OleDbConnection(connString)
Try
If connection.State = ConnectionState.Open Then
Else
connection.Open()
End If
command = New OleDb.OleDbCommand(selectOne, connection)
adapter.SelectCommand = command
adapter.Fill(ds, "Table1")
adapter.SelectCommand.CommandText = selectTwo
adapter.Fill(ds, "Table2")
Catch ex As OleDb.OleDbException
Finally
adapter.Dispose()
If (Not command Is Nothing) Then
command.Dispose()
End If
connection.Close()
End Try
You can get the tables to fill as strings in one of two ways. If you want to read them in as only a string you can add a part to your connection string that should read intermixed datatype columns as text. Alternatively force them into string types by reading in a header row as data. You can also use schemas to read in table definitions but I would need to find a little more info on that if these don't work for you.
The first option is simpler to do:
First change your connection string to add IMEX=1
'Add IMEX=1
Dim connString As String = "Provider=Microsoft.ACE.OLEDB.12.0;IMEX=1;Data Source=" + myFile + ";Extended Properties=Excel 12.0;"
This should then provide you with String based columns on any datacolumns that have mixed data types in their rows.
Another way to do it is force the string column type by reading the header row as a datarow by specifying there is no header row in your connection string. You can then remove this after to have a string column of data.
'Add HDR=NO
Dim connString As String = "Provider=Microsoft.ACE.OLEDB.12.0;HDR=NO;Data Source=" + myFile + ";Extended Properties=Excel 12.0;"
I will research a little more if these don't work for you.
As mentioned, I was unable to fix this so I just ended up creating a schema manually and formatting the data as it was bound.

The Microsoft Jet database engine could not find the object 'Sheet1$' in vb.net

here am trying to keep excel sheet data in to data set.help me to over from this error
The Microsoft Jet database engine could not find the object 'Sheet1$'. Make sure the object exists and that you spell its name and the path name correctly.
Dim ds As System.Data.DataSet
Dim RecTab As Data.DataTable
Dim RecTab1 As Data.DataTable
Dim Rectab2 As Data.DataTable
Dim ds1 As System.Data.DataSet
Dim HFCell As String
Dim HTCell As String
Dim FilePath As String
HFCell = "A1"
HTCell = "B1"
m_FileName = "Data.xls"
FilePath = Server.MapPath("..\TankGauge\Data_Mgr") & "\" & m_FileName
Try
Dim connectionString As String = ""
Try
connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + FilePath + ";" + "Extended Properties=Excel 8.0;"
MyConnection = New OleDbConnection(connectionString)
MyConnection.Open()
dataAdapter = New OleDbDataAdapter("SELECT * FROM [Sheet1$]", MyConnection)
ds = New Data.DataSet
dataAdapter.Fill(ds)
RecTab = ds.Tables(0)
MyConnection.Close()
In your Excel workbook, is there a worksheet named Sheet1? If not, rename the sheet or change your code to call out the sheet you want to get data from.
I believe the database driver you're using won't work. Try changing:
connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + FilePath + ";" + "Extended Properties=Excel 8.0;"
to:
connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + FilePath + ";" + "Extended Properties=Excel 8.0;"

Update Excel Sheet (in Classic ASP/Vbscript)

I tried to search for a code that would update a Excel (XLS) file in Classic-ASP but I cannot get it to work.
here is what I have:
<!--#include file="../adovbs.inc"-->
<%
' Open and Update and then Close The XLS File
Dim objConn
set objConn = Server.CreateObject("ADODB.Connection")
Dim FLConnect
Dim strSQLexcel
' Create the connection string.
FLConnect = "Provider=Microsoft.Jet.OLEDB.4.0 Data Source=" & Server.MapPath("TEST.xls") & "Extended Properties='Excel 8.0;HDR=No'"
' Create the SQL statement.
strSQLexcel= "UPDATE [Sheet1$A1:A1] SET F1='TestValue1'"
set objConn = Server.CreateObject("ADODB.Recordset")
'Set objConn = New ADODB.Connection
' Create and open the Connection object.
objConn.Open FLConnect
' Execute the insert statement.
objConn.Execute strSQLexcel
' Close and destroy the Connection object.
objConn.Close
%>
But I keep getting an error saying: " The connection cannot be used to perform this operation. It is either closed or invalid in this context. "
Thank you so much...
Your connection string is not right.
You have:
Provider=Microsoft.Jet.OLEDB.4.0 Data Source="
& Server.MapPath("TEST.xls") & "Extended Properties='Excel 8.0;HDR=No'"
You are missing a semi-colon after 4.0 and before Extended
Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
& Server.MapPath("TEST.xls") & ";Extended Properties='Excel 8.0;HDR=No'"
See http://connectionstrings.com
This connection string worked best for me:-
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\myExcel.xlsm;Extended Properties='Excel 12.0 Macro;HDR=YES';

Cannot delete excel file after importing data from it using OleDB, says it is being used by another process

I'm importing data from an Excel file in ASP.NET using OleDB. After finishing the import, I want to delete the file using the command System.IO.File.Delete(), but it throws the following exception:
The process cannot access the file '...29.xls' because it is being used by another process.
I used the following code to open and close the file:
Dim fajl As String
fajl = MapPath("fajlovi/" + Request.QueryString("ID"))
Dim sConnectionStringExcel As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & fajl & ";Extended Properties=Excel 8.0;"
Dim objConnExcel As New OleDbConnection(sConnectionStringExcel)
objConnExcel.Open()
Dim objCmdSelectExcel As New OleDbCommand("SELECT ZavedenKodPov, Ime, Mjesto, Adresa, JMBG, LicniBroj, ZaposlenKod, Nepoznat, Umro, Penzioner, Reon, VoziloProizvodjac, VoziloModel, VoziloRegistracija, Nekretnina, Datum, KontoBroj, NazivKonta, OpisPromjene, Dug, Pot FROM [Sheet1$]", objConnExcel)
Dim objAdapterExcel As New OleDbDataAdapter()
objAdapterExcel.SelectCommand = objCmdSelectExcel
Dim objDatasetExcel As New DataSet()
objAdapterExcel.Fill(objDatasetExcel, "XLData")
Dim tExcel As DataTable
tExcel = objDatasetExcel.Tables(0)
'.
'.
'.
objConnExcel.Close()
System.IO.File.Delete(fajl)
Any ideas what I'm doing wrong?
dispose of the command and connection, preferably wrapped in a using statement. then you should be able to delete the file.

Resources