SQL查询到Excel文件 [英] SQL query to Excel file

查看:71
本文介绍了SQL查询到Excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将通过我的VB.net GUI

构建的SQL查询输出到excel文件中。


这是我正在使用的代码:


''Sqlstmt是SQL查询语句

''Conn是SQL连接对象

cmd =新的SqlCommand(Sqlstmt,Conn)

datareader = cmd.ExecuteReader()

''datareader是SQLdatareader对象


Dim objexcel As Excel.Application

Dim objwkb As Workbook

Dim objwksht As Workheet


objexcel =新的Excel.Application

objwkb = objexcel.Workbooks.Add

objwksht = objwkb.ActiveSheet()


Dim cols = datareader .FieldCount()

objwksht.Range(objwksht.Cells(1,1),objwksht.Cells(5,

cols))。CopyFromRecordset(datareader)


上面的行导致异常,即我从未在Excel工作表中收到任何数据

。我把它放在一个try语句来捕获

异常就是这样:


System.InvalidCastException:没有这样的接口支持

at System.RuntimeType.ForwardCallToInvokeMember(Strin g memberName,

BindingFlags flags,Object target,Int32 [] aWrapperTypes,MessageData&

msgData)
$ b Excel.Range.CopyFromRecordset上的$ b(对象数据,对象MaxRows,对象

MaxColumns)
WindowsApplication1.Form1.btGenReport_Click上的
(对象发送者,

EventArgs e)在C:..... \Form1.vb:第416行
我是.NET的完全新手(Unix C / C ++编程背景)。


任何人都可以建议我做错了什么?我有点学习了来自其他人的

代码,所以Range命令对我来说有点粗略......


还有什么其他的替代方法可以用来; datareader"

数据并将其放入excel文件中(我正在尝试根据SQL表中的数据创建基于报表的
)?


谢谢!

I''m trying to output a SQL query that is constructed thru my VB.net GUI
into an excel file.

Here is the code I''m using:

''Sqlstmt is the SQL query statement
''Conn is the SQL Connection object

cmd = New SqlCommand(Sqlstmt, Conn)
datareader = cmd.ExecuteReader()
''datareader is the SQLdatareader object

Dim objexcel As Excel.Application
Dim objwkb As Workbook
Dim objwksht As Worksheet

objexcel = New Excel.Application
objwkb = objexcel.Workbooks.Add
objwksht = objwkb.ActiveSheet()

Dim cols = datareader.FieldCount()
objwksht.Range(objwksht.Cells(1, 1), objwksht.Cells(5,
cols)).CopyFromRecordset(datareader)

The line above is causing an exception, i.e., I never receive any data
in the excel sheet. I put this in a try statement to catch the
exception and it is this:

System.InvalidCastException: No such interface supported
at System.RuntimeType.ForwardCallToInvokeMember(Strin g memberName,
BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData&
msgData)
at Excel.Range.CopyFromRecordset(Object Data, Object MaxRows, Object
MaxColumns)
at WindowsApplication1.Form1.btGenReport_Click(Object sender,
EventArgs e) in C:.....\Form1.vb:line 416
I''m a complete newbie at .NET (Unix C/C++ programming background).

Can anyone advise about what I''m doing wrong? I''ve sort of "learnt" the
code from others, so the Range command is kinda sketchy to me.....

What other alternative ways would there be to take the "datareader"
data and put it in an excel file (I''m trying to create a report based
on the data from a SQL table)?

Thanks!

推荐答案

你知道你做错了吗?

excel是一个电子邮件和一个疾病


将您的数据保存在数据库中

Access拥有比任何数据库更好的数据网格这个.NET垃圾。


使用Access。卸载Excel和.NET。


如果您使用的是SQL Server,则优先访问数据项目。

如果您不使用SQL Server然后启动


-Aaron


wreckingcru写道:
do you know what you''re doing wrong?

excel is a SPREADSHEET and a DISEASE

keep your data in a database
Access has MUCH better datagrids than any of this .NET crap.

Use Access. Uninstall Excel and .NET.

Preferable Access Data Projects if you''re using SQL Server.
if you''re not using SQL Server then start

-Aaron


wreckingcru wrote:

I ''我试图通过我的VB.net GUI

将一个SQL查询输出到一个excel文件中。


这是代码我''使用:


''Sqlstmt是SQL查询语句

''Conn是SQL连接对象


cmd =新的SqlCommand(Sqlstmt,Conn)

datareader = cmd.ExecuteReader()

''datareader是SQLdatareader对象


Dim objexcel As Excel.Application

Dim objwkb As Workbook

Dim objwksht As Workheet


objexcel = New Excel.Application

objwkb = objexcel.Workbooks.Add

objwksht = objwkb.ActiveSheet()


Dim cols = datare ader.FieldCount()


objwksht.Range(objwksht.Cells(1,1),objwksht.Cells(5,

cols))。CopyFromRecordset( datareader)


上面的行导致异常,即我从未在Excel工作表中收到任何数据

。我把它放在一个try语句来捕获

异常就是这样:


System.InvalidCastException:没有这样的接口支持

at System.RuntimeType.ForwardCallToInvokeMember(Strin g memberName,

BindingFlags flags,Object target,Int32 [] aWrapperTypes,MessageData&

msgData)
$ b Excel.Range.CopyFromRecordset上的$ b(对象数据,对象MaxRows,对象

MaxColumns)
WindowsApplication1.Form1.btGenReport_Click上的
(对象发送者,

EventArgs e)在C:..... \Form1.vb:第416行


我是.NET的完全新手(Unix C / C ++编程背景) 。


任何人都可以建议我做错了什么?我有点学习了来自其他人的

代码,所以Range命令对我来说有点粗略......


还有什么其他的替代方法可以用来; datareader"

数据并将其放入excel文件中(我正在尝试根据SQL表中的数据创建基于报表的
)?


谢谢!
I''m trying to output a SQL query that is constructed thru my VB.net GUI
into an excel file.

Here is the code I''m using:

''Sqlstmt is the SQL query statement
''Conn is the SQL Connection object

cmd = New SqlCommand(Sqlstmt, Conn)
datareader = cmd.ExecuteReader()
''datareader is the SQLdatareader object

Dim objexcel As Excel.Application
Dim objwkb As Workbook
Dim objwksht As Worksheet

objexcel = New Excel.Application
objwkb = objexcel.Workbooks.Add
objwksht = objwkb.ActiveSheet()

Dim cols = datareader.FieldCount()
objwksht.Range(objwksht.Cells(1, 1), objwksht.Cells(5,
cols)).CopyFromRecordset(datareader)

The line above is causing an exception, i.e., I never receive any data
in the excel sheet. I put this in a try statement to catch the
exception and it is this:

System.InvalidCastException: No such interface supported
at System.RuntimeType.ForwardCallToInvokeMember(Strin g memberName,
BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData&
msgData)
at Excel.Range.CopyFromRecordset(Object Data, Object MaxRows, Object
MaxColumns)
at WindowsApplication1.Form1.btGenReport_Click(Object sender,
EventArgs e) in C:.....\Form1.vb:line 416
I''m a complete newbie at .NET (Unix C/C++ programming background).

Can anyone advise about what I''m doing wrong? I''ve sort of "learnt" the
code from others, so the Range command is kinda sketchy to me.....

What other alternative ways would there be to take the "datareader"
data and put it in an excel file (I''m trying to create a report based
on the data from a SQL table)?

Thanks!


这真的是一个意见而不是解决方案。

我认为Excel很棒。

无论如何,这不是我自行决定的。我必须使用SQL数据库

(远程) - 这就是传给我的东西。我有连接

字符串和UID / Pass连接到它并根据GUI输入,我是

构建查询。


我想调试我的问题 - 没有听说

微软有什么问题。
aa ********* @ gmail.com 写道:
That''s really an opinion and not a solution.
I think Excel is awesome.
Anyway, this is not my discretion. I HAVE to work with a SQL database
(remote) - that''s what been handed down to me. I have the connection
string and UID/Pass to connect to it and based on the GUI input, I''m
constructing the query.

I''d like to debug my problem - not hear about what''s wrong with
Microsoft.
aa*********@gmail.com wrote:

你呢知道你做错了什么吗?


excel是一个电子书和一个疾病


将你的数据保存在一个数据库中

Access比任何.NET垃圾都有更好的数据网格。


使用Access。卸载Excel和.NET。


如果您使用的是SQL Server,则优先访问数据项目。

如果您不使用SQL Server然后启动


-Aaron


wreckingcru写道:
do you know what you''re doing wrong?

excel is a SPREADSHEET and a DISEASE

keep your data in a database
Access has MUCH better datagrids than any of this .NET crap.

Use Access. Uninstall Excel and .NET.

Preferable Access Data Projects if you''re using SQL Server.
if you''re not using SQL Server then start

-Aaron


wreckingcru wrote:

