Posted by: Red Souldier | June 4, 2009

Read Excel Cell Value from ASP.NET 2008 – VB.NET

In this article I will show how to read value from excel per cell,]

First Im using FileUpload component from asp.net

	<td>
                <asp:FileUpload ID="uplFile" runat="server"  Width="367px"/>
	</td>

Don’t Forget to add triger to set autopostback, or else some people may counter error when the want to get the file name after they click browse button. Normally you put this code at the buttom

      	<Triggers>
              <asp:PostBackTrigger ControlID="btnUpload" />
        </Triggers>

At behind code import
Imports Excel = Microsoft.Office.Interop

In event click

        Dim oApp As New Excel.Excel.Application()
        Dim oWBa As Excel.Excel.Workbook
        Dim oWS As Excel.Excel.Worksheet

            If uplFile.PostedFile.FileName <> "" Then
                filename = Mid(uplFile.PostedFile.FileName, InStrRev(uplFile.PostedFile.FileName, "\") + 1, Len(uplFile.PostedFile.FileName) - InStrRev(uplFile.PostedFile.FileName, "\"))
                uplFile.PostedFile.SaveAs(System.AppDomain.CurrentDomain.BaseDirectory & filename)

                oWBa = oApp.Workbooks.Open(System.AppDomain.CurrentDomain.BaseDirectory & filename)
                oWS = DirectCast(oWBa.Worksheets(1), Excel.Excel.Worksheet)
                oApp.Visible = False

                    Dim strkey2(3) As String
                    strkey2(0) = CStr(oWS.Range("A1")
                    strkey2(1) = CStr(oWS.Range("B1")
                    strkey2(2) = CStr(oWS.Range("C1")
                    strkey2(3) = CStr(oWS.Range("D1")

                   oWS = Nothing
                   oWBa.Close()
                   oWBa = Nothing
                   oApp.Quit()
                   oApp = Nothing

     End If
Advertisements

Responses

  1. I usually don’t post on Blogs but ya forced me to, great info.. excellent! … I’ll add a backlink and bookmark your site.

    I’m Out! 🙂

  2. Hi there, this code seems really neat and handy but I couldn’t get it to work (total newbie!).

    Basically I get this error message: “Type Excel.Excel.Application is not defined”.

    I know I’m missing something really very basic, butr as I said I’m a complete newbie. What have I left out?

    thanks in advance . . . Simonium.

    • Have you installed microsoft office excel. This program need office dll. 🙂

    • it seems missing reference . Expand references and choose Add Reference and choose the COM Tab. Find Microsoft Excel 11.0 Object Library and add

      it should work


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: