Asp.net reads Excel Data

Source: Internet
Author: User

Asp tutorial. net read excel Data
<% @ Page language = "vb" autoeventwireup = "false" codefile = "excel. aspx. vb" inherits = "_ excel" %>
<! Doctype html public "-// w3c // dtd xhtml 1.0 transitional // en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<Html xmlns = "http://www.bKjia. c0m/1999/xhtml">
<Head runat = "server">
<Title> asp.net tutorial reading excel Data </title>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
Worksheet No. <asp: textbox id = "textbox3" runat = "server" width = "60px" text = "1"/>
Row <asp: textbox id = "textbox1" runat = "server" width = "60px" text = "1"/>
Column <asp: textbox id = "textbox2" runat = "server" width = "60px" text = "1"/> <br/>
Excel Data: <input id = "file1" runat = "server" name = "file1" size = "30" type = "file"/>
<Asp: button id = "btnrexcel" runat = "server" text = "reading a column in an excel row"/> </div>
</Form>
</Body>
</Html>

<%

Imports microsoft. office. interop

Partial class _ excel
Inherits system. web. ui. page

Protected sub btnrexcel_click (byval sender as object, byval e as system. eventargs) handles btnrexcel. click
Dim strpath as string = file1.postedfile. filename
If strpath = "" then
Me. response. write ("select the excel data file to import! ")
Else

Dim excelfilepath as string = strpath
Dim myexcel as excel. application = new excel. applicationclass () 'create an excel Object
Dim omissing as object = system. reflection. missing. value
Myexcel. application. workbooks. open (audit, omissing, omissing)
Dim mybook as excel. workbook = myexcel. workbooks (1) 'defines a worksheet Group
Dim mysheet as excel. worksheet = ctype (mybook. worksheets (cint (textbox3.text), excel. worksheet) 'defines a worksheet

Dim r as excel. range = ctype (mysheet. cells (cint (textbox1.text), cint (textbox2.text), excel. range)

Me. response. write ("the value of the & textbox1.text &" row "& textbox2.text &" column in excel: "& (r. value). tostring)
Excelgc (myexcel, mybook, mysheet, r)

End if

End sub

'Close the excel Process

Private shared sub excelgc (byref myexcel as excel. application, byref mybook as excel. workbook, byref mysheet as excel. worksheet, byref r as excel. range)
Mybook. close (false, type. missing, type. missing)
Myexcel. quit ()
System. runtime. interops tutorial ervices. marshal. releasecomobject (r)
System. runtime. interopservices. marshal. releasecomobject (myexcel)
System. runtime. interopservices. marshal. releasecomobject (mysheet)
System. runtime. interopservices. marshal. releasecomobject (mybook)
Mysheet = nothing
Mybook = nothing
Myexcel = nothing
R = nothing
Gc. collect ()
End sub
End class

%>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.