VB.NET使用OLDB连接将数据写入Excel [英] VB.NET writing data to Excel using OLDB Connection

查看:128
本文介绍了VB.NET使用OLDB连接将数据写入Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序可以在excel文件中输出报告.我在添加行时使用OLEDB连接.

I have a program that output a report in excel file. I use OLEDB Connection in adding rows.

Try
    Dim MyConnection As System.Data.OleDb.OleDbConnection
    Dim myCommand As New System.Data.OleDb.OleDbCommand
    Dim sql As String

    MyConnection = New System.Data.OleDb.OleDbConnection _
    ("provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + _
    "'c:\test.xls';Extended Properties=Excel 8.0;")

    MyConnection.Open()
    myCommand.Connection = MyConnection
    sql = "Insert into [Sheet1$] (id,name) values('5','e')"
    myCommand.CommandText = sql
    myCommand.ExecuteNonQuery()
    MyConnection.Close()
Catch ex As Exception
    MsgBox(ex.ToString)
End Try
MsgBox("Row Added ")



http://www.java-samples.com/showtutorial.php?tutorialid=1051 [ ^ ]

我的问题是在该Excel文件中添加行之前,我必须具有标题作为字段名称.
''这是EXCEL文件中的内容



Found this in http://www.java-samples.com/showtutorial.php?tutorialid=1051[^]

My problem is before I add row in that excel file I must have a header as field names.
''THIS IS THE CONTENT IN EXCEL FILE

id | name



如何在没有该标题的excel文件中添加行.

谢谢...



How can I add row in excel file without that header.

Thanks...

推荐答案

(id,name)values('5','e')" myCommand.CommandText = sql myCommand.ExecuteNonQuery() MyConnection.Close() 捕获,例如 As 异常 MsgBox(例如ToString) 结束 尝试 MsgBox(" )
(id,name) values('5','e')" myCommand.CommandText = sql myCommand.ExecuteNonQuery() MyConnection.Close() Catch ex As Exception MsgBox(ex.ToString) End Try MsgBox("Row Added ")



http://www.java-samples.com/showtutorial.php?tutorialid=1051 [ ^ ]

我的问题是在该Excel文件中添加行之前,我必须具有标题作为字段名称.
''这是EXCEL文件中的内容



Found this in http://www.java-samples.com/showtutorial.php?tutorialid=1051[^]

My problem is before I add row in that excel file I must have a header as field names.
''THIS IS THE CONTENT IN EXCEL FILE

id | name



如何在没有该标题的excel文件中添加行.

谢谢...



How can I add row in excel file without that header.

Thanks...


问题出在您的连接字符串上.转到 ConnectionStrings.com [ ^ ],您会发现很多,包括带有标题行的支撑页上的示例.
THe problem is with your connection string. Go to ConnectionStrings.com[^] and you''ll find a ton of them, including samples on supporting sheets with header rows.


这可能会有所帮助:
This could be helpful: inserting data in excel[^]


这篇关于VB.NET使用OLDB连接将数据写入Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