打印

Delphi讀取Excel

Delphi讀取Excel

Create a simple spreadsheet viewer using ADO.
2 s! q  a2 e9 y  u, V& B9 w1. Start Delphi and create a new application.
& X+ B9 R: l# q  J( E& C% W# z+ M
2 T$ K, L: E7 K' F, @4 F% i. b2. Drop an ADODataSet onto the form.9 h0 G* A( n$ F" f4 x* M

& i; q& ?) q+ ]8 t3. Set the CommandType property to cmdTableDirect/ o' K- v  n3 Z

: g" m5 ?; j. N" NAn 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.
/ x" y( r' c4 X3 O; Z1 H
! }# v8 m0 w! Q/ s$ |/ x7 B  `8 l- M4. Set the ConnectionString property.# B% q, T1 V/ J& b8 a  {1 ]

. w$ E: f: g+ u% b! rThe 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.: J- ^, i2 }9 m, b
! X3 y  ?- J& I0 ^) }3 U* k
5. Set the CommandText property.# f3 ^" D5 K/ D& Q4 P

6 g( {! E' ?# o  E) C8 AThis 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.* ?8 K+ W1 e8 W" z3 A# L

6 L8 _' i7 a1 a6 l- g) b2 }% j  S6. Drop a Datasource component from the Data Access tab and a DBGrid from the Data Controls tab.
0 F6 C9 ]: C2 `5 c
' E% c2 Q% g; x: NNow 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.
2 E: J' ~' s4 s! j* V3 U2 r3 G! Y7 r
7. Set the Active property of the ADODataSet to True.% K) a4 \  X: _

. d8 J: x. J6 _  J7 W7 AWe can now test the query in design mode by activating the ADODataSet. The data from the spreadsheet will be displayed in the grid.
# }* O' C$ o% \; {% i4 G8 B# {6 i, K8 N( v& H( w7 J& `. o# L
--
: a0 t) t6 x: M/ Q' H 隨手弄各很爛的範例 > <) m) |6 p* q7 m/ v* ]
按照上面還TRY不出來的話可以參考看看 @@?
附件: 您所在的用戶組無法下載或查看附件

TOP

發新話題