I ''我试图通过我的VB.net GUI

将一个SQL查询输出到一个excel文件中。


这是代码我''使用:


''Sqlstmt是SQL查询语句

''Conn是SQL连接对象


cmd =新的SqlCommand(Sqlstmt,Conn)

datareader = cmd.ExecuteReader()

''datareader是SQLdatareader对象


Dim objexcel As Excel.Application

Dim objwkb As Workbook

Dim objwksht As Workheet


objexcel = New Excel.Application

objwkb = objexcel.Workbooks.Add

objwksht = objwkb.ActiveSheet()


Dim cols = datareader.FieldCount()

objwksht.Range(objwksht.Cells(1,1),objwksht.Cells(5,

cols))。CopyFromRecordset(datareader) )


上面的行导致异常,即我从未在Excel工作表中收到任何数据

。我把它放在一个try语句来捕获

异常就是这样:


System.InvalidCastException:没有这样的接口支持

at System.RuntimeType.ForwardCallToInvokeMember(Strin g memberName,

BindingFlags flags,Object target,Int32 [] aWrapperTypes,MessageData&

msgData)
$ b Excel.Range.CopyFromRecordset上的$ b(对象数据,对象MaxRows,对象

MaxColumns)
WindowsApplication1.Form1.btGenReport_Click上的
(对象发送者,

EventArgs e)在C:..... \Form1.vb:第416行
我是.NET的完全新手(Unix C / C ++编程背景)。


任何人都可以建议我做错了什么?我有点学习了来自其他人的

代码,所以Range命令对我来说有点粗略......


还有什么其他的替代方法可以用来; datareader"

数据并将其放入excel文件中(我正在尝试根据SQL表中的数据创建基于报表的
)?


谢谢!
I''m trying to output a SQL query that is constructed thru my VB.net GUI
into an excel file.

Here is the code I''m using:

''Sqlstmt is the SQL query statement
''Conn is the SQL Connection object

cmd = New SqlCommand(Sqlstmt, Conn)
datareader = cmd.ExecuteReader()
''datareader is the SQLdatareader object

Dim objexcel As Excel.Application
Dim objwkb As Workbook
Dim objwksht As Worksheet

objexcel = New Excel.Application
objwkb = objexcel.Workbooks.Add
objwksht = objwkb.ActiveSheet()

Dim cols = datareader.FieldCount()
objwksht.Range(objwksht.Cells(1, 1), objwksht.Cells(5,
cols)).CopyFromRecordset(datareader)

The line above is causing an exception, i.e., I never receive any data
in the excel sheet. I put this in a try statement to catch the
exception and it is this:

System.InvalidCastException: No such interface supported
at System.RuntimeType.ForwardCallToInvokeMember(Strin g memberName,
BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData&
msgData)
at Excel.Range.CopyFromRecordset(Object Data, Object MaxRows, Object
MaxColumns)
at WindowsApplication1.Form1.btGenReport_Click(Object sender,
EventArgs e) in C:.....\Form1.vb:line 416
I''m a complete newbie at .NET (Unix C/C++ programming background).

Can anyone advise about what I''m doing wrong? I''ve sort of "learnt" the
code from others, so the Range command is kinda sketchy to me.....

What other alternative ways would there be to take the "datareader"
data and put it in an excel file (I''m trying to create a report based
on the data from a SQL table)?

Thanks!


2006年8月16日12:12:15 -0700,wreckingcru < pr ********* @ gmail.comwrote:

