在asp.net中使用excel模板
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 Function
下一篇:关于ASPNET在IIS一些问题的经验总结 >>
相关文章:
- · INI文件的操作(ASP.NET+C#)
- · Asp.net+Xml开发网络硬盘
- · ASP.NET高级应用(3)
- · ASP.NET高级应用(2)
- · ASP.NET高级应用(1)
- · Net 是未来的趋势, 为什么?
- · 迁移到 ASP .NET:需考虑的重要问题
- · ASP+与VB.Net问答QA总汇
- · 基于.NET的Web应用框架构建模式
- · Observer模式深度探索
- · VSS控制存储过程,及其asp.net的远程调试
- · 简介使用ASP.NET访问Oracle数据库的方法
- · ASP.NET编程中的十大技巧
- · ASP.NET Whidbey中personalization和membership的一些特征
- · 使用更精简的代码保证 ASP.NET 应用程序的安全
- · ASP.NET Whidbey 中新的代码编译功能
- · ASP.NET的缓存技术
- · ASP.net 中的页面继承实现和通用页面的工厂模式的实现
- · ASP.NET下MVC设计模式的实现
- · 页面无限跳转间如何保存页面状态 (3)
- · 页面无限跳转间如何保存页面状态(2)
- · 页面无限跳转间如何保存页面状态(1)
- · 基于asp.net的webmenu的数据操作(5)
- · 基于asp.net的webmenu的数据操作(3)
- · 基于asp.net的webmenu的数据操作(2)
- · 基于asp.net的webmenu的数据操作(1)
- · 十天学会ASP.net之第十天
- · 十天学会ASP.net之第九天
- · 十天学会ASP.net之第八天
- · 十天学会ASP.net之第七天
- · 十天学会ASP.net之第六天
- · 十天学会ASP.net之第五天
- · 十天学会ASP.net之第四天
- · 十天学会ASP.net之第三天
- · 十天学会ASP.net之第二天
- · 十天学会ASP.net之第一天
- · 图片上传到数据库并显示(C#+Access)
- · 关于ASPNET在IIS一些问题的经验总结
