首 页文章中心客户留言enfullPDA完全版游戏天空全方位下载繁體中文
设为首页
加入收藏
联系我们
您当前的位置:技术文章大全 -> 文章中心 -> 技术开发 -> .Net专栏 -> 文章内容 退出登录 用户管理
栏目导航
· .Net专栏 · Script
· ASP学院 · PHP学院
· Web服务器 · XML学院
· SQL学院 · JSP学院
· 程序开发 · 数据库
热门文章
· 个人实习总结
· “八荣八耻”演讲稿...
· 毕业论文格式范文
· 八荣八耻 内容|八荣...
· 党校学习个人总结
· “八荣八耻”心得体...
· 教育实习总结报告
· 社会主义荣辱观教育...
· 市场营销系毕业实习...
· 党课学习思想汇报
相关文章
· Excel模板实用技巧五...
· 在Asp中使用存储过程...
· 在ASP中处理数据库的...
· 在ASP中按指定参数格...
· 在asp 中使用 sql_d...
· 在asp 中使用 sql_d...
· 一个可应用在ASP 标...
· 在Asp程序中取得表单...
· 在asp.net中操作sql...
· 在ASP.NET中实现弹出...
在asp.net中使用excel模板
作者:佚名  来源:不详  发布时间:2006-1-8 19:05:21  发布人:admin

减小字体 增大字体

Imports System.Runtime.InteropServices.Marshal


Then, replace the default Page_Load event with the code in Figure 1.


Private Sub Page_Load(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles MyBase.Load

Dim oExcel As New Excel.Application()

Dim oBooks As Excel.Workbooks, oBook As Excel.Workbook

Dim oSheets As Excel.Sheets, oSheet As Excel.Worksheet

Dim oCells As Excel.Range

Dim sFile As String, sTemplate As String

Dim dt As DataTable = _

CType(Application.Item("MyDataTable"), DataTable)



sFile = Server.MapPath(Request.ApplicationPath) & _

"\MyExcel.xls"



sTemplate = Server.MapPath(Request.ApplicationPath) & _

"\MyTemplate.xls"



oExcel.Visible = False : oExcel.DisplayAlerts = False



'Start a new workbook

oBooks = oExcel.Workbooks

oBooks.Open(Server.MapPath(Request.ApplicationPath) & _

"\MyTemplate.xls") 'Load colorful template with chart

oBook = oBooks.Item(1)

oSheets = oBook.Worksheets

oSheet = CType(oSheets.Item(1), Excel.Worksheet)

oSheet.Name = "First Sheet"

oCells = oSheet.Cells



DumpData(dt, oCells) 'Fill in the data



oSheet.SaveAs(sFile) 'Save in a temporary file

oBook.Close()



'Quit Excel and thoroughly deallocate everything

oExcel.Quit()

ReleaseComObject(oCells) : ReleaseComObject(oSheet)

ReleaseComObject(oSheets) : ReleaseComObject(oBook)

ReleaseComObject(oBooks) : ReleaseComObject(oExcel)

oExcel = Nothing : oBooks = Nothing : oBook = Nothing

oSheets = Nothing : oSheet = Nothing : oCells = Nothing

System.GC.Collect()

Response.Redirect(sFile) 'Send the user to the file

End Sub



'Outputs a DataTable to an Excel Worksheet

Private Function DumpData(ByVal _

dt As DataTable, ByVal oCells As Excel.Range) As String

Dim dr As DataRow, ary() As Object

Dim iRow As Integer, iCol As Integer



'Output Column Headers

For iCol = 0 To dt.Columns.Count - 1

oCells(2, iCol + 1) = dt.Columns(iCol).ToString

Next



'Output Data

For iRow = 0 To dt.Rows.Count - 1

dr = dt.Rows.Item(iRow)

ary = dr.ItemArray

For iCol = 0 To UBound(ary)

oCells(iRow + 3, iCol + 1) = ary(iCol).ToString

Response.Write(ary(iCol).ToString & vbTab)

Next

Next

End Fun
[] [返回上一页] [打 印] [收 藏]
∷相关文章评论∷    (评论内容只代表网友观点,与本站立场无关!) [更多评论...]
友情连接 - 网站地图 -