Tải bản đầy đủ (.pdf) (20 trang)

Connect to Other Databases

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (345.53 KB, 20 trang )

Connect to Other Databases
Y
ou can use Excel to connect to any external database for which you have an accompanying
ODBC driver or OLE DB provider (software translation programs that allow Excel to connect to
external data). Once you have connected to an external database, you can use most if not all of
Excel’s built-in features for viewing, finding, refreshing, and analyzing the data associated with
that database.
Some of the ODBC drivers and OLE DB providers that ship with Excel include the following:
• Microsoft Office Excel
• Microsoft Office Access
• Microsoft SQL Server
• Microsoft Windows SharePoint lists and document libraries
• Microsoft Business Solutions
• Microsoft Visual FoxPro
• dBASE
• Oracle
• Paradox
• Text file databases
This chapter describes how to establish connections from Excel to other external Excel
databases as w
ell as Access databases
, SQL Server databases, and OLAP databases hosted in
SQL Server Analysis Services. For information on how to connect to other database types,
contact the specific database’s manufacturer.
5.1 Create a Reusable Connection to External Data
If you find yourself connecting repeatedly from several Excel workbooks to data in a specific
database, you can create a reusable connection to that data. Over time, this approach is more
efficient than specifying the same connection information each time you want to connect to
the same data.
117
CHAPTER 5


■ ■ ■
7516Ch05.qxp 1/5/07 2:59 PM Page 117
Quick Start
To create a reusable connection to external data, do the following:
1. In Excel 2007 click Data

Get External Data

Existing Connections, and then click
Browse for More. In Excel 2003, click Data

Import External Data

Import Data.
2. Click New Source.
3. Follow the onscreen directions to import the data into the current Excel workbook and
create the connection file during the import operation. This connection file can be
reused during subsequent import operations.
How To
To create a reusable connection, do the following:
1. In Excel 2007 click Data

Get External Data

Existing Connections, and then click
Browse for More. In Excel 2003, click Data

Import External Data

Import Data.

2. Click New Source.
3. In the What Kind of Data Source Do You Want to Connect To list, make one of these
selections:
• If you select Microsoft SQL Server, Microsoft SQL Server OLAP Services, or
Microsoft Data Access – OLE DB Provider for Oracle (in Excel 2007) or Oracle (in
Excel 2003), Excel will ask you to provide the server name and logon credentials.
Excel will then ask you for additional connection information based on the
selected database type.
• If you select Microsoft Business Solutions, Excel will ask you to provide the computer
name or URL to where the accompanying
data retrieval service (a Web service
installed on Windows SharePoint Services for connecting to and retrieving data) is
located. Excel will then ask you for additional connection information based on the
selected computer name or URL.
• If you select ODBC DSN, Excel will ask you to select an available ODBC driver type
or data source name such as dBASE files, Excel files, or MS Access Database. Once
you select an available ODBC driver type or data source name, the Data Connec-
tion
W
izard will ask you for additional connection information depending on the
selected ODBC driver type or data source name.
• If you select Other/Advanced, Excel will ask you to select an available OLE DB
provider. Once you select an available OLE DB provider on the Data Link Proper-
ties dialog box’s Provider tab, click Next. The Data Link Properties dialog box will
ask you for additional connection information on the Connection tab depending
on the selected OLE DB provider.
CHAPTER 5

CONNECT TO OTHER DATABASES118
7516Ch05.qxp 1/5/07 2:59 PM Page 118

4. After you supply Excel with the requested connection details, click OK or Next depending
o
n the displayed dialog box. The Save Data Connection File and Finish page appears.
5. In the File Name box, type a name for the connection file. Click Browse to save the
connection file to a specified location.
6. Depending on the selected data source, Excel may ask for additional connection infor-
mation, or Excel may ask you to import the associated data directly into the current
workbook.
After you finish importing the data, the connection information is saved as a reusable
connection with the file extension .odc.
Once you create a reusable connection, reusing the connection file is straightforward:
1. In Excel 2007, click Data

