将单元格链接到 XML 地图元素
Contents
[
Hide
]
可能的使用场景
您可以使用Aspose.Cells将单元格链接到XML映射元素。请使用Cells.LinkToXmlMap()方法来实现此目的。
将单元格链接到 XML 地图元素
以下示例代码加载包含 XML 地图的源 Excel 文件(5472518.xlsx),然后将单元格 A1、B2、C3、D4、E5 和 F6 分别链接到 XML 地图元素 FIELD1、FIELD2、FIELD4、FIELD5、FIELD7 和 FIELD8,然后将工作簿保存在输出 Excel 文件(5472517.xlsx)中。
如果您打开输出的 Excel 文件(5472517.xlsx) 并点击Developer > Source按钮,您将会看到这些单元格已链接到 XML 地图元素,并且它们也会被 Microsoft Excel 标记,如下图所示。
示例代码
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(LinkCellstoXmlMapElements.class) + "articles/"; | |
// Load sample workbook | |
Workbook wb = new Workbook(dataDir + "LinkCellstoXmlMapElements_in.xlsx"); | |
// Access the Xml Map inside it | |
XmlMap map = wb.getWorksheets().getXmlMaps().get(0); | |
// Access first worksheet | |
Worksheet ws = wb.getWorksheets().get(0); | |
// Map FIELD1 and FIELD2 to cell A1 and B2 | |
ws.getCells().linkToXmlMap(map.getName(), 0, 0, "/root/row/FIELD1"); | |
ws.getCells().linkToXmlMap(map.getName(), 1, 1, "/root/row/FIELD2"); | |
// Map FIELD4 and FIELD5 to cell C3 and D4 | |
ws.getCells().linkToXmlMap(map.getName(), 2, 2, "/root/row/FIELD4"); | |
ws.getCells().linkToXmlMap(map.getName(), 3, 3, "/root/row/FIELD5"); | |
// Map FIELD7 and FIELD8 to cell E5 and F6 | |
ws.getCells().linkToXmlMap(map.getName(), 4, 4, "/root/row/FIELD7"); | |
ws.getCells().linkToXmlMap(map.getName(), 5, 5, "/root/row/FIELD8"); | |
// Save the workbook in xlsx format | |
wb.save(dataDir + "LinkCellstoXmlMapElements_out.xlsx"); |