?????? ?? ????????? ???? ?? ??? ???? asp.NET, ADO.NET ?? ????? C# .NET ?? ????? ?? ???? ?? excel

???? ?????? ???? ??????
???? ID: 306572 - ?? ???????? ?? ?????? ??? ?? ?? ???? ???? ???? ??.
??? ?? ??????? ???? | ??? ?? ??????? ????

?? ????? ??

??????

?? ??? ?? ??? ???? ????????? ???? ?? ?? Visual C# .NET ?? ????? ?? ???? Excel ????? ????? ?? ?? (.aspx) asp.NET ??? ?? ???? ?? ????????? ???? ?? ??? ???? ?????


????? Excel ?????????? ??? ?????

  1. Microsoft Excel ??????? ????, ?? ???? ??? ?? ?? ????????????? ??????
  2. ?? ?????? Excel ??????? ????? ?? ??? ?? ?????????? ??? ????? ??????? ??????:
    ?? ?????? ?? ??????? ?????? ?????? ?? ??????? ????
    AB
    1FirstNameLastName
    2ScottBishop
    3Katie??????
    ???:: ?????? ???? ??? A1 ??? ?? ?????? ?? ??????? ???? ??, ?? ???? ??????? ?????? ?? ?????????? ??? ???? ?? ??? ?? ???? ?? ???? ???? ????
  3. ????????? ?? ????? ?????? ???? ????? ??????? ?????
  4. ????? ???????????? ???????? ??,????? ????-????? ????, ?? ???? ???????????? ????.
  5. ?????????????????? ??? ?????? ????? ???, ??????myRange1?? ????-????? ????, ?? ???? ???OK.
  6. ????? ???????????? ??,??????. ?????????? ?????????? ???, ??? ????? ??? (?? ?? ?????????? C:\InetPub\Wwwroot\) ?? ??? ????? ?????????? ?????? ????? ???, ??????ExcelData.xls. ????? ????,OK.
  7. ????? ???????????? ??,exit.

????? C# .NET ????? asp.NET ????? ?????

This code sample demonstrates how to query and display information in an Excel worksheet. The following code uses the worksheet that you created in the previous section.
  1. Open Microsoft Visual Studio .NET. The Visual Studio .NET Integrated Development Environment (IDE) is displayed.
  2. ????? ???????????? ??,????? ????-????? ????, ?? ???? ????????????.
  3. ???????? ?????????dialog box, under????????? ??????????? ????,Visual C# Projects. ?? ???????????????????? ????,ASP.NET ??? ?????????.
  4. ???????? ?????????dialog box, locate the???, ???????text boxes. ?? ????????text box is not available (it appears grayed out or dimmed). The?????text box contains the following text (or similar):
    http://localhost/WebApplication1
    Replace the text in the?????text box withhttp://localhost/ExcelCSTest?? ????-????? ????, ?? ???? ???OK. ??? ??? ????????? ????? ??, ?? ???? ??? ??????? WebForm1.aspx ??? ????? ???
  5. Visual Studio .NET IDE, ??? ?????? Explorer ????? ?? ?????? ?????? ??? ?? ??? ???? ???? ???, ????? ?????????? Explorer????? ????????????? ??..
  6. ?????? Explorer ???, ???? ?????WebForm1.aspx?? ????-????? ????, ?? ???? ???????? ???????????? ?? ?????? ?? ??? designer ????????? ????? Designer ???????? ????? ?? ????? ?? ?????? ????? ?? ??? ???? ?????? ???? ???
  7. ????? ????? ?? ?????? ?????? ???? IDE ?????? ?????? ?? ???? ?? ???? ????? ?? ??? (?? ????? IDE ?? ???? ??? ????? ???? ??) ?? ??? ??? ????? ? ???? ??? ??? ?? ????? ????? ?? ???? ???? ????, ????? ????????? ?????????? ????????????? ??..

    ??? ????? ????? ?? ??? ?? ??? ??? ????? ???? ??, ?? ???? ?? ???? ??? ?? ?? ????? ????? ?? ??????? ????????? ??? ???? ????
  8. ??? ?? ??? ??? ??????? ??????? ????? ?????? ??, ????? ????? ?????? ??? ?????????, ???, HTML, ?? ???? ???????? ??? ??????? ??? ??? ??????? ??? ????? ?????
  9. ????? ????? ?? ??? ??????? ???, ????? ????DataGrid, ?? ???? ??? ??? WebForm1 ?? ??? designer ?? ???????
  10. ????-????? ????WebForm1.aspx?? ????-????? ????, ?? ???? ???????? ?????? behind ????? ????? ????????? ?????
  11. ????? ????? ??? behind ?????, ??? ???????? ?????? ?? ????? ??? ??????:
    using System.Data.OleDb;
    using System.Data;
    					
  12. ????? ??? ??????? ??? ?? ????-????? ????, ?? ???? ????????????. WebForm1.aspx.cs, ??? ??? ??? ???????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();
    					
  13. ????? ???????????? ??,??? ??????????????? ??????? ?????? ???
  14. ????? ????????????? ??,?????????????? ????? ??? ??? ????????? ?? ???? ??? ???? behind-??? ??? ??? ??? ????? ???? ???
  15. ?????? Explorer ???, ???? ?????WebForm1.aspx?? ????-????? ????, ?? ???? ?????????? ??? ???????? ?? ??????