Get External Data

Existing Connections. In Excel 2003,
click Data

Import External Data

Import Data.
2. Browse to and select the connection file, and click Open.
3. Follow the onscreen directions to finish importing the external data into the current
Excel workbook.
Try It
In this exercise, you will practice creating a reusable connection to a Microsoft Office Access
database file. You will use the connection file to connect to the same Access database from two
separate Excel worksheets.
Create the reusable connection file:
1. Start Excel.

2. With a new blank worksheet visible, in Excel 2007, click Data

Get External Data

Existing Connections, and then click Browse for More. In Excel 2003, click Data

Import External Data

Import Data.
3. Click New Source.
4. In the What Kind of Data Source Do You Want to Connect To, select ODBC DSN, and
click N
ext.
5. In the ODBC Data Sources list, select MS Access Database, and click Next.
6. Browse to and select the ExcelDB_Ch05_01_04.mdb file, and click OK.
7. Clear the Connect to a Specific Table check box, and click Next.
8. In the Description box, type Alaska Fish Counts and World Populations.
9. I
n the S
earch Keywords box, type
Alaska fish counts w
or
ld populations
, and click F
inish.
10. Click Cancel.
CHAPTER 5

CONNECT TO OTHER DATABASES 119
7516Ch05.qxp 1/5/07 2:59 PM Page 119

Use the connection file to connect to the same Access database from two separate Excel
w
orksheets:
1. With the Sheet1 worksheet tab of the new blank worksheet selected, in Excel 2007,
click Data

Get External Data

Existing Connections. In Excel 2003, click Data

Import External Data

Import Data.
2. Select the ExcelDB_Ch05_01_04 connection file, and click Open.
3. In the Select Table dialog box, select AlaskaFishCounts, and click OK.
4. With the Existing worksheet option selected, click OK.
5. Click the Sheet2 worksheet tab, and in Excel 2007, click Data

Get External Data

Existing Connections. In Excel 2003, click Data

Import External Data

Import Data.
6. Select the ExcelDB_Ch05_01_04 connection file, and click Open.

Note
In Excel 2007, make sure to select the ExcelDB_Ch05_01_04 connection file listed in the Connec-
tion Files on This Computer section. If you select the ExcelDB_Ch05_01_04 connection file listed in the

Connections in This Workbook section, you will not be able to complete the next step.
7. In the Select Table dialog box, select WorldPopulations, and click OK.
8. With the Existing worksheet option selected, click OK.
5.2 Adjust External Data While Importing
Before you connect to data in an external database, you may want to adjust the amount of
data that Excel imports from that database during the import operation, either by importing
specific data rows or data columns, by presorting the data, or by joining separate data tables
together. To do this, you use a tool included with Excel named Microsoft Query.

Note
To use Microsoft Query in conjunction with an external database, you must first install that data-
base’
s ODBC driver on the same computer tha
t Excel is installed. ODBC drivers for Access, Excel, SQL
Ser
ver
,
Microsoft
Visual FoxPro, dBASE, Oracle, Paradox, and text file databases are included with Excel.
For ODBC drivers for other database types, contact that database’s manufacturer.
CHAPTER 5

CONNECT TO OTHER DATABASES120
7516Ch05.qxp 1/5/07 2:59 PM Page 120
Quick Start
To use Microsoft Query to adjust external data while importing, do the following:
1. In Excel 2007, click Data

Get External Data


From Other Sources

From Microsoft
Query. In Excel 2003, click Data

Import External Data

New Database Query.
2. In the Choose Data Source dialog box, click the Databases tab, and click one of the
available database types.
3. With the Use the Query Wizard to Create/Edit Queries check box selected, click OK.
4. Depending on the database type, complete the onscreen directions to finish importing
the data into the current Excel workbook, start Microsoft Query and perform more
advanced data filtering operations before importing the data into the current Excel
workbook, or create an offline cube file from the imported data.
How To
To use Microsoft Query to adjust external data while importing it into the current Excel work-
book, do the following:
1. In Excel 2007, click Data

