电信主站 网通分站
购买流程 付款方式 常见问题 在线提问 续租服务 购物车
用户名: 密 码: 忘记密码?
首 页
域名注册
虚拟主机
双线主机
服务器租用
VPS主机
企业邮局
代理专区
客服中心
虚拟主机行业资讯 虚拟主机评测对比 互联网最新动态 技术学院 站长资讯 在线教程 网站运营
搜索优化 服务器 网络编程 图形图象 站长之家 网页制作 操作系统
冲浪宝典 软件教学 视频通信 办公软件 邮件系统 网络安全 认证考试
您当前位置:西部数码->资讯中心-> 在线教程-> .NET
vb.net中不需要EXCEL导出成XSL-.NET教程,VB.Net语言
作者:网友供稿 点击:58
  西部数码-全国虚拟主机10强!20余项虚拟主机管理功能,全国领先!第6代双线路虚拟主机,南北访问畅通无阻!虚拟主机可在线rar解压,自动数据恢复设置虚拟目录等.虚拟主机免费赠送访问统计,企业邮局.Cn域名注册10元/年,自助建站480元起,免费试用7天,满意再付款!P4主机租用799元/月.月付免压金!
文章页数:[1] 
网上有太多的使用打开隐藏文档的方式将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
大家一起研究,共同进步~~~

文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!
相关主题
文章页数:[1] 
Google
热门文章
·经典收藏之 - C++内存管理详解-.NET教程,C#语言
·Master Page 初探-.NET教程,评论及其它
·GDI+编程10个基本技巧-.NET教程,评论及其它
·VB.NET中让Textbox只能输入数字(二)-.NET教程,VB.Net语言
·stl应用小问题-.NET教程,评论及其它
·WIN32中颜色值(COLORREF)与.NET中颜色值(Color)的转换-ASP教程,系统相关
·打造自己的专业图像工具-Visual C++ 2005图像编程系列【三】-.NET教程,C#语言
·.Net中常见问题及解决方法归类-.NET教程,.NET Framework
·Lex和Yacc从入门到精通(3)--一个极其简单的lex和yacc程序-.NET教程,评论及其它
·VB下几个非常有用的函数-.NET教程,VB.Net语言

最新文章
·VC#初学入门:第一个Windows程序
·ASP.NET 2.0-选用DataSet或DataReader
·用.net 处理xmlHttp发送异步请求
·asp.net创建文件夹的IO类的问题
·asp.net 2.0 中加密web.config 文件中的配置节
·关于ASP.NET调用JavaScript的实现
·如何实现ASP.NET网站个性化
·Acegi安全系统的配置-.NET教程,评论及其它
·Spring安全系统:Acegi Security Acegi简介-.NET教程,评论及其它
·Biztalk 开发之 架构和实例的验证-.NET教程,评论及其它


 
 


版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!

特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。
  打印  刷新  关闭
返回首页 |关于我们 | 联系我们 | 付款方式 | 创业联盟 | 虚拟主机 | 资讯中心 | 友情链接 | 网站地图

版权所有 西部数码(www.west263.com)
CopyRight (c) 2002~2006 west263.com all right reserved.
公司地址:四川成都市万和路90号天象大厦4楼 邮编:610031
电话总机:028-86262244 86263048 86263408 86263960 86264018 86267838
售前咨询:总机转201 202 203 204 206 208
售后服务:总机转211 212 213 214
财务咨询:总机转224 223 传真:028-86264041 财务QQ:点击发送消息给对方635483282
售前咨询QQ:点击发送消息给对方2182518 点击发送消息给对方241975952 点击发送消息给对方275026793 点击发送消息给对方408235859
售后服务QQ:点击发送消息给对方17708515 点击发送消息给对方307742704 点击发送消息给对方287976517 点击发送消息给对方363783715
《中华人民共和国增值电信业务经营许可证》编号:川B2-20030065号