{BoyLee's Blog}
目前有点菜,正在变得越来越不菜,頑張って .........
随笔- 54 文章- 0 评论- 200
博客园
首页
新随笔
管理
订阅
asp.net操作excel
Private
Sub Page_Load()
Sub
Page_Load(
ByVal
sender
As
System.Object,
ByVal
e
As
System.EventArgs)
Handles
MyBase
.Load
On
Error
GoTo
ERR_FLAG
Dim
strPath
As
String
Dim
strSave
As
String
Dim
strOpenFile
As
String
Dim
excelWorkBook
As
Excel.Workbook
Dim
excelWorkSheet
As
Excel.Worksheet
Dim
excelRange
As
Excel.Range
Dim
excelApp
As
New
Excel.Application
strOpenFile
=
Server.MapPath(
"
ExcelTemplates\TestExcel.xls
"
)
strSave
=
Server.MapPath(
"
ReportOutput\TestExcel.xls
"
)
excelApp.Application.DisplayAlerts
=
False
excelApp.Visible
=
False
excelWorkBook
=
excelApp.Workbooks.Open(strOpenFile)
excelWorkSheet
=
excelWorkBook.Worksheets(
1
)
If
Not
setWorkSheet(excelWorkSheet)
Then
GoTo
ERR_FLAG
End
If
excelWorkBook.SaveAs(strSave)
excelWorkBook.Close()
excelApp.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelWorkSheet)
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelWorkBook)
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp)
Call
showFile(strSave)
Exit Sub
ERR_FLAG:
If
Not
excelWorkSheet
Is
Nothing
Then
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelWorkSheet)
End
If
If
Not
excelWorkBook
Is
Nothing
Then
excelWorkBook.Close()
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelWorkBook)
End
If
If
Not
excelApp
Is
Nothing
Then
excelApp.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp)
End
If
excelWorkSheet
=
Nothing
excelWorkBook
=
Nothing
excelApp
=
Nothing
System.GC.Collect(
0
)
Response.Write(Err.Description)
End Sub
Function getData()
Function
getData()
As
DataSet
Dim
con
As
SqlConnection
con
=
New
SqlConnection(
"
server=wxhy-lee;database=JustTest;uid=sa;pwd=sa;
"
)
Dim
da
As
SqlDataAdapter
da
=
New
SqlDataAdapter(
"
Select ClassID,ClassName,UpClassID,ClassLevel From ClassName
"
, con)
Dim
ds
As
New
DataSet
da.Fill(ds)
Return
ds
End Function
Function setWorkSheet()
Function
setWorkSheet(
ByRef
excelWorkSheet
As
Excel.Worksheet)
As
Boolean
On
Error
GoTo
ERR_FLAG
setWorkSheet
=
False
Dim
dSet
As
DataSet
Dim
intI
As
Integer
Dim
intTotal
As
Integer
dSet
=
getData()
intTotal
=
dSet.Tables(
0
).Rows.Count
If
intTotal
=
0
Then
GoTo
ERR_FLAG
End
If
For
intI
=
2
To
intTotal
+
1
excelWorkSheet.Range(
"
A
"
&
intI).Value
=
dSet.Tables(
0
).Rows(intI
-
2
).Item(
"
ClassID
"
)
excelWorkSheet.Range(
"
B
"
&
intI).Value
=
dSet.Tables(
0
).Rows(intI
-
2
).Item(
"
ClassName
"
)
excelWorkSheet.Range(
"
C
"
&
intI).Value
=
IIf
(
IsDBNull
(dSet.Tables(
0
).Rows(intI
-
2
).Item(
"
UpClassID
"
)),
"
NULL
"
, dSet.Tables(
0
).Rows(intI
-
2
).Item(
"
UpClassID
"
))
excelWorkSheet.Range(
"
D
"
&
intI).Value
=
dSet.Tables(
0
).Rows(intI
-
2
).Item(
"
ClassLevel
"
)
Next
dSet.Clear()
dSet.Dispose()
setWorkSheet
=
True
Exit Function
ERR_FLAG:
Return
False
End Function
Sub showFile()
Sub
showFile(
ByVal
strSave
As
String
)
Response.ContentType
=
"
application/ms-excel
"
Response.AddHeader(
"
content-disposition
"
,
"
attachment; filename=
"
&
"
TestExcel.xls
"
)
Response.WriteFile(strSave)
Response.End()
End Sub
posted @ 2007-04-04 14:14
BoyLee
阅读(375)
评论(0)
编辑
收藏
所属分类:
Asp.Net
新用户注册
刷新评论列表
标题
姓名
主页
Email
(博主才能看到)
验证码
*
看不清,换一张
[
登录
][
注册
]
内容(请不要发表任何与政治相关的内容)
网站首页
新闻频道
社区
小组
博问
网摘
闪存
找找看
Remember Me?
登录
使用高级评论
新用户注册
返回页首
恢复上次提交
[使用Ctrl+Enter键可以直接提交]
该文被作者在 2007-07-24 10:13 编辑过
相关文章:
ExtJs+WCF+LINQ实现分页Grid
[添砖加瓦]:ExtJS+WCF+LINQ打造全功能Grid
ASP.NET AJAX入门系列
使用Visual Studio2005入门.Net2.0系列视频教程
HttpHeadle是什么
Asp.Net在无Cookie状态下会话问题
Asp.Net通用权限管理系统 专用代码生成工具(DDBuildTools) 1.0
Asp.Net权限管理系统 专用代码生成工具(DDBuildTools) 1.1.0
相关链接:
所属分类的其他文章:
[转]petshop4.0设计说明
推荐几个asp.net测试空间
收藏: C#设计模式之简单工厂篇
收藏:简单工厂模式(SimpleFactory-C#)
vb.net版中文验证码
验证码 收藏
asp.net操作csv
asp.net操作excel
UltraWebGrid多条数据scoll bar定位解决
UltraWebGrid使用指南(原创)
最新IT新闻:
百度网络交易平台定名“有啊”
全屏触摸力反馈 黑莓Storm发布
微软推出Touchless:无需触屏的触屏技术
AMD剥离芯片制造业务 获60亿美元投资
Google向Wine贡献更多代码
<
2007年4月
>
日
一
二
三
四
五
六
25
26
27
28
29
30
31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
1
2
3
4
5
与我联系
发短消息
搜索
我参加的小组
每天一句日语
蛋蛋日语学堂
ASP.NET
软件工程师日语
设计模式
web标准设计
.NET 3.x
jQuery
AJAX
随笔分类
(49)
Asp.Net(14)
C/C++(1)
JS/AJAX(1)
Others(2)
Win Form(1)
非技术类(27)
日语学习(1)
数据库(2)
随笔档案
(54)
2008年7月 (1)
2008年4月 (2)
2008年2月 (1)
2007年12月 (1)
2007年11月 (3)
2007年10月 (1)
2007年9月 (3)
2007年8月 (11)
2007年7月 (6)
2007年5月 (2)
2007年4月 (9)
2007年3月 (8)
2007年1月 (1)
2006年11月 (1)
2006年10月 (1)
2006年9月 (3)
友情链接
BoyLee's Blog
Ktgu
Temptation
戏水
小 8
积分与排名
积分 - 45887
排名 - 959
最新随笔
1. 希望大家不要为了首页而首页
2. 突然发现很少在这里写东西了
3. 哎......
4. 周末公司新人见闻录
5. 招聘面试趣闻录
6. 程序产品商业包装的重要性
7. 多收了三五斗(在日IT民工版)
8. Microsoft IME 操作(日文输入)
9. 刚颁布的中国贫富标准线,来看看程序员生活在哪个阶层
10. 不知道是我技术提高了,还是园子堕落了
最新评论
1. re: UltraWebGrid使用系列(一)
兄弟,请问如何访问UltraWebGrid某行某列内dropdownlist控件的值阿(dropdownlist是预先在前台CellTemplate里定义好的模板列)
(阿辉)
2. re: 希望大家不要为了首页而首页
@水言木
我可不敢放首页,被人骂了很多次了.再放到首页,那些人就都来骂了.哈哈 (BoyLee)
3. re: 希望大家不要为了首页而首页
@LanceZhang
那个是日文里常用字符.中文打出来正常.但是日文里可能JIS编码问题,不正确,不明白啊
我那个博客3年多了吧.以前是一个网站,后来关了.写写博客.呵呵 (BoyLee)
4. re: 希望大家不要为了首页而首页
这篇文章一定要放首页啊,最近首页的某些文章确实让人看不下去了,大都是新手搞的,园子要加强刚注册入园时的思想教育了 (水言木)
5. re: 希望大家不要为了首页而首页
另外,楼主,你的博客
?访问量那么多,佩服佩服,有什么经验吗? (LanceZhang)
阅读排行榜
1. UltraWebGrid使用系列(一)(3912)
2. UltraWebGrid使用系列(三) (2854)
3. UltraWebGrid使用系列(二)(1584)
4. UltraWebGrid使用系列(四)(1525)
5. UltraWebGrid使用指南(原创) (1307)