Get External Data

From Other Sources

From Microsoft
Query. In Excel 2003, click Data

Import External Data

New Database Query.

2. In the Choose Data Source dialog box, click the Databases tab, and click one of the
available database types.
3. With the Use the Query Wizard to Create/Edit Queries check box selected, click OK.
4. Depending on the database type, complete the onscreen directions to specify the data-
base file or database connection details.
5. Select the newly created database connection entry, and click OK.
6. In the Query Wizard – Choose Columns page, in the Available Tables and Columns list,
select the desired data columns to include in the incoming data, and click the arrow
key to move the selected data columns to the Columns in Your Query list. Then click
Next.
7. In the Query Wizard – Filter Data page, in the Column to Filter list, select any desired
data columns by which to filter the incoming data. For each selected data column, in
the Only Include Rows Where area, specify the filter criteria. Then click Next.
8. In the Query Wizard – Sort Order page, in the Sort By and Then By lists, select any
desir
ed data columns by which to sort the incoming data. Also click the Ascending or
D
escending options as desir
ed.
Then click Next.
CHAPTER 5

CONNECT TO OTHER DATABASES 121
7516Ch05.qxp 1/5/07 2:59 PM Page 121
9. In the Query Wizard – Finish page, do one of these options:
• Select the Return Data to Microsoft Office Excel option to import the data into the
current Excel workbook.
• Select the View Data or Edit Query in Microsoft Query option to start Microsoft
Query and perform more advanced data filtering operations before importing the
data into the current Excel workbook.

• In Excel 2003 only, select the Create an OLAP Cube From This Query option to start
the OLAP Cube Wizard and create an offline cube file from the imported data.
Excel uses the offline cube file’s contents to display the imported data.
10. If desired, click the Save Query button to save the data filtering details that you specified
in the Query Wizard to a query file with the file extension .dqy. Provide the query file
name and location in the Save As dialog box, and then click Save.

Note
.dqy files can be reused on subsequent data import operations. In Excel 2007, click Data

Get
External Data

From Other Sources

From Microsoft Query, and then click the Queries tab. In Excel 2003,
click Data

Import External Data

New Database Query, and then click the Queries tab.
11. Click Finish, and complete the onscreen directions to finish importing the data into
the current Excel workbook; start Microsoft Query and perform more advanced data
filtering operations before importing the data into the current Excel workbook; or in
Excel 2003 create an offline cube file from the imported data.
Try It
In this exercise, you will practice using Microsoft Query to filter data in a text file as you are
importing it into the current Excel workbook. You will also practice changing the data in the
text file and refreshing the changed data in the Excel workbook.
Use Microsoft Query to filter data in the text file as you are importing it:

1. Start Excel.
2. With a new blank worksheet visible, in Excel 2007, click Data

Get External Data

From Other Sources

From Microsoft Query. In Excel 2003, click Data

Import Exter-
nal D
ata

N
ew Database Q
uery.
3. I
n the Choose Data Source dialog box, with the Databases tab selected, click <New
Data Source>, and click OK.
4. I
n the
What Name Do You Want to Give Your Data Source, type
Alaska F
ish C
ounts Text
F
ile
.
5. I
n the S

elect a D
river for the Type of Database You Want to Access list, select Microsoft
Text Driver (*.txt; *.csv).
CHAPTER 5

