zoukankan      html  css  js  c++  java
  • 如何使用 ASP.NET、ADO.NET 和 Visual C# .NET 查询和显示 Excel 数据

    http://support.microsoft.com/kb/306572/zh-cn

    Page_Load 事件中:

    // Create connection string variable. Modify the "Data Source"
    // parameter as appropriate for your environment.
    String sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
    	"Data Source=" + Server.MapPath("../ExcelData.xls") + ";" +
    	"Extended Properties=Excel 8.0;";
    
    // Create connection object by using the preceding connection string.
    OleDbConnection objConn = new OleDbConnection(sConnectionString);
    
    // Open connection with the database.
    objConn.Open();
    
    // The code to follow uses a SQL SELECT command to display the data from the worksheet.
    
    // Create new OleDbCommand to return data from worksheet.
    OleDbCommand objCmdSelect =new OleDbCommand("SELECT * FROM myRange1", objConn);
    
    // Create new OleDbDataAdapter that is used to build a DataSet
    // based on the preceding SQL SELECT statement.
    OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
    
    // Pass the Select command to the adapter.
    objAdapter1.SelectCommand = objCmdSelect;
    
    // Create new DataSet to hold information from the worksheet.
    DataSet objDataset1 = new DataSet();
    
    // Fill the DataSet with the information from the worksheet.
    objAdapter1.Fill(objDataset1, "XLData");
    
    // Bind data to DataGrid control.
    DataGrid1.DataSource = objDataset1.Tables[0].DefaultView;
    DataGrid1.DataBind();
    
    // Clean up objects.
    objConn.Close();
    			
  • 相关阅读:
    JMeter之录制脚本
    好的软件测试人员简历是什么样子的?
    好的软件测试人员简历是什么样子的?
    luogu P2002 消息扩散
    luogu P1726 上白泽慧音
    luogu P1038 神经网络
    luogu P1418 选点问题
    luogu P1824 进击的奶牛
    luogu P1330 封锁阳光大学
    luogu P1546 最短网络 Agri-Net
  • 原文地址:https://www.cnblogs.com/no7dw/p/1509801.html
Copyright © 2011-2022 走看看