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';
Related
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
Does anyone know how I can display the content of an Excel 2016 file in a webpage?
I cannot figure out how to open a connection from the webpage nor which drivers I might need to install in order to open the ODBC or DSN connection.
Can anyone help me?
I got it working after using a couple of hours on it.
Heres how I read the data out
<%
Dim Conn, Sql, rs
Set Conn = Server.createobject("ADODB.Connection")
Conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\inetpub\wwwroot\test\test.xlsx;Extended Properties=""Excel 12.0 Xml;HDR=YES;IMEX=1"";"
' Ark1 is danish for Sheet1
Sql = "select * from [Ark1$]"
set rs = Conn.Execute(Sql)
do while not rs.eof or rs.bof
response.write rs("test") & "<br>" & vbCrLf
rs.movenext
loop
Conn.CLose
Set Conn = nothing
%>
Hope this might help others.
im trying to insert a new row with new data to an ms access table, using asp page.
i have no background in asp, im an android developer, but those are my client specifications.
i know im doing something very wrong, but i dont know what...
can you please help me?
this is what i was trying to do:
<%
'define variables
dim conn, strsql, strMDBPath
Set conn = Server.CreateObject("ADODB.Connection")
'Connect to the database
strMDBpath = Server.MapPath("data.mdb")
conn.open "PROVIDER=MICROSOFT.JET.OLEDB.4.0;DATA SOURCE=" & strMDBPath
'On Error Resume Next
'write to the database
strSql = "INSERT INTO avi (id,first,last) VALUES ("4", '" yom "','" cobi "')"
conn.Execute(strSql)
'close database
conn.close
Set conn = nothing
%>
You're missing some ampersands.
strSql = "INSERT INTO avi (id,first,last) VALUES (4, '" & yom & "', '" & cobi & "')"
I'd to know if it's possible to have my asp classic form saved into a excel file in a column after submit?
Thank you all.
use the Microsoft.Jet.OLEDB Driver to access the excel sheet like so:
dim conn : set conn = server.createObject("ADODB.Connection")
dim rs : set rs = server.createObject("adodb.recordset")
dim sql
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" &_
"myExcelFile.xls;" &_
"Extended Properties=""Excel 8.0;HDR=YES;"""
then you yould use just sql to insert your data...
the possible connectionstrings for excel are listed here
THE BELOW CODE IS TO INSERT INTO AN EXISTING EXCEL FILE. THIS IS WHAT YOU NEED.
<%
Option Explicit
' OPEN DATABASE
dim objConn,strConnection,objRS,strQuery
'Set objConn = New ADODB.Connection
set objConn = Server.CreateObject("ADODB.Connection")
objConn.ConnectionString = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("TEST.xls") & "; Extended Properties=Excel 8.0;"
objConn.Open strConnection
'Set objRS = New ADODB.Recordset
set objRS = Server.CreateObject("ADODB.Recordset")
set objRS.ActiveConnection = objConn
' This is to Select A1:A1 and open the recordset
strQuery = "SELECT * FROM A1:A1"
objRS.Open strQuery
' This is to insert into A1:A1 a value that says: testttest
strQuery = "insert into [A1:A1] values('testttest')"
' Close and destroy the Connection object.
objConn.Execute strQuery
objConn.Close
Set objRS=Nothing
Set objConn=Nothing
%>
TO UPDATE A SPECIFIC COLUMN
You can do this: See here: http://bytes.com/topic/asp-classic/answers/620074-update-existing-excel-file-using-asp-urgent
and also here: Update Excel Sheet (in Classic ASP/Vbscript)
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.