本文整理汇总了C#中DocumentFormat.OpenXml.Packaging.WorkbookPart.AddPart方法的典型用法代码示例。如果您正苦于以下问题:C# WorkbookPart.AddPart方法的具体用法?C# WorkbookPart.AddPart怎么用?C# WorkbookPart.AddPart使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DocumentFormat.OpenXml.Packaging.WorkbookPart
的用法示例。
在下文中一共展示了WorkbookPart.AddPart方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: copySheet
/// <summary>
/// Copies a sheet with a given sheet name and saves it with the clonedSheetName in the workbook
/// </summary>
/// <param name="docType">DocType to created for temp file in the method</param>
/// <param name="workbookPart">Workbook which the worksheet to be copied and the copied worksheet to be saved</param>
/// <param name="sheetName">Name of the worksheet to copy</param>
/// <param name="clonedSheetName">New name of the copied sheet</param>
private void copySheet(SpreadsheetDocumentType docType,
WorkbookPart workbookPart, string sheetName,
string clonedSheetName)
{
//Get the source sheet to be copied
WorksheetPart sourceSheetPart = getWorksheetPart(workbookPart,
sheetName);
//Take advantage of AddPart for deep cloning
SpreadsheetDocument tempSheet =
SpreadsheetDocument.Create(new MemoryStream(), docType);
WorkbookPart tempWorkbookPart = tempSheet.AddWorkbookPart();
WorksheetPart tempWorksheetPart =
tempWorkbookPart.AddPart(sourceSheetPart);
//Add cloned sheet and all associated parts to workbook
WorksheetPart clonedSheet = workbookPart.AddPart(tempWorksheetPart);
//Table definition parts are somewhat special and need unique ids...so let's make an id based on count
int numTableDefParts =
sourceSheetPart.GetPartsCountOfType<TableDefinitionPart>();
_tableId = numTableDefParts;
//Clean up table definition parts (tables need unique ids)
if (numTableDefParts != 0) {
fixupTableParts(clonedSheet);
}
//There should only be one sheet that has focus
cleanView(clonedSheet);
//Add new sheet to main workbook part
var sheets = workbookPart.Workbook.GetFirstChild<Sheets>();
var copiedSheet = new Sheet();
copiedSheet.Name = clonedSheetName;
copiedSheet.Id = workbookPart.GetIdOfPart(clonedSheet);
copiedSheet.SheetId = (uint) sheets.ChildElements.Count + 1;
sheets.Append(copiedSheet);
//Save Changes
workbookPart.Workbook.Save();
}