欢迎光临
我们一直在努力

vb.net中不需要EXCEL导出成XSL-.NET教程,VB.Net语言

建站超值云服务器,限时71元/月

网上有太多的使用打开隐藏文档的方式将dataset或者datatable导出到excel.

这样做有几个问题:

1.excel进程很难杀死,甚至出现无响应

2.响应速度慢

3.必须要安装office才能使用

当然,也有它的优点,那就是格式化能力强,能够做出格式化的excel报表.但是比起常常出错,反映速度,以及特定环境看来,优点也不是很明显

在此贡献给大家一个不同的导出excel方式

优点是:

1.速度快

2.不容易出错

3.无需安装office

但是也有确定,就是导出的报表象excel一样,没有格式化.

因为此种方法就是将excel当成数据库来创建的.

以下为详细的程序,需要研究的朋友只需要建立一个win窗体,将以下代码覆盖原来的全部代码就ok了.不过请自己在bin目录下放一个数据库,或者将程序里面的连接代码改一下:

imports system.data.oledb

imports system.data

imports scripting

public class form1

inherits system.windows.forms.form

private dataset as new dataset

private mytable as datatable

private dbpath as string = application.startuppath & "\yj_manage.mdb" 这里请自己改

private dbconnstr as string = "provider=microsoft.jet.oledb.4.0;data source=" & dbpath

private dataadapter as oledbdataadapter

private dataconnection as oledbconnection

private dataset as dataset

#region " windows 窗体设计器生成的代码 "

public sub new()

mybase.new()

该调用是 windows 窗体设计器所必需的。

initializecomponent()

在 initializecomponent() 调用之后添加任何初始化

end sub

窗体重写 dispose 以清理组件列表。

protected overloads overrides sub dispose(byval disposing as boolean)

if disposing then

if not (components is nothing) then

components.dispose()

end if

end if

mybase.dispose(disposing)

end sub

windows 窗体设计器所必需的

private components as system.componentmodel.icontainer

注意: 以下过程是 windows 窗体设计器所必需的

可以使用 windows 窗体设计器修改此过程。

不要使用代码编辑器修改它。

friend withevents btnexport as system.windows.forms.button

friend withevents savedl as system.windows.forms.savefiledialog

<system.diagnostics.debuggerstepthrough()> private sub initializecomponent()

me.btnexport = new system.windows.forms.button

me.savedl = new system.windows.forms.savefiledialog

me.suspendlayout()

btnexport

me.btnexport.location = new system.drawing.point(104, 128)

me.btnexport.name = "btnexport"

me.btnexport.size = new system.drawing.size(80, 32)

me.btnexport.tabindex = 0

me.btnexport.text = "button1"

form1

me.autoscalebasesize = new system.drawing.size(6, 14)

me.clientsize = new system.drawing.size(292, 273)

me.controls.add(me.btnexport)

me.name = "form1"

me.text = "form1"

me.resumelayout(false)

end sub

#end region

private function getdatafromdb(byval sqlstr as string) as dataset

try

dataconnection = new oledbconnection

dataconnection.connectionstring = dbconnstr

dataadapter = new oledbdataadapter(sqlstr, dataconnection)

dataset = new dataset

dataset.clear()

dataadapter.fill(dataset)

dataconnection.close()

catch ex as exception

messagebox.show(err.description, "数据错误", messageboxbuttons.ok, messageboxicon.warning)

exit function

end try

if dataset.tables(0).rows.count > 0 then

return dataset

else

return nothing

end if

end function

private sub btnexport_click(byval sender as system.object, byval e as system.eventargs) handles btnexport.click

dim myoledbcn as new oledbconnection

dim myoledbcmd as new oledbcommand

dim introwscnt, intcolscnt as integer

dim strsql as string, strflname as string

dim fso as new filesystemobject

dataset = getdatafromdb("select * from employee_userinfo_main")

mytable = dataset1.tables(0)

savedl.title = "保持为"

savedl.filter = "xls工作薄|*.xls"

if savedl.showdialog = dialogresult.ok then

if savedl.filename <> "" then

strflname = savedl.filename()

else

exit sub

end if

else

exit sub

end if

try

me.cursor.current = system.windows.forms.cursors.waitcursor

myoledbcn.connectionstring = "provider=microsoft.jet.oledb.4.0;" & _

"data source=" & strflname & ";" & _

"extended properties=""excel 8.0;hdr=yes;"""

myoledbcn.open()

myoledbcmd.connection = myoledbcn

myoledbcmd.commandtype = commandtype.text

第一行插入列标题

strsql = "create table sheet1("

for intcolscnt = 0 to mytable.columns.count – 1

if intcolscnt <> mytable.columns.count – 1 then

strsql = strsql & mytable.columns(intcolscnt).caption & " text,"

else

strsql = strsql & mytable.columns(intcolscnt).caption & " text)"

end if

next

myoledbcmd.commandtext = strsql

myoledbcmd.executenonquery()

插入各行

for introwscnt = 0 to mytable.rows.count – 1

strsql = "insert into sheet1 values("

for intcolscnt = 0 to mytable.columns.count – 1

if intcolscnt <> mytable.columns.count – 1 then

strsql = strsql & mytable.rows(introwscnt).item(intcolscnt) & ","

else

strsql = strsql & mytable.rows(introwscnt).item(intcolscnt) & ")"

end if

next

myoledbcmd.commandtext = strsql

myoledbcmd.executenonquery()

next

messagebox.show("数据已经成功导入excel文件" & strflname, "数据导出", messageboxbuttons.ok, messageboxicon.information)

catch errcode as exception

msgbox("错误信息:" & errcode.message & vbcrlf & vbcrlf & _

"引发事件:" & errcode.targetsite.tostring, msgboxstyle.okonly + msgboxstyle.information, "错误来源:" & errcode.source)

exit sub

finally

myoledbcmd.dispose()

myoledbcn.close()

myoledbcn.dispose()

me.cursor.current = system.windows.forms.cursors.default

end try

end sub

end class

当然,将excel导入到access,mssql等数据库,datagrid等容器也是用相反类似的方法,将excel作为数据库来使用.有感兴趣的朋友可以加我qq:270499458

大家一起研究,共同进步~~~

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » vb.net中不需要EXCEL导出成XSL-.NET教程,VB.Net语言
分享到: 更多 (0)

相关推荐

  • 暂无文章