问题描述
至于,SpreadsheetML的基本文档结构文档由工作簿中的工作表和工作表元素组成。
As to MSDN the basic document structure of a SpreadsheetML document consists of the Sheets and Sheet elements, which reference the worksheets in the Workbook.
例如,有一个工作簿包含表格:
For example, there is one workbook with sheets:
<workbook xmlns=http://schemas.openxmlformats.org/spreadsheetml/2006/main xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
<sheets>
<sheet name="MySheet1" sheetId="1" r:id="rId1" />
<sheet name="MySheet2" sheetId="2" r:id="rId2" />
</sheets>
</workbook>
还有两个工作表如:
<?xml version="1.0" encoding="UTF-8" ?>
<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
<sheetData>
<row r="1">
<c r="A1">
<v>100</v>
</c>
</row>
</sheetData>
</worksheet>
我的任务是通过其工作表的名称获取工作表。
但我不明白什么是关系beetwen Sheet和Worksheet。
好的,我在表格中找到了预期名称为MySheet2的Sheet,但是如何获得适当的工作表?工作表类没有名称属性或一些外键链接到Sheet。
My task is to get Worksheet by name of its Sheet.But I don't understand what is relationship beetwen Sheet and Worksheet.Ok, I found Sheet with expected name "MySheet2" in Sheets, but how can I get appropriate Worksheet? Worksheet class doesn't have name-attributes or some "foreign keys" to link with Sheet.
推荐答案
您的答案是正确的您需要使用关系ID,但您可以使用和方法:
Your answer is correct in that you need to use the relationship id but you can simplify your code a little by using the Descendants<T>
and GetPartById
methods:
//find the sheet by name
Sheet sheet = workbookPart.Workbook.Descendants<Sheet>().FirstOrDefault(
s => s.Name.ToString().Equals(sheetName, StringComparison.InvariantCultureIgnoreCase));
if (sheet != null)
{
string relationshipId = sheets.First().Id.Value;
//get the worksheetpart by Id
WorksheetPart worksheetPart = (WorksheetPart)workbookPart.GetPartById(relationshipId);
//get the sheetdata from the worksheet
SheetData sheetData = worksheetPart.Worksheet.Elements<SheetData>().First();
}
这篇关于工作表和工作表之间的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!