使用 ASP / VBScript 写入 Excel 文件

发布于 2024-12-09 02:40:32 字数 1243 浏览 2 评论 0原文

我正在尝试使用 ASP / VBScript(不是 VB.NET)从 HTML 表单中获取信息并输入到 Excel 文件(xlsx)中。我在 Java 和 PHP 方面有一些经验,但对 VB 世界还是个新手。 到目前为止,我已经找到了从 GET/POST 方法获取数据的方法。现在我正在尝试创建与 Excel 文件的 ADO 连接。 到目前为止,这是我的代码:

Dim cn as ADODB.Connection
Set cn = New ADODB.Connection
With cn
    .Provider = "Microsoft.ACE.OLEDB.12.0"
    .ConnectionString = "Data Source=EXCEL_FILE.xlsx;" & _"Extended Properties=Excel 12.0 Xml;HDR=YES"
    .Open
End With

我从这里获取了连接字符串:connectionstrings.com 并尝试坚持本指南:http://support.microsoft.com/kb/257819 /en-us 但到目前为止还没有运气。 所以我的问题是:

1)这总体上是正确的想法吗?例如,从 POST 获取数据,然后使用 ADO 打开与 excel 文件的连接,并在连接对象上添加带有查询的信息?

2)代码中有什么明显的缺陷吗?

3) 如果有人能概述一个解决方案,将 HTML 表单中的数据写入 Excel 文件,那就太好了。

提前致谢。

编辑: 好的,这是我尝试的代码:

Dim cn
Set cn = Server.CreateObject("ADODB.Connection")
With cn
    .Provider = "Microsoft.ACE.OLEDB.12.0"
    .ConnectionString = "Data Source=EXCEL_FILE.xlsx;Extended Properties=""Excel 12.0 Xml;HDR=YES"""
'From : http://www.connectionstrings.com/excel-2007
    .Open
End With

一旦我在 cn 上调用“Open”,它就会给我一个 500 内部错误。我不确定我是否犯了一个明显的错误,但由于我不知道在哪里可以找到错误日志,所以我不知道从哪里开始查找。

I am trying to take information from a HTML Form and input into an Excel File (xlsx) with ASP / VBScript (not VB.NET). I have some experience in Java and PHP but am new to the VB world.
Sofar I have found ways to get the Data from the GET/POST methods. Now I am trying to create an ADO connection to the excel file.
here is my code so far:

Dim cn as ADODB.Connection
Set cn = New ADODB.Connection
With cn
    .Provider = "Microsoft.ACE.OLEDB.12.0"
    .ConnectionString = "Data Source=EXCEL_FILE.xlsx;" & _"Extended Properties=Excel 12.0 Xml;HDR=YES"
    .Open
End With

I got the connection String from here: connectionstrings.com
and tried to stick to this guide: http://support.microsoft.com/kb/257819/en-us
But no luck up until now.
So here are my questions:

1) Is this the right idea in general? So grabbing the Data from POST for example and then opening a connection with ADO to the excel file and adding the info with queries on the connection object?

2) Any obvious flaws in the code ?

3) Would be great if someone could outline a solution, writing data from a HTML Form into an Excel file.

Thanks in advance.

Edit:
Ok Here is the code I try:

Dim cn
Set cn = Server.CreateObject("ADODB.Connection")
With cn
    .Provider = "Microsoft.ACE.OLEDB.12.0"
    .ConnectionString = "Data Source=EXCEL_FILE.xlsx;Extended Properties=""Excel 12.0 Xml;HDR=YES"""
'From : http://www.connectionstrings.com/excel-2007
    .Open
End With

Once I call "Open" on cn it gives me a 500 internal Error. I am not sure if I am making an obvious mistake, but since I don't know where to find error logs I don't have a clue where to start looking.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你げ笑在眉眼 2024-12-16 02:40:32

1 - 如果您必须使用Excel作为数据库,是的,这是正确的。但是,如果您需要数据库,应该使用数据库,而不是 Excel 工作表。
2 - VBScript 不支持早期绑定。您应该定义没有数据类型的变量,应该使用 创建对象。您需要对连接字符串(引号)进行一些更改。
eg

Dim cn
Set cn = Server.CreateObject("ADODB.Connection")
With cn
    .Provider = "Microsoft.ACE.OLEDB.12.0"
    .ConnectionString = "Data Source=EXCEL_FILE.xlsx;Extended Properties=""Excel 12.0 Xml;HDR=YES"""
    'From : http://www.connectionstrings.com/excel-2007
.Open
End With
'
' Add new records etc.
'
cn.Close
Set cn = Nothing

3 - 添加新记录的示例(将其替换为上面的“添加新记录等”)从 HTML 表单获取值(帖子方法)。

Dim rs
Set rs = Server.CreateObject("Adodb.Recordset")
With rs
    .Open "[Sheet1$]", cn, 1, 3
    .AddNew 
    .Fields(0).Value = Request.Form("Param1") 'Column A1 (or with name rs.Fields("col1").Value = exp )
    .Fields(1).Value = Request.Form("Param2") 'Column B1
    .Update
    .Close
End With
Set rs = Nothing

1 - If you have to use Excel as database, yes it's right. But, if you need a database, should use a database, not an excel sheet.
2 - VBScript doesn't support early binding. You should define variables without data type, should create objects using CreateObject. And you need to some changes in connection string (quotes).
e.g.

Dim cn
Set cn = Server.CreateObject("ADODB.Connection")
With cn
    .Provider = "Microsoft.ACE.OLEDB.12.0"
    .ConnectionString = "Data Source=EXCEL_FILE.xlsx;Extended Properties=""Excel 12.0 Xml;HDR=YES"""
    'From : http://www.connectionstrings.com/excel-2007
.Open
End With
'
' Add new records etc.
'
cn.Close
Set cn = Nothing

3 - An example to add new record (put the instead of above 'Add new records etc.) gets the values from HTML Form (post method).

Dim rs
Set rs = Server.CreateObject("Adodb.Recordset")
With rs
    .Open "[Sheet1$]", cn, 1, 3
    .AddNew 
    .Fields(0).Value = Request.Form("Param1") 'Column A1 (or with name rs.Fields("col1").Value = exp )
    .Fields(1).Value = Request.Form("Param2") 'Column B1
    .Update
    .Close
End With
Set rs = Nothing
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文