关闭

关于ASP.NET访问Excel 电子表格的代码

标签: excelasp.netdatagridspreadsheetdatasetproperties
706人阅读 评论(0) 收藏 举报
分类:
本文是关于ASP.NET访问Excel 电子表格的代码
 
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;";

'You must use the $ after the object you reference in the spreadsheet

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">SpreadSheet Contents:</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()

'You can also use the Excel ODBC driver I believe - didn't try though

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

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

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

'You must use the $ after the object you reference in the spreadsheet

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">SpreadSheet Contents:</asp:Label></p>

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

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:13337次
    • 积分:193
    • 等级:
    • 排名:千里之外
    • 原创:4篇
    • 转载:12篇
    • 译文:0篇
    • 评论:0条