ASP.NET Read Excel Multiple Sheet (Excel Application)       | 
   
 
			  
			  
                ASP.NET Read Excel Multiple Sheet (Excel Application) เป็นการเขียน ASP.NET กับ Excel ในการอ่านไฟล์ Excel โดยทำการอ่านจากหลาย ๆ Sheet และนำข้อมูลมาแสดงคนล่ะ DataGrid 
  
Framework 1.1,2.0,3.0,4,0 
 
Excel for C# อ่านและดัดแปลงได้จากบทความนี้ 
 
 
Syntax 
 
Dim xlApp As New Excel.Application  
 
AspNetReadExcelMultipleSheet.aspx 
 
<%@ 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
        Dim xlSheet2 As Excel.Worksheet
        xlBook = xlApp.Workbooks.Open(Server.MapPath(OpenFile))
        xlBook.Application.Visible = False
		
		'*** Read Sheet (1) ***'
        xlSheet1 = xlBook.Worksheets(1)		
		'*** Create DataTable (1) ***'
        Dim dt1 As New System.Data.DataTable
        Dim dr1 As System.Data.DataRow
        '*** Column ***'
        dt1.Columns.Add("Cloumn1")
        dt1.Columns.Add("Cloumn2")
        dt1.Columns.Add("Cloumn3")
        dt1.Columns.Add("Cloumn4")
		i = 2
		Do While Not Trim(xlSheet1.Cells.Item(i, 1).Value) = ""
            '*** Rows ***'
            dr1 = dt1.NewRow
            dr1("Cloumn1") = xlSheet1.Cells.Item(i, 1).Value
            dr1("Cloumn2") = xlSheet1.Cells.Item(i, 2).Value
            dr1("Cloumn3") = xlSheet1.Cells.Item(i, 3).Value
            dr1("Cloumn4") = xlSheet1.Cells.Item(i, 4).Value
            dt1.Rows.Add(dr1)
			i = i + 1
		Loop
        '*** End DataTable (1) ***'
		
		'*** Read Sheet (2) ***'
        xlSheet2 = xlBook.Worksheets(2)		
		'*** Create DataTable (2) ***'
        Dim dt2 As New System.Data.DataTable
        Dim dr2 As System.Data.DataRow
        '*** Column ***'
        dt2.Columns.Add("Cloumn1")
        dt2.Columns.Add("Cloumn2")
        dt2.Columns.Add("Cloumn3")
        dt2.Columns.Add("Cloumn4")
		i = 2
		Do While Not Trim(xlSheet2.Cells.Item(i, 1).Value) = ""
            '*** Rows ***'
            dr2 = dt2.NewRow
            dr2("Cloumn1") = xlSheet2.Cells.Item(i, 1).Value
            dr2("Cloumn2") = xlSheet2.Cells.Item(i, 2).Value
            dr2("Cloumn3") = xlSheet2.Cells.Item(i, 3).Value
            dr2("Cloumn4") = xlSheet2.Cells.Item(i, 4).Value
            dt2.Rows.Add(dr2)
			i = i + 1
		Loop
        '*** End DataTable (2) ***'
        '*** BindData To DataGrid (1) ***'
        Me.myDataGrid1.DataSource = dt1
        Me.myDataGrid1.DataBind()
        '*** BindData To DataGrid (2) ***'
        Me.myDataGrid2.DataSource = dt2
        Me.myDataGrid2.DataBind()
        '*** Quit and Clear Object ***'
		xlBook.Close()
		xlApp.Quit()
		
		xlApp.Application.Quit()
        xlSheet1 = Nothing
		xlSheet2 = 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="myDataGrid1" runat="server"></asp:DataGrid>
		<br /><br />
		<asp:DataGrid id="myDataGrid2" runat="server"></asp:DataGrid>
	</form>
</body>
</html> 
 
 
 
Screenshot  
 
 
               
  
              			
			  
								  
			  
  
                          
  |