本文整理汇总了C#中SpreadsheetDocument.AddNewPart方法的典型用法代码示例。如果您正苦于以下问题:C# SpreadsheetDocument.AddNewPart方法的具体用法?C# SpreadsheetDocument.AddNewPart怎么用?C# SpreadsheetDocument.AddNewPart使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SpreadsheetDocument
的用法示例。
在下文中一共展示了SpreadsheetDocument.AddNewPart方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Print
public void Print(SpreadsheetDocument document)
{
var extendedFilePropertiesPart = document.AddNewPart<ExtendedFilePropertiesPart>("rId3");
generateExtendedFilePropertiesPartContent(extendedFilePropertiesPart);
var workbookPart = document.AddWorkbookPart();
generateWorkbookPartContent(workbookPart);
var workbookStylesPart = workbookPart.AddNewPart<WorkbookStylesPart>("rId3");
GenerateWorkbookStylesPartContent(workbookStylesPart);
var themePart = workbookPart.AddNewPart<ThemePart>("rId2");
generateThemePartContent(themePart);
var worksheetPart = workbookPart.AddNewPart<WorksheetPart>("rId1");
GenerateWorksheetPartContent(worksheetPart);
var spreadsheetPrinterSettingsPart = worksheetPart.AddNewPart<SpreadsheetPrinterSettingsPart>("rId1");
generateSpreadsheetPrinterSettingsPartContent(spreadsheetPrinterSettingsPart);
var sharedStringTablePart = workbookPart.AddNewPart<SharedStringTablePart>("rId4");
GenerateSharedStringTablePartContent(sharedStringTablePart);
setPackageProperties(document);
}
示例2: CreateParts
// Adds child parts and generates content of the specified part.
private void CreateParts(SpreadsheetDocument document)
{
ExtendedFilePropertiesPart extendedFilePropertiesPart1 = document.AddNewPart<ExtendedFilePropertiesPart>("rId3");
GenerateExtendedFilePropertiesPart1Content(extendedFilePropertiesPart1);
WorkbookPart workbookPart1 = document.AddWorkbookPart();
GenerateWorkbookPart1Content(workbookPart1);
WorksheetPart worksheetPart1 = workbookPart1.AddNewPart<WorksheetPart>("rId3");
GenerateWorksheetPart1Content(worksheetPart1);
WorksheetPart worksheetPart2 = workbookPart1.AddNewPart<WorksheetPart>("rId2");
GenerateWorksheetPart2Content(worksheetPart2);
WorksheetPart worksheetPart3 = workbookPart1.AddNewPart<WorksheetPart>("rId1");
GenerateWorksheetPart3Content(worksheetPart3);
WorkbookStylesPart workbookStylesPart1 = workbookPart1.AddNewPart<WorkbookStylesPart>("rId5");
GenerateWorkbookStylesPart1Content(workbookStylesPart1);
ThemePart themePart1 = workbookPart1.AddNewPart<ThemePart>("rId4");
GenerateThemePart1Content(themePart1);
SetPackageProperties(document);
}
示例3: CreateParts
// Adds child parts and generates content of the specified part.
private void CreateParts(SpreadsheetDocument document)
{
ExtendedFilePropertiesPart extendedFilePropertiesPart1 = document.AddNewPart<ExtendedFilePropertiesPart>("rId3");
GenerateExtendedFilePropertiesPart1Content(extendedFilePropertiesPart1);
WorkbookPart workbookPart1 = document.AddWorkbookPart();
GenerateWorkbookPart1Content(workbookPart1);
WorkbookStylesPart workbookStylesPart1 = workbookPart1.AddNewPart<WorkbookStylesPart>("rId3");
GenerateWorkbookStylesPart1Content(workbookStylesPart1);
ThemePart themePart1 = workbookPart1.AddNewPart<ThemePart>("rId2");
GenerateThemePart1Content(themePart1);
WorksheetPart worksheetPart1 = workbookPart1.AddNewPart<WorksheetPart>("rId1");
GenerateWorksheetPart1Content(worksheetPart1);
DrawingsPart drawingsPart1 = worksheetPart1.AddNewPart<DrawingsPart>("rId2");
GenerateDrawingsPart1Content(drawingsPart1);
ImagePart imagePart1 = drawingsPart1.AddNewPart<ImagePart>("image/jpeg", "rId1");
GenerateImagePart1Content(imagePart1);
SpreadsheetPrinterSettingsPart spreadsheetPrinterSettingsPart1 = worksheetPart1.AddNewPart<SpreadsheetPrinterSettingsPart>("rId1");
GenerateSpreadsheetPrinterSettingsPart1Content(spreadsheetPrinterSettingsPart1);
CalculationChainPart calculationChainPart1 = workbookPart1.AddNewPart<CalculationChainPart>("rId5");
GenerateCalculationChainPart1Content(calculationChainPart1);
SharedStringTablePart sharedStringTablePart1 = workbookPart1.AddNewPart<SharedStringTablePart>("rId4");
GenerateSharedStringTablePart1Content(sharedStringTablePart1);
SetPackageProperties(document);
}
示例4: CreateParts
// Adds child parts and generates content of the specified part.
private void CreateParts(SpreadsheetDocument document)
{
ExtendedFilePropertiesPart extendedFilePropertiesPart1 = document.AddNewPart<ExtendedFilePropertiesPart>("rId3");
GenerateExtendedFilePropertiesPart1Content(extendedFilePropertiesPart1);
WorkbookPart workbookPart1 = document.AddWorkbookPart();
GenerateWorkbookPart1Content(workbookPart1);
ThemePart themePart1 = workbookPart1.AddNewPart<ThemePart>("rId3");
GenerateThemePart1Content(themePart1);
ExtendedPart extendedPart1 = workbookPart1.AddExtendedPart("http://schemas.openxmlformats.org/officeDocument/2006/relationships/powerPivotData", "application/vnd.openxmlformats-officedocument.model+data", "data", "rId7");
GenerateExtendedPart1Content(extendedPart1);
PivotTableCacheDefinitionPart pivotTableCacheDefinitionPart1 = workbookPart1.AddNewPart<PivotTableCacheDefinitionPart>("rId2");
GeneratePivotTableCacheDefinitionPart1Content(pivotTableCacheDefinitionPart1);
WorksheetPart worksheetPart1 = workbookPart1.AddNewPart<WorksheetPart>("rId1");
GenerateWorksheetPart1Content(worksheetPart1);
TableDefinitionPart tableDefinitionPart1 = worksheetPart1.AddNewPart<TableDefinitionPart>("rId3");
GenerateTableDefinitionPart1Content(tableDefinitionPart1);
QueryTablePart queryTablePart1 = tableDefinitionPart1.AddNewPart<QueryTablePart>("rId1");
GenerateQueryTablePart1Content(queryTablePart1);
SpreadsheetPrinterSettingsPart spreadsheetPrinterSettingsPart1 = worksheetPart1.AddNewPart<SpreadsheetPrinterSettingsPart>("rId2");
GenerateSpreadsheetPrinterSettingsPart1Content(spreadsheetPrinterSettingsPart1);
PivotTablePart pivotTablePart1 = worksheetPart1.AddNewPart<PivotTablePart>("rId1");
GeneratePivotTablePart1Content(pivotTablePart1);
pivotTablePart1.AddPart(pivotTableCacheDefinitionPart1, "rId1");
SharedStringTablePart sharedStringTablePart1 = workbookPart1.AddNewPart<SharedStringTablePart>("rId6");
GenerateSharedStringTablePart1Content(sharedStringTablePart1);
WorkbookStylesPart workbookStylesPart1 = workbookPart1.AddNewPart<WorkbookStylesPart>("rId5");
GenerateWorkbookStylesPart1Content(workbookStylesPart1);
ConnectionsPart connectionsPart1 = workbookPart1.AddNewPart<ConnectionsPart>("rId4");
GenerateConnectionsPart1Content(connectionsPart1);
SetPackageProperties(document);
}
示例5: CreateParts
// Adds child parts and generates content of the specified part.
private void CreateParts(SpreadsheetDocument document)
{
ExtendedFilePropertiesPart extendedFilePropertiesPart1 = document.AddNewPart<ExtendedFilePropertiesPart>("rId3");
GenerateExtendedFilePropertiesPart1Content(extendedFilePropertiesPart1);
WorkbookPart workbookPart1 = document.AddWorkbookPart();
GenerateWorkbookPart1Content(workbookPart1);
SlicerCachePart slicerCachePart1 = workbookPart1.AddNewPart<SlicerCachePart>("rId3");
GenerateSlicerCachePart1Content(slicerCachePart1);
SharedStringTablePart sharedStringTablePart1 = workbookPart1.AddNewPart<SharedStringTablePart>("rId7");
GenerateSharedStringTablePart1Content(sharedStringTablePart1);
SlicerCachePart slicerCachePart2 = workbookPart1.AddNewPart<SlicerCachePart>("rId2");
GenerateSlicerCachePart2Content(slicerCachePart2);
WorksheetPart worksheetPart1 = workbookPart1.AddNewPart<WorksheetPart>("rId1");
GenerateWorksheetPart1Content(worksheetPart1);
TableDefinitionPart tableDefinitionPart1 = worksheetPart1.AddNewPart<TableDefinitionPart>("rId3");
GenerateTableDefinitionPart1Content(tableDefinitionPart1);
TableDefinitionPart tableDefinitionPart2 = worksheetPart1.AddNewPart<TableDefinitionPart>("rId2");
GenerateTableDefinitionPart2Content(tableDefinitionPart2);
DrawingsPart drawingsPart1 = worksheetPart1.AddNewPart<DrawingsPart>("rId1");
GenerateDrawingsPart1Content(drawingsPart1);
SlicersPart slicersPart1 = worksheetPart1.AddNewPart<SlicersPart>("rId4");
GenerateSlicersPart1Content(slicersPart1);
WorkbookStylesPart workbookStylesPart1 = workbookPart1.AddNewPart<WorkbookStylesPart>("rId6");
GenerateWorkbookStylesPart1Content(workbookStylesPart1);
ThemePart themePart1 = workbookPart1.AddNewPart<ThemePart>("rId5");
GenerateThemePart1Content(themePart1);
SlicerCachePart slicerCachePart3 = workbookPart1.AddNewPart<SlicerCachePart>("rId4");
GenerateSlicerCachePart3Content(slicerCachePart3);
SetPackageProperties(document);
}
示例6: CreateParts
// Adds child parts and generates content of the specified part.
private void CreateParts(SpreadsheetDocument document)
{
var context = new SaveContext();
var workbookPart = document.WorkbookPart ?? document.AddWorkbookPart();
var worksheets = WorksheetsInternal;
var partsToRemove = workbookPart.Parts.Where(s => worksheets.Deleted.Contains(s.RelationshipId)).ToList();
partsToRemove.ForEach(s => workbookPart.DeletePart(s.OpenXmlPart));
context.RelIdGenerator.AddValues(workbookPart.Parts.Select(p => p.RelationshipId).ToList(), RelType.Workbook);
var extendedFilePropertiesPart = document.ExtendedFilePropertiesPart ??
document.AddNewPart<ExtendedFilePropertiesPart>(
context.RelIdGenerator.GetNext(RelType.Workbook));
GenerateExtendedFilePropertiesPartContent(extendedFilePropertiesPart);
GenerateWorkbookPartContent(workbookPart, context);
var sharedStringTablePart = workbookPart.SharedStringTablePart ??
workbookPart.AddNewPart<SharedStringTablePart>(
context.RelIdGenerator.GetNext(RelType.Workbook));
GenerateSharedStringTablePartContent(sharedStringTablePart, context);
var workbookStylesPart = workbookPart.WorkbookStylesPart ??
workbookPart.AddNewPart<WorkbookStylesPart>(
context.RelIdGenerator.GetNext(RelType.Workbook));
GenerateWorkbookStylesPartContent(workbookStylesPart, context);
foreach (var worksheet in WorksheetsInternal.Cast<XLWorksheet>().OrderBy(w => w.Position))
{
//context.RelIdGenerator.Reset(RelType.);
WorksheetPart worksheetPart;
var wsRelId = worksheet.RelId;
if (workbookPart.Parts.Any(p => p.RelationshipId == wsRelId))
{
worksheetPart = (WorksheetPart)workbookPart.GetPartById(wsRelId);
var wsPartsToRemove = worksheetPart.TableDefinitionParts.ToList();
wsPartsToRemove.ForEach(tdp => worksheetPart.DeletePart(tdp));
}
else
worksheetPart = workbookPart.AddNewPart<WorksheetPart>(wsRelId);
context.RelIdGenerator.AddValues(worksheetPart.HyperlinkRelationships.Select(hr => hr.Id).ToList(),
RelType.Workbook);
context.RelIdGenerator.AddValues(worksheetPart.Parts.Select(p => p.RelationshipId).ToList(),
RelType.Workbook);
if (worksheetPart.DrawingsPart != null)
context.RelIdGenerator.AddValues(
worksheetPart.DrawingsPart.Parts.Select(p => p.RelationshipId).ToList(), RelType.Workbook);
// delete comment related parts (todo: review)
DeleteComments(worksheetPart, worksheet, context);
if (worksheet.Internals.CellsCollection.GetCells(c => c.HasComment).Any())
{
var worksheetCommentsPart =
worksheetPart.AddNewPart<WorksheetCommentsPart>(context.RelIdGenerator.GetNext(RelType.Workbook));
GenerateWorksheetCommentsPartContent(worksheetCommentsPart, worksheet);
//VmlDrawingPart vmlDrawingPart = worksheetPart.AddNewPart<VmlDrawingPart>(worksheet.LegacyDrawingId);
var vmlDrawingPart = worksheetPart.VmlDrawingParts.FirstOrDefault();
if (vmlDrawingPart == null)
{
if (XLHelper.IsNullOrWhiteSpace(worksheet.LegacyDrawingId))
{
worksheet.LegacyDrawingId = context.RelIdGenerator.GetNext(RelType.Workbook);
worksheet.LegacyDrawingIsNew = true;
}
vmlDrawingPart = worksheetPart.AddNewPart<VmlDrawingPart>(worksheet.LegacyDrawingId);
}
GenerateVmlDrawingPartContent(vmlDrawingPart, worksheet, context);
}
GenerateWorksheetPartContent(worksheetPart, worksheet, context);
if (worksheet.PivotTables.Any())
{
GeneratePivotTables(workbookPart, worksheetPart, worksheet, context);
}
//DrawingsPart drawingsPart = worksheetPart.AddNewPart<DrawingsPart>("rId1");
//GenerateDrawingsPartContent(drawingsPart, worksheet);
//foreach (var chart in worksheet.Charts)
//{
// ChartPart chartPart = drawingsPart.AddNewPart<ChartPart>("rId1");
// GenerateChartPartContent(chartPart, (XLChart)chart);
//}
}
GenerateCalculationChainPartContent(workbookPart, context);
//.........这里部分代码省略.........
示例7: ImportStyle
/// <summary>
/// Import style from source to target document
/// </summary>
/// <param name="sourceDoc"></param>
/// <param name="id"></param>
/// <param name="targetDoc"></param>
/// <returns></returns>
protected static uint ImportStyle(SpreadsheetDocument sourceDoc, uint id, SpreadsheetDocument targetDoc)
{
// Get style parts
var srcStylePart = sourceDoc.WorkbookPart.GetPartsOfType<WorkbookStylesPart>().FirstOrDefault();
if (srcStylePart == null)
throw new InvalidOperationException("StylesPart was not found in source document!");
if (srcStylePart.Stylesheet == null)
throw new InvalidOperationException("Stylesheet was not found in source document!");
var tgtStylePart = targetDoc.WorkbookPart.GetPartsOfType<WorkbookStylesPart>().FirstOrDefault();
if (tgtStylePart == null)
tgtStylePart = targetDoc.AddNewPart<WorkbookStylesPart>();
if (tgtStylePart.Stylesheet == null)
tgtStylePart.Stylesheet = new Stylesheet();
// Get the source style
var srcCellFormat = srcStylePart.Stylesheet.CellFormats.ElementAt((int) id) as CellFormat;
if (srcCellFormat == null)
throw new InvalidOperationException(String.Format("CellFormat {0} was not found in source document!", id));
// Import the style to target
return ImportCellFormat(srcStylePart.Stylesheet, srcCellFormat, tgtStylePart.Stylesheet);
}
示例8: CreateParts
// Adds child parts and generates content of the specified part.
private void CreateParts(SpreadsheetDocument document)
{
ExtendedFilePropertiesPart extendedFilePropertiesPart1 = document.AddNewPart<ExtendedFilePropertiesPart>("rId3");
GenerateExtendedFilePropertiesPart1Content(extendedFilePropertiesPart1);
WorkbookPart workbookPart1 = document.AddWorkbookPart();
GenerateWorkbookPart1Content(workbookPart1);
ThemePart themePart1 = workbookPart1.AddNewPart<ThemePart>("rId3");
GenerateThemePart1Content(themePart1);
WorksheetPart worksheetPart1 = workbookPart1.AddNewPart<WorksheetPart>("rId2");
GenerateWorksheetPart1Content(worksheetPart1);
DrawingsPart drawingsPart1 = worksheetPart1.AddNewPart<DrawingsPart>("rId1");
GenerateDrawingsPart1Content(drawingsPart1);
DiagramStylePart diagramStylePart1 = drawingsPart1.AddNewPart<DiagramStylePart>("rId3");
GenerateDiagramStylePart1Content(diagramStylePart1);
ExtendedPart extendedPart1 = drawingsPart1.AddExtendedPart("http://schemas.microsoft.com/office/2007/relationships/hdphoto", "image/vnd.ms-photo", "wdp", "rId7");
GenerateExtendedPart1Content(extendedPart1);
DiagramLayoutDefinitionPart diagramLayoutDefinitionPart1 = drawingsPart1.AddNewPart<DiagramLayoutDefinitionPart>("rId2");
GenerateDiagramLayoutDefinitionPart1Content(diagramLayoutDefinitionPart1);
DiagramDataPart diagramDataPart1 = drawingsPart1.AddNewPart<DiagramDataPart>("rId1");
GenerateDiagramDataPart1Content(diagramDataPart1);
ImagePart imagePart1 = drawingsPart1.AddNewPart<ImagePart>("image/png", "rId6");
GenerateImagePart1Content(imagePart1);
DiagramPersistLayoutPart diagramPersistLayoutPart1 = drawingsPart1.AddNewPart<DiagramPersistLayoutPart>("rId5");
GenerateDiagramPersistLayoutPart1Content(diagramPersistLayoutPart1);
DiagramColorsPart diagramColorsPart1 = drawingsPart1.AddNewPart<DiagramColorsPart>("rId4");
GenerateDiagramColorsPart1Content(diagramColorsPart1);
WorksheetPart worksheetPart2 = workbookPart1.AddNewPart<WorksheetPart>("rId1");
GenerateWorksheetPart2Content(worksheetPart2);
DrawingsPart drawingsPart2 = worksheetPart2.AddNewPart<DrawingsPart>("rId3");
GenerateDrawingsPart2Content(drawingsPart2);
ChartPart chartPart1 = drawingsPart2.AddNewPart<ChartPart>("rId1");
GenerateChartPart1Content(chartPart1);
ChartColorStylePart chartColorStylePart1 = chartPart1.AddNewPart<ChartColorStylePart>("rId2");
GenerateChartColorStylePart1Content(chartColorStylePart1);
ChartStylePart chartStylePart1 = chartPart1.AddNewPart<ChartStylePart>("rId1");
GenerateChartStylePart1Content(chartStylePart1);
SpreadsheetPrinterSettingsPart spreadsheetPrinterSettingsPart1 = worksheetPart2.AddNewPart<SpreadsheetPrinterSettingsPart>("rId2");
GenerateSpreadsheetPrinterSettingsPart1Content(spreadsheetPrinterSettingsPart1);
WorksheetCommentsPart worksheetCommentsPart1 = worksheetPart2.AddNewPart<WorksheetCommentsPart>("rId6");
GenerateWorksheetCommentsPart1Content(worksheetCommentsPart1);
TableDefinitionPart tableDefinitionPart1 = worksheetPart2.AddNewPart<TableDefinitionPart>("rId5");
GenerateTableDefinitionPart1Content(tableDefinitionPart1);
VmlDrawingPart vmlDrawingPart1 = worksheetPart2.AddNewPart<VmlDrawingPart>("rId4");
GenerateVmlDrawingPart1Content(vmlDrawingPart1);
worksheetPart2.AddHyperlinkRelationship(new System.Uri("http://www.ericwhite.com/", System.UriKind.Absolute), true, "rId1");
CalculationChainPart calculationChainPart1 = workbookPart1.AddNewPart<CalculationChainPart>("rId6");
GenerateCalculationChainPart1Content(calculationChainPart1);
SharedStringTablePart sharedStringTablePart1 = workbookPart1.AddNewPart<SharedStringTablePart>("rId5");
GenerateSharedStringTablePart1Content(sharedStringTablePart1);
WorkbookStylesPart workbookStylesPart1 = workbookPart1.AddNewPart<WorkbookStylesPart>("rId4");
GenerateWorkbookStylesPart1Content(workbookStylesPart1);
SetPackageProperties(document);
}
示例9: CreateParts
// Adds child parts and generates content of the specified part.
private void CreateParts(SpreadsheetDocument document)
{
WebExTaskpanesPart webExTaskpanesPart1 = document.AddNewPart<WebExTaskpanesPart>("rId2");
GenerateWebExTaskpanesPart1Content(webExTaskpanesPart1);
WebExtensionPart webExtensionPart1 = webExTaskpanesPart1.AddNewPart<WebExtensionPart>("rId2");
GenerateWebExtensionPart1Content(webExtensionPart1);
WebExtensionPart webExtensionPart2 = webExTaskpanesPart1.AddNewPart<WebExtensionPart>("rId1");
GenerateWebExtensionPart2Content(webExtensionPart2);
WorkbookPart workbookPart1 = document.AddWorkbookPart();
GenerateWorkbookPart1Content(workbookPart1);
ThemePart themePart1 = workbookPart1.AddNewPart<ThemePart>("rId3");
GenerateThemePart1Content(themePart1);
WorksheetPart worksheetPart1 = workbookPart1.AddNewPart<WorksheetPart>("rId2");
GenerateWorksheetPart1Content(worksheetPart1);
DrawingsPart drawingsPart1 = worksheetPart1.AddNewPart<DrawingsPart>("rId1");
GenerateDrawingsPart1Content(drawingsPart1);
WebExtensionPart webExtensionPart3 = drawingsPart1.AddNewPart<WebExtensionPart>("rId3");
GenerateWebExtensionPart3Content(webExtensionPart3);
ImagePart imagePart1 = webExtensionPart3.AddNewPart<ImagePart>("image/png", "rId1");
GenerateImagePart1Content(imagePart1);
ImagePart imagePart2 = drawingsPart1.AddNewPart<ImagePart>("image/png", "rId2");
GenerateImagePart2Content(imagePart2);
WebExtensionPart webExtensionPart4 = drawingsPart1.AddNewPart<WebExtensionPart>("rId1");
GenerateWebExtensionPart4Content(webExtensionPart4);
webExtensionPart4.AddPart(imagePart2, "rId1");
drawingsPart1.AddPart(imagePart1, "rId4");
WorksheetPart worksheetPart2 = workbookPart1.AddNewPart<WorksheetPart>("rId1");
GenerateWorksheetPart2Content(worksheetPart2);
DrawingsPart drawingsPart2 = worksheetPart2.AddNewPart<DrawingsPart>("rId1");
GenerateDrawingsPart2Content(drawingsPart2);
ImagePart imagePart3 = drawingsPart2.AddNewPart<ImagePart>("image/png", "rId2");
GenerateImagePart3Content(imagePart3);
WebExtensionPart webExtensionPart5 = drawingsPart2.AddNewPart<WebExtensionPart>("rId1");
GenerateWebExtensionPart5Content(webExtensionPart5);
webExtensionPart5.AddPart(imagePart3, "rId1");
WorkbookStylesPart workbookStylesPart1 = workbookPart1.AddNewPart<WorkbookStylesPart>("rId4");
GenerateWorkbookStylesPart1Content(workbookStylesPart1);
ExtendedFilePropertiesPart extendedFilePropertiesPart1 = document.AddNewPart<ExtendedFilePropertiesPart>("rId4");
GenerateExtendedFilePropertiesPart1Content(extendedFilePropertiesPart1);
SetPackageProperties(document);
}
示例10: CreateParts
// Adds child parts and generates content of the specified part.
private void CreateParts(SpreadsheetDocument document, List<ProjectFile> projectFiles)
{
ExtendedFilePropertiesPart extendedFileProperties = document.AddNewPart<ExtendedFilePropertiesPart>("rId3");
GenerateExtendedFilePropertiesContent(extendedFileProperties);
WorkbookPart workbook = document.AddWorkbookPart();
GenerateWorkbookContent(workbook);
WorkbookStylesPart workbookStyles = workbook.AddNewPart<WorkbookStylesPart>("rId2");
GenerateWorkbookStylesContent(workbookStyles);
//from this point is the content
WorksheetPart worksheet = workbook.AddNewPart<WorksheetPart>("rId1");
GenerateWorksheetContent(worksheet, projectFiles);
SharedStringTablePart sharedStringTablePart = workbook.AddNewPart<SharedStringTablePart>("rId4");
GenerateSharedStringTablePartContent(sharedStringTablePart);
SetPackageProperties(document);
}
示例11: ImportSharedString
/// <summary>
/// Import shared string item from one document to another
/// </summary>
/// <param name="sourceDoc">Source excel document</param>
/// <param name="id">Id of shared string item</param>
/// <param name="targetDoc">Target excel document</param>
/// <returns></returns>
protected static uint ImportSharedString(SpreadsheetDocument sourceDoc, uint id, SpreadsheetDocument targetDoc)
{
// Prepare resource parts
var srcSharedStringPart = sourceDoc.WorkbookPart.GetPartsOfType<SharedStringTablePart>().FirstOrDefault();
if (srcSharedStringPart == null)
throw new InvalidOperationException("SharedStringPart was not found in Source document!");
if (srcSharedStringPart.SharedStringTable == null)
throw new InvalidOperationException("SharedStringTable was not found in Source document!");
var tgtSharedStringPart = targetDoc.WorkbookPart.GetPartsOfType<SharedStringTablePart>().FirstOrDefault();
if (tgtSharedStringPart == null)
{ // Create one
tgtSharedStringPart = targetDoc.AddNewPart<SharedStringTablePart>();
}
if (tgtSharedStringPart.SharedStringTable == null)
{
tgtSharedStringPart.SharedStringTable = new SharedStringTable();
}
// Get original shared string item
var srcItem = srcSharedStringPart.SharedStringTable.ElementAt((int)id) as SharedStringItem;
if (srcItem == null)
throw new InvalidOperationException(
String.Format("SharedStringItem {0} was not found in source document!", id));
// Try to find same shared string item in target
uint i = 0;
foreach (var item in tgtSharedStringPart.SharedStringTable.Elements<SharedStringItem>())
{
if (item.InnerText == srcItem.InnerText)
{ // Found same value
return i;
}
i++;
}
// Not found, import it
var tgtItem = srcItem.Clone() as SharedStringItem;
tgtSharedStringPart.SharedStringTable.AppendChild(tgtItem);
return i;
}