ASP.NET(vb.net) & Read Multiple worksheet from excel (Excel.Application) - This article example scripts you will learn how to Read multiple worksheet from excel using ASP.NET Scripts
ShotDev Focus:
- ASP.NET(vb.net) & Read Multiple worksheet from excel (Excel.Application)
Example
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") dt1.Columns.Add("Cloumn5") dt1.Columns.Add("Cloumn6") 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 dr1("Cloumn5") = xlSheet1.Cells.Item(i, 5).Value dr1("Cloumn6") = xlSheet1.Cells.Item(i, 6).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") dt2.Columns.Add("Cloumn5") dt2.Columns.Add("Cloumn6") 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 dr2("Cloumn5") = xlSheet2.Cells.Item(i, 5).Value dr2("Cloumn6") = xlSheet2.Cells.Item(i, 6).Value dt2.Rows.Add(dr2) i = i + 1 Loop '*** End DataTable (2) ***' '*** BindData To GridView (1) ***' Me.myGridView1.DataSource = dt1 Me.myGridView1.DataBind() '*** BindData To GridView (2) ***' Me.myGridView2.DataSource = dt2 Me.myGridView2.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>ShotDev.Com Tutorial</title> </head> <body> <form id="form1" runat="server"> <asp:GridView id="myGridView1" runat="server"></asp:GridView> <br /><br /> <asp:GridView id="myGridView2" runat="server"></asp:GridView> </form> </body> </html>
Screenshot