程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> 在ASP.NET訪問Excel文件 (VB and C#)

在ASP.NET訪問Excel文件 (VB and C#)

編輯:關於C語言
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"/>
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved