文件首頁
MySQL Connector/ODBC 開發人員指南
相關文件 下載本手冊
PDF (美式信紙) - 1.7Mb
PDF (A4) - 1.7Mb


MySQL Connector/ODBC 開發人員指南  /  Connector/ODBC 範例  /  將 Connector/ODBC 與 Microsoft Word 或 Excel 搭配使用

6.5 將 Connector/ODBC 與 Microsoft Word 或 Excel 搭配使用

您可以使用 Microsoft Word 和 Microsoft Excel 來存取來自 MySQL 資料庫的資訊,方法是使用 Connector/ODBC。在 Microsoft Word 中,此功能在匯入郵件合併的資料,或是要包含在報表中的表格和資料時最為實用。在 Microsoft Excel 中,您可以在您的 MySQL 伺服器上執行查詢,並將資料直接匯入 Excel 工作表,以一連串的列和欄呈現資料。

在這兩個應用程式中,資料都是使用 Microsoft Query 來存取和匯入到應用程式,這可讓您透過 ODBC 來源執行查詢。您可以使用 Microsoft Query 來建立要執行的 SQL 陳述式,選取表格、欄位、選取條件和排序順序。例如,若要將 World 測試資料庫中的表格資訊插入 Excel 試算表,使用第 5 章《設定 Connector/ODBC中所示的 DSN 範例

  1. 建立新的工作表。

  2. 資料 選單中,選擇 匯入外部資料,然後選取 新增資料庫查詢

  3. Microsoft Query 將會啟動。首先,您需要選取資料來源,方法是選取現有的資料來源名稱。

    圖 6.19 Microsoft Query 精靈:選擇資料來源對話方塊

    Shows the initial "Choose Data Source" dialog in the Microsoft Query wizard. The three available tabbed sections are "Databases", "Queries", and "OLAP Cubes". The "Databases" tab is selected that shows the following fields: "New Data Source", "dBASE Files", "Excel Files", "MS Access Database" and "Test World" where "Test World" is selected. The "Use the Query Wizard to create/edit queries" option's check box is selected.

  4. 查詢精靈 中,選擇要匯入的欄。透過 DSN 設定讓使用者可用的表格清單會顯示在左邊,將會新增到您查詢的欄會顯示在右邊。您選擇的欄相當於 SELECT 查詢的第一個區段中的欄。按一下 下一步 以繼續。

    圖 6.20 Microsoft Query 精靈:選擇欄

    Content is described in the surrounding text.

  5. 您可以使用 篩選資料 對話方塊篩選查詢中的列 (相當於 WHERE 子句)。按一下 下一步 以繼續。

    圖 6.21 Microsoft Query 精靈:篩選資料

    The left pane titled "Column to filter" lists available columns, and the right pane titled "Only include rows where" with six select boxes each with "And" and "Or" options for defining the filter's WHERE clause behavior.

  6. 選取資料的 (選用) 排序順序。這相當於在您的 SQL 查詢中使用 ORDER BY 子句。您可以選取最多三個欄位來排序查詢傳回的資訊。按一下 下一步 以繼續。

    圖 6.22 Microsoft Query 精靈:排序順序

    Shows sorting options "Sort by" and "Then by", each with "Ascending" and "Descending" options.

  7. 選取查詢的目的地。您可以選擇將資料傳回 Microsoft Excel,您可以在其中選擇要插入資料的工作表和儲存格;您可以繼續在 Microsoft Query 中檢視查詢和結果,您可以在其中編輯 SQL 查詢,並進一步篩選和排序傳回的資訊;或者您可以從查詢建立 OLAP 立方體,然後可以直接在 Microsoft Excel 中使用。按一下 完成

    圖 6.23 Microsoft Query 精靈:選取目的地

    Content is described in the surrounding text.

相同的程序可以用來將資料匯入 Word 文件,其中資料會以表格的形式插入。這可以用於郵件合併用途 (其中欄位資料是從 Word 表格中讀取),或者您想要在報表或其他文件中包含資料和報表。