成人午夜激情影院,小视频免费在线观看,国产精品夜夜嗨,欧美日韩精品一区二区在线播放

在ASP.NET中使用Excel模板

2010-08-28 10:48:52來源:西部e網作者:

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 Function

關鍵詞:ASP.NET

贊助商鏈接:

主站蜘蛛池模板: 云霄县| 靖西县| 昌黎县| 沙湾县| 仁怀市| 兰州市| 绵竹市| 龙山县| 双柏县| 仁布县| 正阳县| 公安县| 西乡县| 普格县| 彩票| 竹北市| 双峰县| 廊坊市| 靖江市| 厦门市| 临海市| 山西省| 高阳县| 红桥区| 鲜城| 岢岚县| 布拖县| 岳西县| 巫山县| 高雄县| 江口县| 昭觉县| 南京市| 九江市| 正蓝旗| 密云县| 清苑县| 噶尔县| 呈贡县| 五常市| 甘德县|