Bỏ qua để tới nội dung chính
Đăng nhập với Microsoft
Đăng nhập hoặc tạo một tài khoản.
Xin chào,
Chọn một tài khoản khác.
Bạn có nhiều tài khoản
Chọn tài khoản bạn muốn đăng nhập.

To import and export XML data in Excel, an XML Map that associates XML elements with data in cells to get the results you want will be useful. To create one, you need to have an XML schema file (.xsd) and an XML data file (.xml). After creating the XML Map, you can map XML elements the way you want.

Mẹo: You'll find more information about using XML with Excel in this overview of XML in Excel.

Locate or create XML schema and XML data files

If another database or application created an XML schema or XML data file, you might already have them available. For example, you might have a line-of-business application that exports data into these XML file formats, a commercial web site or web service that supplies these XML files, or a custom application developed by your IT department that automatically creates these XML files.

If you don’t have the necessary XML files, you can create them by saving the data you want to use as a text file. You can then use both Access and Excel to convert that text file to the XML files you need. Here’s how:

Truy nhập

  1. Import the text file you want to convert and link it to a new table.

    1. Bấm Tệp > Mở.

    2. In the Open dialog box, select and open the database in which you want to create a new table.

    3. Click External Data > Text File, and follow the instructions for each step, making sure that you link the table to the text file.

      Access tạo bảng mới và hiển thị trong Ngăn Dẫn hướng.

  2. Xuất dữ liệu từ bảng được nối kết này đến tệp dữ liệu XML và tệp lược đồ XML.

    1. Click External Data > XML File (in the Export group).

    2. In the Export - XML File dialog box, specify the file name and format, and click OK.

  3. Thoát khỏi Access.

Excel

  1. Create an XML Map based on the XML schema file you exported from Access.

    If the Multiple Roots dialog box appears, make sure you choose dataroot so you can create an XML table.

  2. Create an XML table by mapping the dataroot element. See Map XML elements for more information.

  3. Import the XML file you exported from Access.

Lưu ý: 

  • There are several types of XML schema element constructs Excel doesn't support. The following XML schema element constructs can't be imported into Excel:

  • <mọi>    Yếu tố này cho phép bạn bao gồm các yếu tố không được khai báo bằng lược đồ.

  • <bất kỳ Phân phối>    Yếu tố này cho phép bạn bao gồm các thuộc tính không được khai báo bằng lược đồ.

  • Recursive structures    A common example of a recursive structure is a hierarchy of employees and managers in which the same XML elements are nested several levels. Excel doesn't support recursive structures more than one level deep.

  • Phần tử trừu tượng    Những phần tử này được khai báo trong lược đồ nhưng không bao giờ được dùng như phần tử. Phần tử trừu tượng tùy thuộc vào các phần tử khác dùng để thay thế cho phần tử trừu tượng.

  • Nhóm thay thế    Nhóm này cho phép một phần tử có thể được hoán đổi bất kỳ khi nào phần tử kia được tham chiếu. Phần tử sẽ cho biết nó là thành viên của nhóm thay thế của phần tử kia thông qua thuộc tính <substitutionGroup>.

  • Nội dung hỗn hợp    Nội dung này được khai báo bằng cách dùng hỗn hợp="true" trên định nghĩa kiểu phức hợp. Excel không hỗ trợ nội dung đơn giản của kiểu phức hợp nhưng sẽ hỗ trợ thẻ và thuộc tính con được định nghĩa trong kiểu phức hợp.

Use sample XML schema and XML data files

The following sample data has basic XML elements and structures you can use to test XML mapping if you don't have XML files or text files to create the XML files. Here’s how you can save this sample data to files on your computer:

  1. Select the sample text of the file you want to copy, and press Ctrl+C.

  2. Start Notepad, and press Ctrl+V to paste the sample text.

  3. Press Ctrl+S to save the file with the file name and extension of the sample data you copied.

  4. Press Ctrl+N in Notepad and repeat step 1-3 to create a file for the second sample text.

  5. Thoát khỏi Notepad.

Dữ liệu XML mẫu (Expenses.xml)

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<Root>
  <EmployeeInfo>
    <Name>Jane Winston</Name>
    <Date>2001-01-01</Date>
    <Code>0001</Code>
  </EmployeeInfo>
  <ExpenseItem>
    <Date>2001-01-01</Date>
    <Description>Airfare</Description>
    <Amount>500.34</Amount>
  </ExpenseItem>
  <ExpenseItem>
    <Date>2001-01-01</Date>
    <Description>Hotel</Description>
    <Amount>200</Amount>
  </ExpenseItem>
  <ExpenseItem>
    <Date>2001-01-01</Date>
    <Description>Taxi Fare</Description>
    <Amount>100.00</Amount>
  </ExpenseItem>
  <ExpenseItem>
    <Date>2001-01-01</Date>
    <Description>Long Distance Phone Charges</Description>
    <Amount>57.89</Amount>
  </ExpenseItem>
  <ExpenseItem>
    <Date>2001-01-01</Date>
    <Description>Food</Description>
    <Amount>82.19</Amount>
  </ExpenseItem>
  <ExpenseItem>
    <Date>2001-01-02</Date>
    <Description>Food</Description>
    <Amount>17.89</Amount>
  </ExpenseItem>
  <ExpenseItem>
    <Date>2001-01-02</Date>
    <Description>Personal Items</Description>
    <Amount>32.54</Amount>
  </ExpenseItem>
  <ExpenseItem>
    <Date>2001-01-03</Date>
    <Description>Taxi Fare</Description>
    <Amount>75.00</Amount>
  </ExpenseItem>
  <ExpenseItem>
    <Date>2001-01-03</Date>
    <Description>Food</Description>
    <Amount>36.45</Amount>
  </ExpenseItem>
  <ExpenseItem>
    <Date>2001-01-03</Date>
    <Description>New Suit</Description>
    <Amount>750.00</Amount>
  </ExpenseItem>
</Root>

Lược đồ XML mẫu (Expenses.xsd)

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="Root">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element minOccurs="0" maxOccurs="1" name="EmployeeInfo">
          <xsd:complexType>
            <xsd:all>
              <xsd:element minOccurs="0" maxOccurs="1" name="Name" />
              <xsd:element minOccurs="0" maxOccurs="1" name="Date" />
              <xsd:element minOccurs="0" maxOccurs="1" name="Code" />
            </xsd:all>
          </xsd:complexType>
        </xsd:element>
        <xsd:element minOccurs="0" maxOccurs="unbounded" name="ExpenseItem">
          <xsd:complexType>
            <xsd:sequence>
              <xsd:element name="Date" type="xsd:date"/>
              <xsd:element name="Description" type="xsd:string"/>
              <xsd:element name="Amount" type="xsd:decimal" />
            </xsd:sequence>
          </xsd:complexType>
        </xsd:element>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

Tạo ánh xạ XML

You create an XML Map by adding an XML schema to a workbook. The schema can be copied from an XML schema file (.xsd), or Excel can try to infer one from an XML data file (.xml).

  1. Click Developer > Source.

    Lệnh XML trên tab Nhà phát triển

    Nếu bạn không thấy tab Nhà phát triển, hãy xem Hiện tab Nhà phát triển.

  2. In the XML Source task pane, click XML Maps, and then click Add.

  3. Trong danh sách Tìm trong, hãy bấm vị trí ổ đĩa, thư mục hoặc Internet có chứa tệp bạn muốn mở.

  4. Bấm vào tệp rồi bấm Mở.

    • For an XML schema file, XML will create an XML Map based on the XML schema. If the Multiple Roots dialog box appears, choose one of the root nodes defined in the XML schema file.

    • For an XML data file, Excel will try to infer the XML schema from the XML data, and then creates an XML Map.

  5. Bấm OK.

    The XML Map appears in the XML Source task pane.

Ánh xạ các phần tử XML