CONNECT TO OTHER DATABASES122
7516Ch05.qxp 1/5/07 2:59 PM Page 122
6. Click the Connect button.
7. Clear the Use Current Directory check box, and click the Select Directory button.
8
.
B
rowse to the folder containing the ExcelDB_Ch05_02.csv file, and click OK.
9. Click OK three more times in Excel 2007, and two more times in Excel 2003.
10. In the Choose Data Source dialog box, with the Databases tab selected, click Alaska
Fish Counts Text File, and click OK.
Instruct the Query Wizard to only display rows where the city name contains the word
Creek as follows:
1. In the Query Wizard – Choose Columns page, in the Available Tables and Columns list,
select ExcelDB_Ch5_02.csv, click the right arrow (>) button, and click Next.
2. In the Query Wizard – Filter Data page, in the Column to Filter list, click City.
3. In the Only Include Rows Where list, in the top left list, select Contains.
4. In the top right box, type Creek, then click Next.
5. Since the results will already be sorted by city name, in the Query Wizard – Sort Order
page, click Next.
6. In the Query Wizard – Finish page, click the Save Query button.
7. In the File Name box, type Alaska Fish Counts Text File Query, and click Save.
8. With the Return Data to Microsoft Excel option selected, click Finish.
9. With the Existing Worksheet option selected, click OK. Excel displays data records for
six cities with the word

Creek in their city names.
Change the data in the text file and refresh the changed data in the Excel workbook:
1. Using Windows Notepad, open the ExcelDB_Ch05_02.csv file.
2. Find the following line of text in the file: Alexander Creek,1,0,0.
3. I
nsert a blank line beneath this line and type the following text:
Alexandr
ia Creek
South,42,79,99
.
4. Save the file, and then return to Excel.
5. W
ith the six cities visible in the worksheet, click any single cell inside the data, and
then click Data (C
onnections)

R
efr
esh All (in E
x
cel 2007) or click D
ata

R
efr
esh
Data (in Excel 2003). The text
Alexandria Creek South,42,79,99 appears as a seventh
matching data r
ecord.

6. Change the text in the worksheet from Alexandria Creek South to Alexander Creek
N
or
th, and then click D
ata

(C
onnections) R
efr
esh All (in Excel 2007) or click Data

Refresh Data (in Excel 2003).
CHAPTER 5

CONNECT TO OTHER DATABASES 123
7516Ch05.qxp 1/5/07 2:59 PM Page 123
The text changes back to Alexandria Creek South, because this is the text that is refreshed
f
rom the original connected text file.
5.3 Connect to Excel Data in Other Workbooks
In most circumstances, you will want to simply open an Excel workbook directly rather than
import the workbook’s data into another workbook. But there are some scenarios in which
you might want to import a portion of an external workbook’s data. For example, you may not
want to risk modifying the data in the external workbook, so you decide to view, modify, and
analyze a copy of the data from another workbook. You can refresh the copied data with the
data values in the other workbook if you decide you ever want to start over with the original
data values. Similarly, perhaps the data in the external workbook is updated frequently. You
don’t want to interrupt the data updating process, so you decide to view and analyze the
constantly updating data from another workbook. You can refresh the data from the other
workbook if you need to view and analyze the most up-to-date data.

Quick Start
To connect to Excel data in a workbook other than the current workbook, do the following:
1. In Excel 2007, click Data

Get External Data

Existing Connections, and then click
Browse for More. In Excel 2003, click Data

Import External Data

Import Data.
2. With the Files of Type list displaying All Files, browse to and select the desired connec-
tion file or Excel workbook file, and click Open.
3. Complete the onscreen directions to finish importing the workbook’s data into the
current workbook.
How To
To connect to Excel data in a workbook other than the current workbook, do the following:
1. In Excel 2007, click Data

Get External Data

Existing Connections, and then click
Browse for More. In Excel 2003, click Data

Import External Data

Import Data.
2. With the Files of Type list displaying All Files, browse to and select the desired connec-
tion file or E

x
cel wor
kbook file, and click Open.
3. In the Select Table dialog box, click the desired worksheet in the workbook, and click OK.
4. Go to the Import Data dialog box and do the following:
a. In Excel 2007 only, click the Table, PivotTable Report, PivotChart and PivotTable
Report, or Only Create Connection option to view the data as a data table, a Pivot-
T
able
, or a Piv
otChart and PivotTable, or to create a reusable connection file,
respectively.
b. Click the Properties button to modify the default data refresh, formatting, and lay-
out options.
CHAPTER 5

CONNECT TO OTHER DATABASES124
7516Ch05.qxp 1/5/07 2:59 PM Page 124

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×