แนวทางการเขียน asp.net กับ excel เพื่อแสดงข้อมูลผ่านหน้าเว็บ
Tag : - - - -
Date :
27 พ.ย. 2549 11:32:37
By :
hamlet character analysis
View :
2032
Reply :
2
No. 1
Guest
Code (VB.NET)
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="Excel"%>
<%@ Page Language="VB" %>
<script runat="server">
Sub Page_Load(sender As Object, e As EventArgs)
Dim OpenFile As String
Dim i As Integer
OpenFile = "MyXls/MyExcelDB.xls"
'*** Create Excel.Application ***'
Dim xlApp As New Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet1 As Excel.Worksheet
xlBook = xlApp.Workbooks.Open(Server.MapPath(OpenFile))
xlBook.Application.Visible = False
xlSheet1 = xlBook.Worksheets(1)
'*** Create DataTable ***'
Dim dt As New System.Data.DataTable
Dim dr As System.Data.DataRow
'*** Column ***'
dt.Columns.Add("Cloumn1")
dt.Columns.Add("Cloumn2")
dt.Columns.Add("Cloumn3")
dt.Columns.Add("Cloumn4")
i = 2
Do While Not Trim(xlSheet1.Cells.Item(i, 1).Value) = ""
'*** Rows ***'
dr = dt.NewRow
dr("Cloumn1") = xlSheet1.Cells.Item(i, 1).Value
dr("Cloumn2") = xlSheet1.Cells.Item(i, 2).Value
dr("Cloumn3") = xlSheet1.Cells.Item(i, 3).Value
dr("Cloumn4") = xlSheet1.Cells.Item(i, 4).Value
dt.Rows.Add(dr)
i = i + 1
Loop
'*** End DataTable ***'
'*** BindData To DataGrid ***'
Me.myDataGrid.DataSource = dt
Me.myDataGrid.DataBind()
'*** Quit and Clear Object ***'
xlApp.Application.Quit()
xlApp.Quit()
xlSheet1 = Nothing
xlBook = Nothing
xlApp = Nothing
End Sub
</script>
<html>
<head>
<title>ThaiCreate.Com ASP.NET - Excel Application</title>
</head>
<body>
<form id="form1" runat="server">
<asp:DataGrid id="myDataGrid" runat="server"></asp:DataGrid>
</form>
</body>
</html>