Để nhập và xuất dữ liệu XML trong Excel, hãy sử dụng ánh xạ XML liên kết các thành phần XML với dữ liệu trong ô. Sự liên kết này giúp bạn có được kết quả mong muốn. To create an XML map, you need an XML schema file (.xsd) and an XML data file (.xml). Sau khi tạo ánh xạ XML, bạn có thể ánh xạ các phần tử XML theo cách bạn muốn.
Mẹo
Để biết thêm thông tin về cách sử dụng XML với Excel, hãy xem tổng quan về XML trong Excel.
- Locate or create XML schema and XML data files
- Use sample XML schema and XML data files
- Tạo ánh xạ XML
- Ánh xạ các phần tử XML
Lưu ý
Excel dành cho máy tính chạy Windows là phiên bản duy nhất cung cấp các tính năng ánh xạ XML. Các công cụ này không sẵn dùng trong Excel for Mac hoặc Excel cho web.
Ánh xạ XML là một tính năng kế thừa. Đối với nhiều kịch bản, hãy sử dụng Power Query trong Excel để nhập và chuyển đổi dữ liệu XML, đặc biệt là đối với các cấu trúc XML phức tạp hay lồng nhau.
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. Sau đây là cách thực hiện:
Access
Import the text file you want to convert and link it to a new table.
- Chọn Mở Tệp>.
- In the Open dialog box, select and open the database in which you want to create a new table.
- Chọn External Data>Text File, rồi làm theo hướng dẫn cho từng bước, đảm bảo rằng bạn liên kết bảng với tệp văn bản.
Access tạo bảng mới và hiển thị trong Ngăn Dẫn hướng.
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.
- Chọn Dữ liệu> NgoàiThêm>Tệp XML (trong nhóm Xuất).
- In the Export - XML File dialog box, specify the file name and format, and select OK.
Thoát khỏi Access.
Excel
-
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. - Create an XML table by mapping the dataroot element. See Map XML elements for more information.
- Import the XML file you exported from Access.
Ngoài ra, bạn có thể sử dụng Power Query để nhập trực tiếp dữ liệu XML mà không cần tạo tệp sơ đồ XML.
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:
- <bất kỳ Yếu> tố nào 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 đồ.
- <anyAttribute> 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 Ví dụ phổ biến của cấu trúc đệ quy là hệ thống phân cấp nhân viên và nhà quản lý, trong đó các phần tử XML giống nhau được lồng vào nhau ở vài cấp độ. 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. Một phần tử biểu thị nó là một thành viên của nhóm thay thế của một phần tử khác 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 includes 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:
- Select the sample text of the file you want to copy, and press Ctrl+C.
- Start Notepad, and press Ctrl+V to paste the sample text.
- Press Ctrl+S to save the file with the file name and extension of the sample data you copied.
- Nhấn Ctrl+N trong Notepad và lặp lại các bước 1-3 để tạo tệp cho văn bản mẫu thứ hai.
- 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
Create an XML map by adding an XML schema to a workbook. Bạn có thể sao chép sơ đồ từ tệp sơ đồ XML (.xsd), hoặc Excel có thể thử phỏng đoán sơ đồ từ tệp dữ liệu XML (.xml).
ChọnNguồn 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.In the XML Source task pane, select XML Maps, and then select Add.
Trong danh sách Tìm trong , hãy chọn ổ đĩa, thư mục hoặc vị trí Internet có chứa tệp bạn muốn mở.
Chọn tệp rồi chọn Mở.
- Đối với tệp sơ đồ XML, Excel sẽ tạo ra một ánh xạ XML dựa trên sơ đồ XML. If the Multiple Roots dialog box appears, choose one of the root nodes defined in the XML schema file.
- Đối với tệp dữ liệu XML, Excel sẽ tìm cách phỏng đoán sơ đồ XML từ dữ liệu XML, sau đó tạo ra một ánh xạ XML.
Chọn OK.
Ánh xạ XML sẽ xuất hiện trong ngăn tác vụ Nguồn XML .
Ánh xạ các phần tử XML
Ánh xạ các 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.
ChọnNguồn 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.In the XML Source task pane, select the elements you want to map.
Để chọn các phần tử không kề nhau, hãy chọn một phần tử, rồi nhấn giữ Ctrl và chọn từng phần tử mà bạn muốn ánh xạ.Để ánh xạ các phần tử, hãy hoàn thành các bước sau:
Right-click the selected elements, and select Map element.
In the Map XML elements dialog box, select a cell and select 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ạ.
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 đề Chọn 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 Chọn 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 Chọn 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.
In the XML Source task pane, select Options to further control XML table behavior:
Tự động Phối các Phần tử khi Ánh xạ Khi chọn hộp kiểm này, Excel sẽ tự động bung rộng bảng XML 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. Chọn Xem lại>Chia sẻ Sổ làm việc để xác nhận điều đó và loại bỏ sổ làm việc này khỏi mục đích dùng chung khi cần thiết.
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 bảng Excel that contains the same data. If you want the new table to be an XML table, complete the following steps: > 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. > 1. Á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. Ô không được phối và thành phần được ánh xạ tới ô nơi đặt con trỏ.
Mẹo
- You can unmap XML elements you don't want to use, or to prevent the contents of cells from being overwritten when you import XML data. 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. When the import is complete, you can map the XML element to the formula cells again, so you can export the results of the formulas to the XML data file.
- To unmap XML elements, right-click their name in the XML Source task pane, and select 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 làm theo các bước sau để hiển thị tab này:
- ChọnTùy chọntệp>.
- Chọn danh mục Tùy chỉnh Ruy-băng .
- Under Main Tabs, check the Developer box, and select OK.
Xem thêm
Xóa thông tin ánh xạ XML khỏi sổ làm việc