在ASP.NET访问Excel文件

2021-03-22 15:04:43 浏览数 (1)

<!--StartFragment-->C# Code

---------------------------------------------

<%@ Page Language="C#" %>

<%@ Import Namespace="System.Data" %>

<%@ Import Namespace="System.Data.ADO" %>

<script language="C#" runat="server">

protected void Page_Load(Object Src, EventArgs E)

{

string strConn;

strConn = "Provider=Microsoft.Jet.OLEDB.4.0;"

"Data Source=C://exceltest.xls;"

"Extended Properties=Excel 8.0;";

ADODataSetCommand myCommand = new ADODataSetCommand("SELECT * FROM [Sheet1$]", strConn);

DataSet myDataSet = new DataSet();

myCommand.FillDataSet(myDataSet, "ExcelInfo");

DataGrid1.DataSource = myDataSet.Tables["ExcelInfo"].DefaultView;

DataGrid1.DataBind();

}

</script>

<p><asp:Label id=Label1 runat="server">Excel表内容:</asp:Label></p>

<asp:DataGrid id=DataGrid1 runat="server"/>

VB.NET Code

----------------------------------------------

<%@ Page Language="VB" %>

<%@ Import Namespace="System.Data" %>

<%@ Import Namespace="System.Data.ADO" %>

<script language="VB" runat="server">

Sub Page_Load(sender As Object, e As EventArgs)

Dim myDataset As New DataSet()

Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;" _

& "Data Source=C:/exceltest.xls;" _

& "Extended Properties=""Excel 8.0;"""

Dim myADODataSetCommand As New ADODataSetCommand("SELECT * FROM [Sheet1$]", strConn)

myADODataSetCommand.TableMappings.Add("Table", "ExcelTest")

myADODataSetCommand.FillDataSet(myDataset)

DataGrid1.DataSource = myDataset.Tables(0).DefaultView

DataGrid1.DataBind()

End Sub

</script>

<p><asp:Label id=Label1 runat="server">Excel表格内容:</asp:Label></p>

<asp:DataGrid id=DataGrid1 runat="server"/>

本文由来源 21aspnet,由 javajgs_com 整理编辑,其版权均为 21aspnet 所有,文章内容系作者个人观点,不代表 Java架构师必看 对观点赞同或支持。如需转载,请注明文章来源。

0 人点赞