asp教程.net 读取excel数据
<%@ 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.111cn.net/1999/xhtml" >
<head runat="server">
<title>asp.net教程 读取excel数据</title>
</head>
<body>
<form id="form1" runat="server">
<div>
第<asp:textbox id="textbox3" runat="server" width="60px" text="1" />工作表
第<asp:textbox id="textbox1" runat="server" width="60px" text="1" />行
第<asp:textbox id="textbox2" runat="server" width="60px" text="1" />列<br />
excel数据:<input id="file1" runat="server" name="file1" size="30" type="file" />
<asp:button id="btnrexcel" runat="server" text="读取excel的某行某列" /></div>
</form>
</body>
</html>
<%
imports microsoft.office.interop
partial class _excel
inherits system.web.ui.pageprotected 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("请选择要导入的excel数据文件!")
elsedim excelfilepath as string = strpath
dim myexcel as excel.application = new excel.applicationclass() '创建一个excel对象
dim omissing as object = system.reflection.missing.value
myexcel.application.workbooks.open(excelfilepath, omissing, omissing, omissing, omissing, omissing, omissing, omissing, omissing, omissing, omissing, omissing, omissing, omissing, omissing)
dim mybook as excel.workbook = myexcel.workbooks(1) '定义一个工作表组
dim mysheet as excel.worksheet = ctype(mybook.worksheets(cint(textbox3.text)), excel.worksheet) '定义一个工作表dim r as excel.range = ctype(mysheet.cells(cint(textbox1.text), cint(textbox2.text)), excel.range)
me.response.write("excel中第" & textbox1.text & "行 第" & textbox2.text & "列的值:" & (r.value).tostring)
excelgc(myexcel, mybook, mysheet, r)end if
end sub
'关闭excel进程
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教程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
%>