Adjust External Data While Importing

Một phần của tài liệu Sử dụng Excel thiết lập cơ sở dữ liệu (Trang 139 - 143)

Chapter 7: Automate Repetitive Database Tasks

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 that Excel is installed. ODBC drivers for Access, Excel, SQL Server, 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.

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 desired data columns by which to sort the incoming data. Also click the Ascending or Descending options as desired. Then click Next.

C H A P T E R 5 ■ C O N N E C T TO OT H E R D ATA B A S E S 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 Data ➤New Database Query.

3. In the Choose Data Source dialog box, with the Databases tab selected, click <New Data Source>, and click OK.

4. In the What Name Do You Want to Give Your Data Source, type Alaska Fish Counts Text File.

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

6. Click the Connect button.

7. Clear the Use Current Directory check box, and click the Select Directory button.

8. Browse 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 wordCreek 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. Insert a blank line beneath this line and type the following text: Alexandria Creek South,42,79,99.

4. Save the file, and then return to Excel.

5. With the six cities visible in the worksheet, click any single cell inside the data, and then click Data (Connections) ➤Refresh All (in Excel 2007) or click Data ➤Refresh Data (in Excel 2003). The text Alexandria Creek South,42,79,99 appears as a seventh matching data record.

6. Change the text in the worksheet from Alexandria Creek South to Alexander Creek North, and then click Data ➤(Connections) Refresh All (in Excel 2007) or click Data ➤ Refresh Data (in Excel 2003).

C H A P T E R 5 ■ C O N N E C T TO OT H E R D ATA B A S E S 123

The text changes back to Alexandria Creek South, because this is the text that is refreshed from the original connected text file.

Một phần của tài liệu Sử dụng Excel thiết lập cơ sở dữ liệu (Trang 139 - 143)

Tải bản đầy đủ (PDF)

(245 trang)