$ b $b¤我正在尝试输出通过我的VB.net GUI构建的SQL查询
$ b $b¤成为一个excel文件。
$ b $b¤
$ b $b¤这是我正在使用的代码:
$ b $b¤
$ b $b¤'''sqlstmt是SQL查询语句
$ b $b¤''Conn是SQL连接对象
$ b $b¤

¤cmd=新的SqlCommand(Sqlstmt,Conn)
$ b $b¤datareader= cmd.ExecuteReader()
$ b $b¤''datareader是SQLdatareader对象

¤
$ b $b¤Dimobjexcel As Excel.Application
$ b $b¤Dimobjwkb As Workbook
$ b $b¤Dimobjwksht As Workheet

¤
$ b $b¤objexcel=新的Excel.Application
$ b $b¤objwkb= objexcel.Workbooks.Add
$ b $b¤objwksht= objwkb.ActiveSheet()< br $> $ b $b¤
$ b $b¤Dimcols = datareader.FieldCount()
$ b $b¤
$ b $b¤

¤objwksht.Range(objwksht.Cells(1,1),objwksht.Cells(5,
$ b $b¤cols))。CopyFr omRecordset(datareader)
$ b $b¤
$ b $b¤上面的行导致异常,即我从未收到任何数据
$ b $b¤在excel表中。我把它放在一个try语句中来捕获
$ b $b¤异常就是这样:
$ b $b¤
$ b $b¤System.InvalidCastException:没有这样的接口支持
$ b $b¤在System.RuntimeType.ForwardCallToInvokeMember(Strin g memberName,
$ b $b¤BindingFlags标志,Object target,Int32 [] aWrapperTypes,MessageData&

¤msgData)
$ b $b¤在Excel.Range.CopyFromRecordset(对象数据,对象MaxRows,对象
$ b $b¤MaxColumns)
$ b $b¤在WindowsApplication1上。 Form1.btGenReport_Click(对象发件人,
$ b $b¤EventArgse)在C:..... \ .Form1.vb:第416行
$ b $b¤


CopyFromRecordset仅支持ADO和DAO Recordset对象。它不支持ADO.NET。


您可以尝试使用直接SQL执行此操作:


将ExcelConnection变为新的

System.Data.OleDb.OleDbConnection(" Provider = Micros oft.Jet.OLEDB.4.0;& _

" Data Source ="&" c: \测试

文件\ ExcelElsWls.xls"&" ;;"& _

"扩展属性= Excel

8.0;")


ExcelConnection.Open()


Dim ExcelCommand As New System.Data.OleDb.OleDbCommand(" SELECT * INTO [Orders] FROM [Orders]

IN''''[ODBC; Driver = {SQL Server}; Server =(local); Database = Northwind; Trusted_ Connection = yes];",

ExcelConnection)


ExcelCommand.ExecuteNonQuery()

ExcelConnection.Close()

Paul

~~~~

Microsoft MVP(Visual Basic)
On 16 Aug 2006 12:12:15 -0700, "wreckingcru" <pr*********@gmail.comwrote:

¤ I''m trying to output a SQL query that is constructed thru my VB.net GUI
¤ into an excel file.
¤
¤ Here is the code I''m using:
¤
¤ ''Sqlstmt is the SQL query statement
¤ ''Conn is the SQL Connection object
¤
¤ cmd = New SqlCommand(Sqlstmt, Conn)
¤ datareader = cmd.ExecuteReader()
¤ ''datareader is the SQLdatareader object
¤
¤ Dim objexcel As Excel.Application
¤ Dim objwkb As Workbook
¤ Dim objwksht As Worksheet
¤
¤ objexcel = New Excel.Application
¤ objwkb = objexcel.Workbooks.Add
¤ objwksht = objwkb.ActiveSheet()
¤
¤ Dim cols = datareader.FieldCount()
¤
¤
¤ objwksht.Range(objwksht.Cells(1, 1), objwksht.Cells(5,
¤ cols)).CopyFromRecordset(datareader)
¤
¤ The line above is causing an exception, i.e., I never receive any data
¤ in the excel sheet. I put this in a try statement to catch the
¤ exception and it is this:
¤
¤ System.InvalidCastException: No such interface supported
¤ at System.RuntimeType.ForwardCallToInvokeMember(Strin g memberName,
¤ BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData&
¤ msgData)
¤ at Excel.Range.CopyFromRecordset(Object Data, Object MaxRows, Object
¤ MaxColumns)
¤ at WindowsApplication1.Form1.btGenReport_Click(Object sender,
¤ EventArgs e) in C:.....\Form1.vb:line 416
¤

CopyFromRecordset only supports ADO and DAO Recordset objects. It does not support ADO.NET.

You could try doing this using straight SQL:

Dim ExcelConnection As New
System.Data.OleDb.OleDbConnection("Provider=Micros oft.Jet.OLEDB.4.0;" & _
"Data Source=" & "c:\Test
Files\ExcelWB.xls" & ";" & _
"Extended Properties=Excel
8.0;")

ExcelConnection.Open()

Dim ExcelCommand As New System.Data.OleDb.OleDbCommand("SELECT * INTO [Orders] FROM [Orders]
IN '''' [ODBC;Driver={SQL Server};Server=(local);Database=Northwind;Trusted_ Connection=yes];",
ExcelConnection)

ExcelCommand.ExecuteNonQuery()
ExcelConnection.Close()
Paul
~~~~
Microsoft MVP (Visual Basic)


这篇关于SQL查询到Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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