Bạn có thể ánh xạ phần tử XML vào ô ánh xạ đơn và lặp lại cho các ô trong bảng XML sao cho bạn có thể tạo mối quan hệ giữa ô này và phần tử dữ liệu XML trong lược đồ XML.

  1. Click Developer > Source.

    Lệnh XML trên tab Nhà phát triển

    Nếu bạn không thấy tab Nhà phát triển, hãy xem Hiện tab Nhà phát triển.

  2. In the XML Source task pane, select the elements you want to map.

    To select nonadjacent elements, click one element, and then hold down Ctrl and click each element you want to map.

  3. Để ánh xạ các phần tử, hãy thực hiện các thao tác sau:

    1. Right-click the selected elements, and click Map element.

    2. In the Map XML elements dialog box, select a cell and click OK.

      Mẹo: Bạn cũng có thể kéo phần tử đã chọn tới vị trí trang tính mà bạn muốn chúng xuất hiện.

      Từng phần tử sẽ được tô đậm và xuất hiện ở ngăn tác vụ Nguồn XML để cho biết phần tử nào được ánh xạ.

  4. Quyết định cách xử lý đối với nhãn và đầu đề cột:

    • Khi kéo phần tử XML không lặp vào trang tính nhằm tạo ô ánh xạ đơn, thẻ thông tin cùng ba câu lệnh sẽ hiển thị, bạn có thể dùng chúng để kiểm soát sự bố trí của đầu đề hoặc nhãn:

      Dữ liệu của Tôi Đã Có Đầu đề    Bấm vào tùy chọn này để bỏ qua đầu đề phần tử XML, vì ô này đã có đầu đề rồi (ở bên trái dữ liệu hoặc ở trên dữ liệu).

      Đặt Đầu đề XML ở bên Trái    Bấm vào tùy chọn này để dùng đầu đề phần tử XML như là nhãn của ô (ở bên trái dữ liệu).

      Đặt Đầu đề XML ở Trên    Bấm vào tùy chọn này để dùng đầu đề phần tử XML như là đầu đề của ô (ở trên dữ liệu).

    • Khi kéo phần tử XML lặp vào trang tính nhằm tạo ô lặp trong bảng XML, tên phần tử XML được tự động dùng như đầu đề cột cho bảng. Tuy nhiên, bạn có thể thay đổi đầu đề cột thành bất kỳ đầu đề nào bạn muốn bằng cách sửa ô đầu cột.

      Trong ngăn tác vụ Nguồn XML, bạn có thể bấm vào Tùy chọn để kiểm soát hơn nữa hành vi bảng XML:

      Tự động Phối các Phần tử khi Ánh xạ    Khi chọn hộp kiểm này, bảng XML sẽ tự động mở rộng khi bạn kéo phần tử vào ô liền kề bảng XML.

      Dữ liệu của Tôi có Đầu đề    Khi chọn hộp kiểm này, dữ liệu hiện có có thể dùng như là đầu đề cột khi bạn ánh xạ các phần tử lặp vào trang tính của mình.

      Lưu ý: 

      • If all XML commands are dimmed, and you can't map XML elements to any cells, the workbook might be shared. Click Review > Share Workbook to verify that and to remove it from shared use as needed.

        If you want to map XML elements in a workbook you want to share, map the XML elements to the cells you want, import the XML data, remove all of the XML maps, and then share the workbook.

      • If you can't copy an XML table that contains data to another workbook, the XML table might have an associated XML Map that defines the data structure. This XML Map is stored in the workbook, but when you copy the XML table to a new workbook, the XML Map isn't automatically included. Instead of copying the XML table, Excel creates an Excel table that contains the same data. If you want the new table to be an XML table, do the following:

        1. Add an XML Map to the new workbook by using the .xml or .xsd file you used to create the original XML Map. You should save these files if you want to add XML Maps to other workbooks.

        2. Ánh xạ các phần tử XML vào bảng để tạo bảng XML.

      • When you map a repeating XML element to a merged cell, Excel unmerges the cell. This is expected behavior, because repeating elements are designed to work with unmerged cells only.

        You can map single, nonrepeating XML elements to a merged cell, but mapping a repeating XML element (or an element that contains a repeating element) to a merged cell isn't allowed. The cell will be unmerged, and the element will be mapped to the cell where the pointer is located.

Mẹo: 

  • Bạn có thể bỏ ánh xạ các thành phần XML mà bạn không muốn sử dụng hoặc để ngăn nội dung của ô bị ghi đè khi bạn nhập dữ liệu XML. For example, you could temporarily unmap an XML element from a single cell or repeating cells that have formulas you don't want to overwrite when you import an XML file. Khi quá trình nhập hoàn tất, bạn có thể ánh xạ lại thành phần XML tới các ô công thức, vì vậy bạn có thể xuất kết quả của công thức sang tệp dữ liệu XML.

  • To unmap XML elements, right-click their name in the XML Source task pane, and click Remove element.

Hiện tab Nhà phát triển

Nếu bạn không thấy tab Nhà phát triển, hãy thực hiện các thao tác sau để hiển thị tab:

  • Trong Excel 2010 và các phiên bản mới hơn:

    1. Bấm Tệp > Tùy chọn.

    2. Bấm vào thể loại Tùy chỉnh Ruy-băng.

    3. Under Main Tabs, check the Developer box, and click OK.

  • Trong Excel 2007:

    1. Bấm vào Nút Microsoft Office để Ảnh nút>chọn Excel.

    2. Click the Popular category.

    3. Under Top options for working with Excel, check the Show Developer tab in the Ribbon box, and click OK.

Xem thêm

Xóa thông tin ánh xạ XML khỏi sổ làm việc

Chắp thêm hoặc ghi đè dữ liệu XML được ánh xạ

Tổng quan về XML trong Excel

Nhập dữ liệu XML

Xuất dữ liệu XML

Bạn cần thêm trợ giúp?

Bạn muốn xem các tùy chọn khác?

Khám phá các lợi ích của gói đăng ký, xem qua các khóa đào tạo, tìm hiểu cách bảo mật thiết bị của bạn và hơn thế nữa.

Cộng đồng giúp bạn đặt và trả lời các câu hỏi, cung cấp phản hồi và lắng nghe ý kiến từ các chuyên gia có kiến thức phong phú.

Thông tin này có hữu ích không?

Bạn hài lòng đến đâu với chất lượng dịch thuật?
Điều gì ảnh hưởng đến trải nghiệm của bạn?
Khi nhấn gửi, phản hồi của bạn sẽ được sử dụng để cải thiện các sản phẩm và dịch vụ của Microsoft. Người quản trị CNTT của bạn sẽ có thể thu thập dữ liệu này. Điều khoản về quyền riêng tư.

Cảm ơn phản hồi của bạn!

×