[程式] Delphi 讀取 Excel

剛好要用到  , 就順手查一下資料 …

沒想到透過 ADO 真是令人驚訝的簡單 XDD

基本上就是開各 ADODATASET 然後再開各 datasource

再來要給 dbgrid 或是其他物件使用就都跟 ADOQuery 還是  Table  操作差不多了

要轉入其他 DataBase 基本上也很簡單了 …

下面是原廠提供德說明 🙂 

Delphi讀取Excel

Create a simple spreadsheet viewer using ADO.
1. Start Delphi and create a new application.

2. Drop an ADODataSet onto the form.

3. Set the CommandType property to cmdTableDirect

An ADODataSet is similar to the BDEDataSet used to access database tables. With ADO, you describe the data source and then use data-aware controls to display the data in your application. The first property to set is the CommandType. This property tells the ADO engine what type of command you are going to be calling.

4. Set the ConnectionString property.

The ConnectionString property tells ADO how to connect to the datasource. Click the ellipse to display the ConnectionString dialog. We are going to build the string ourselves so select Use Connection String and click the Build button to display the Data Link Properties dialog. The first choice to make is what data provider are we going to use to get the data. To access Excel we use the Microsoft Jet OLEDB 4.0 Provider, select it and click Next. The next piece is what are we going to connect to. In this case we want to access an Excel file. Use the Browse button and find an Excel file on your system or use Forecast.xls provided in the ADOExcel.zip file. Before we can test the connection we need to enter an Extended Property to the connection string. Click OK to go back the ConnectionString dialog and type 'Extended Properties=Excel 8.0;' without the quotes at the end of the connection string (remember, all ConnectionString entries must be separated by semi-colons. This tells ADO that we need Excel support and use the type library associated with Excel 8.0. Go ahead and click Build again and then Test the connection. If you get Test Connection Succeeded, you did everything right. Close the ConnectionString dialog. The last property to set is the CommandText property.

5. Set the CommandText property.

This property now becomes a combobox that allows you to select the tables defined in the datasource. Choose the name of the spreadsheet with a $ appended to it.

6. Drop a Datasource component from the Data Access tab and a DBGrid from the Data Controls tab.

Now we want to hook up the ADODataset to a grid for display. Connect the Grid to the datasource by setting the DataSource property to DataSource1 and connect the DataSource to the ADODataset by setting the DataSet property to ADODataSet1.

7. Set the Active property of the ADODataSet to True.

We can now test the query in design mode by activating the ADODataSet. The data from the spreadsheet will be displayed in the grid.


隨手弄各很爛的範例 > <
按照上面還TRY不出來的話可以參考看看 @@?

附件: [delphi 讀取 excel] Excel.zip (2007-8-7 10:54 PM, 415.75 K)
該附件被下載次數 0

分類未分類

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料