在asp.net中使用excel模板
2014-04-22 18:56
351 查看
在asp.net中使用excel模板
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
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
相关文章推荐
- 在asp.net中使用excel模板
- ASP.NET中如何使用前台和后台验证
- ASP.NET MVC的WebApi使用
- asp.net DoDragDrop 方法的使用
- asp.net之DataList的使用方法,及分页(存储过程创建),编辑,更新,删除 .
- asp.net多线程在web页面中简单使用
- 在ASP.NET中使用Session常见问题集锦
- asp.net .ashx文件使用Server.MapPath解决方法
- asp.net 调用 mysql 分页存储过程,repeater使用mysql分页存储过程,mysql 存储过程的注意事项
- 在ASP.NET使用JavaScript显示信息提示窗口实现原理及代码
- asp.net 2.0中使用OWC组件
- ASP.NET中使用AJAX后,Session丢失异常的自定义处理。
- asp.net 使用NPOI实现导出Excel功能
- 使用ASP.NET Atlas编写显示真实进度的ProgressBar(进度条)控件
- asp.net Ajax--PopupControl控件使用
- asp.net 2.0中傻瓜式使用soap header
- 如何在ASP.NET页面中使用异步任务(PageAsyncTask)
- asp.net 服务器控件使用HTML(input)元素属性
- kindeditor asp.net的使用方法 超简单
- 使用ASP.NET Web Api构建基于REST风格的服务实战系列教程【外传】——Attribute Routing