???????? ??? ?? ??????????

?? ???? ??? ??? ????? Excel ?????????? ??? ?????? ???? ?? ??? Microsoft Jet OLE DB ??????? ?? ????? ???? ??? ?? ??? ?? ?????????? ?? ?????? ???? ?? ??? ????? ??????? ???????? ?? ????? ???? ??:
// 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;";
				
?????????? ????? ???? ???, ?? ??? ??? ??????? Excel ????????????? ?? ??? ?? ??????? ??????? ???? ?????? ??? ???? ????????, ???? ?? ??? ???? ?????? ?? ???????????? ???????? ?? ??? ?? ?????? ???? ?? ??? ???????? ???

??? ??????? ???????? ?? ????? ???? ??Server.MapPath?????? ?? ??? ???? ???.. ?? ?????? ??? ?? ?? ???? ????? ?? ??????? Microsoft ??????? ?????????? ???????? (IIS) ?? ?? ?? ????? ?? ??? ????? ????? ?? ?? ???? ?? ?? ???? ??? ?????? ?? ???, ???????? Excel ?????????? ???????? ???, ?? ????? ExcelData.xls ??? ??? ?????????? ???, ?? ?????????? C:\Inetpub\Wwwroot ?? ????? ??? ?? ?? ????? ???????? Wwwroot ?????? ?? ???? ExcelCSTest ?? ??? ?? ExcelCSTest ?????? ?? ???? WebForm1.aspx ?? ??? ?? ??? ??????

?? ?????? ??? ????? ????? ?? ????? ?? ??????????? ??:
     C drive
        - Inetpub
            - Wwwroot (which contains ExcelData.xls)
                - ExcelCSTest (which contains WebForm1.aspx)
				
?? ??????? ?? ??? IIS ?? ??????????? ??:
     Web Root (which contains ExcelData.xls)
        - ExcelCSTest (which contains WebForm1.aspx)
				
?? ?????? ???,???????ExcelData.xls ????? ?? ??? ?? WebForm1.aspx ????? ?? ?? ".../ ExcelData.xls "? ".../ "???? ????? IIS ??????? ?? ?? ???? ??? ???? ?? ???? ?????, ???
Server.MapPath("../ExcelData.xls")
				
????? ???????? ???? ??:
C:\Inetpub\Wwwroot\ExcelData.xls
?? ?? ????? ???? ?? ??? ?????? ???? ??Server.MapPath. You can also hard code this information to a specific path, or you can use any method to supply the location of the Excel file on the hard disk.

??????

For additional information about how to access Excel files with ASP.NET, click the article numbers below to view the articles in the Microsoft Knowledge Base:
307029HOWTO: Transfer XML Data to Microsoft Excel 2002 by Using Visual C# .NET
306023HOW TO: Transfer Data to an Excel Workbook by Using Visual C# .NET
For additional information about using ADO.NET, click the article numbers below to view the articles in the Microsoft Knowledge Base:
306636HOW TO: Connect to a Database and Run a Command by Using ADO.NET and Visual C# .NET
314145HOW TO: Populate a DataSet Object from a Database by Using Visual C# .NET
307587HOW TO: Update a Database from a DataSet Object by Using Visual C# .NET
???:: The example companies, organizations, products, domain names, e-mail addresses, logos, people, places, and events depicted herein are fictitious. No association with any real company, organization, product, domain name, email address, logo, person, places, or events is intended or should be inferred.

???

???? ID: 306572 - ????? ???????: 29 ??????? 2010 - ??????: 4.0
???? ???? ???? ??:
  • Microsoft Excel 2002 Standard Edition
  • Microsoft Excel 2000 Standard Edition
  • Microsoft ASP.NET 1.1
  • Microsoft ASP.NET 1.0
  • Microsoft Visual C# .NET 2003 Standard Edition
  • Microsoft Visual C# .NET 2002 Standard Edition
  • Microsoft ADO.NET 1.1
??????: 
kbdatabase kbhowtomaster kbwebforms kbmt KB306572 KbMthi
???? ?????? ????????
??????????: ?? ???? ?? ???? ??????? ?? ????? ?? Microsoft ????-?????? ?????????? ?????? ?????? ???? ??? ??. Microsoft ???? ??? ????-???????? ?? ????-???????? ????? ?????? ?? ???? ???????? ???? ?? ???? ????? ????? ??? ?? ??? ?????? ?? ???? ???? ???? ??? ????? ??. ???????, ????-???????? ???? ????? ???? ???? ???? ???. ?????, ????????, ?????-???? ?? ??????? ?? ???????? ?? ???? ???, ???? ?? ??? ?????? ???? ???? ??? ????? ??? ?? ???? ??. Microsoft ??????? ??? ???? ?? ?????? ?? ??????????, ????????? ?? ??? ?????? ?? ???? ????? ?? ???? ???????? ?? ??? ???? ????? ?? ??? ????????? ???? ??. Microsoft ????-?????? ?????????? ?? ????? ?????? ?? ?? ??? ??.
?????????? ?? ??????? ????????? ??????? ??:306572

??????????? ???

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com