本文整理汇总了C#中SpreadsheetDocument.AddWorkbookPart方法的典型用法代码示例。如果您正苦于以下问题:C# SpreadsheetDocument.AddWorkbookPart方法的具体用法?C# SpreadsheetDocument.AddWorkbookPart怎么用?C# SpreadsheetDocument.AddWorkbookPart使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SpreadsheetDocument
的用法示例。
在下文中一共展示了SpreadsheetDocument.AddWorkbookPart方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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);
}
示例2: 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);
}
示例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
private void CreateParts(SpreadsheetDocument document)
{
WorkbookPart workbookPart1 = document.AddWorkbookPart();
GenerateWorkbookPart1Content(workbookPart1);
// Create sharedstring
SharedStringTablePart sharedStringTablePart1 = workbookPart1.AddNewPart<SharedStringTablePart>("rId7");
GenerateSharedStringTablePart1Content(sharedStringTablePart1);
// Create Sheet2
WorksheetPart worksheetPart2 = workbookPart1.AddNewPart<WorksheetPart>("rId2");
GenerateWorksheetPart2Content(worksheetPart2);
// Create Sheet1
WorksheetPart worksheetPart3 = workbookPart1.AddNewPart<WorksheetPart>("rId1");
GenerateWorksheetPart3Content(worksheetPart3);
// Add PivotTablePart to Sheet1
PivotTablePart pivotTablePart1 = worksheetPart3.AddNewPart<PivotTablePart>("rId1");
GeneratePivotTablePart1Content(pivotTablePart1);
// Add PivotTableCacheDefinitionPart to PivotTablePart
PivotTableCacheDefinitionPart pivotTableCacheDefinitionPart1 = pivotTablePart1.AddNewPart<PivotTableCacheDefinitionPart>("rId1");
GeneratePivotTableCacheDefinitionPart1Content(pivotTableCacheDefinitionPart1);
// Add PivotTableCacheDefinitionPart to PivotTableCacheDefinitionPart
PivotTableCacheRecordsPart pivotTableCacheRecordsPart1 = pivotTableCacheDefinitionPart1.AddNewPart<PivotTableCacheRecordsPart>("rId1");
GeneratePivotTableCacheRecordsPart1Content(pivotTableCacheRecordsPart1);
WorkbookStylesPart workbookStylesPart1 = workbookPart1.AddNewPart<WorkbookStylesPart>("rId6");
GenerateWorkbookStylesPart1Content(workbookStylesPart1);
workbookPart1.AddPart(pivotTableCacheDefinitionPart1, "rId4");
}
示例5: Create
public ExcelHelper Create(string ExcelFile)
{
System.IO.FileInfo fi = new System.IO.FileInfo(ExcelFile);
if (!fi.Directory.Exists)
fi.Directory.Create();
_doc = SpreadsheetDocument.Create(ExcelFile, SpreadsheetDocumentType.Workbook);
_wbPart = _doc.AddWorkbookPart();
_wbPart.Workbook = new Workbook();
_doc.WorkbookPart.Workbook.AppendChild<Sheets>(new Sheets());
return _instance;
}
示例6: CreateParts
// Adds child parts and generates content of the specified part
private static void CreateParts(SpreadsheetDocument document)
{
WorkbookPart workbookPart1 = document.AddWorkbookPart();
GenerateWorkbookPart1Content(workbookPart1);
WorksheetPart worksheetPart1 = workbookPart1.AddNewPart<WorksheetPart>("rId1");
Worksheet worksheet1 = new Worksheet();
SheetData sheetData1 = new SheetData();
worksheet1.Append(sheetData1);
worksheetPart1.Worksheet = worksheet1;
//GenerateWorksheetPart1Content(worksheetPart1);
}
示例7: LogGenerator
private WorksheetPart wsSheet = null; // WorkSheetPart
#endregion Fields
#region Constructors
public LogGenerator(string fn)
{
//// get spreadsheet path from constructor
//path = folder;
//// File name is based on date and time
//DateTime now = DateTime.Now;
//// Construct the spreadsheet filename
//string fn = string.Format("{0}\\report_{1}-{2}-{3}_{4}{5}{6}.xlsx",
// path, now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second);
if (File.Exists(fn))
{
doc = SpreadsheetDocument.Open(fn, true);
}
else
{
//
// Create the Spreadsheet document
//
doc = SpreadsheetDocument.Create(fn, SpreadsheetDocumentType.Workbook);
//
// Add WoorkBookPart to the document
//
wbPart = doc.AddWorkbookPart();
wbPart.Workbook = new Workbook();
wbPart.Workbook.AddNamespaceDeclaration("x", "http://schemas.openxmlformats.org/spreadsheetml/2006/main");
//
// Add WorkSheetPart to the WorkBookPart
//
wsSheet = wbPart.AddNewPart<WorksheetPart>();
wsSheet.Worksheet = new Worksheet(new SheetData());
wsSheet.Worksheet.AddNamespaceDeclaration("x", "http://schemas.openxmlformats.org/spreadsheetml/2006/main");
//
// Add sheets to the WorkBook
//
sheets = doc.WorkbookPart.Workbook.AppendChild<Sheets>(new Sheets());
//
// Append new sheet and associate it with the workbook
//
sheet = new Sheet() { Id = doc.WorkbookPart.GetIdOfPart(wsSheet), SheetId = 1, Name = wsName };
sheet.AddNamespaceDeclaration("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships");
sheets.Append(sheet);
wbPart.Workbook.Save();
//CreatePackage(fn);
//if (File.Exists(fn))
//{
// TestXls();
//}
}
}
示例8: CopyWorksheet
/// <summary>
/// Copy sheet to another document
/// </summary>
/// <param name="sourceDoc">Source document</param>
/// <param name="srcSheetName">Name of source sheet</param>
/// <param name="targetDoc">Spreadsheet document to copied</param>
/// <param name="targetIndex">Index of copied sheet in target document</param>
public static void CopyWorksheet(SpreadsheetDocument sourceDoc, String srcSheetName, SpreadsheetDocument targetDoc, uint targetIndex)
{
// Locate the source sheet
if (sourceDoc.WorkbookPart == null)
throw new InvalidOperationException("WorkbookPart is not exist in sourceDoc!");
if (sourceDoc.WorkbookPart.Workbook.Sheets == null)
throw new InvalidOperationException("No sheets exist in sourceDoc!");
var srcSheet =
sourceDoc.WorkbookPart.Workbook.Sheets.Descendants<Sheet>()
.FirstOrDefault(a => a.Name == srcSheetName);
if (srcSheet == null)
throw new InvalidOperationException(String.Format("No sheet found with name {0}!", srcSheetName));
var srcSheetPart = sourceDoc.WorkbookPart.GetPartById(srcSheet.Id) as WorksheetPart;
if (srcSheetPart == null)
throw new InvalidOperationException(String.Format("Cannot find worksheet part with Id {0}!", srcSheet.Id));
var srcWorkSheet = srcSheetPart.Worksheet;
if (srcWorkSheet == null)
throw new InvalidOperationException("Worksheet not exist in source worksheet part!");
// Locate the position of target sheet
WorkbookPart tgtWbPart = targetDoc.WorkbookPart ?? targetDoc.AddWorkbookPart();
Sheets tgtSheets = tgtWbPart.Workbook.Sheets ?? tgtWbPart.Workbook.AppendChild<Sheets>(new Sheets());
if (targetIndex > tgtSheets.Count()) targetIndex = (uint)tgtSheets.Count();
// Create a new worksheet and clone data from original worksheet
var newSheetPart = tgtWbPart.AddNewPart<WorksheetPart>();
newSheetPart.Worksheet = new Worksheet(); //srcWorkSheet.Clone() as Worksheet;
// Create a unique ID for the new worksheet.
uint sheetId = 1;
if (tgtSheets.Elements<Sheet>().Any())
{
sheetId = tgtSheets.Elements<Sheet>().Select(s => s.SheetId.Value).Max() + 1;
}
// Add cloned worksheet to target workbook
var newSheet = new Sheet()
{
Id = tgtWbPart.GetIdOfPart(newSheetPart),
SheetId = sheetId,
Name = srcSheet.Name
};
tgtSheets.InsertAt(newSheet, (int)targetIndex);
// Import data from source sheet to target sheet
ImportWorksheet(sourceDoc, srcWorkSheet, targetDoc, newSheetPart.Worksheet);
// Import all necessary resources into target document that referenced by cloned sheet
//ImportResources(sourceDoc, newSheetPart.Worksheet, targetDoc);
// Import defined names
ImportDefinedNames(sourceDoc, srcSheetName, targetDoc);
// Import calculate chain
ImportCalculateChain(sourceDoc, (int)srcSheet.SheetId.Value, targetDoc, (int)sheetId);
// Save it
tgtWbPart.Workbook.Save();
}
示例9: Save
public override void Save(ResearchResult result)
{
if (!Directory.Exists(storageStr))
{
Directory.CreateDirectory(storageStr);
}
string fileName = storageStr + result.ResearchName;
if (File.Exists(fileName + ".xls") || File.Exists(fileName + ".xlsx"))
fileName += result.ResearchID;
document = SpreadsheetDocument.Create(fileName + ".xlsx", SpreadsheetDocumentType.Workbook);
workbook = document.AddWorkbookPart();
workbook.Workbook = new Workbook();
// Add a WorksheetPart to the WorkbookPart.
WorksheetPart worksheetPart = workbook.AddNewPart<WorksheetPart>();
worksheetPart.Worksheet = new Worksheet(new SheetData());
// Add Sheets to the Workbook.
Sheets sheets = document.WorkbookPart.Workbook.
AppendChild<Sheets>(new Sheets());
// Append a new worksheet and associate it with the workbook.
Sheet sheet = new Sheet()
{
Id = document.WorkbookPart.
GetIdOfPart(worksheetPart),
SheetId = 1,
Name = "mySheet"
};
sheets.Append(sheet);
/*SaveResearchInfo(result.ResearchID, result.ResearchName,
result.ResearchType, result.ModelType, result.RealizationCount,
result.Size, result.Edges);
SaveResearchParameters(result.ResearchParameterValues);
SaveGenerationParameters(result.GenerationParameterValues);
for (int i = 0; i < result.EnsembleResults.Count; ++i)
{
SaveEnsembleResult(result.EnsembleResults[i], i);
}*/
workbook.Workbook.Save();
document.Close();
}
示例10: 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);
}
示例11: 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);
}
示例12: CreateParts
// Adds child parts and generates content of the specified part.
private void CreateParts(SpreadsheetDocument document, WorkBookData data)
{
WorkbookPart workbookPart1 = document.AddWorkbookPart();
GenerateWorkbookPartContent(workbookPart1, data.sheetNames);
ThemePart themePart1 = workbookPart1.AddNewPart<ThemePart>("rId" + (data.data.Tables.Count + 1));
GenerateThemePart1Content(themePart1);
WorkbookStylesPart workbookStylesPart1 = workbookPart1.AddNewPart<WorkbookStylesPart>("rId" + (data.data.Tables.Count + 2));
GenerateWorkbookStylesPartContent(workbookStylesPart1);
int wsID = 1;
WorksheetPart wsPart;
DataRow currentRow;
foreach (DataTable dt in data.data.Tables)
{
wsPart = workbookPart1.AddNewPart<WorksheetPart>("rId" + wsID++);
GenerateWorksheetPartContent(wsPart, dt, wsID <= 2);
//Adding HyperLinkRelationships
int idTracker = 1;
for (int ii = 0; ii < dt.Rows.Count; ii++)
{
currentRow = dt.Rows[ii];
for (int jj = 0; jj < dt.Columns.Count; jj++)
{
if (dt.Columns[jj].DataType == typeof(string) && currentRow[jj].ToString().ToLower().Contains("http"))
{
wsPart.AddHyperlinkRelationship(new System.Uri(currentRow[jj].ToString(), System.UriKind.Absolute), true, "rId" + idTracker++);
}
}
}
}
//SharedStringTablePart sharedStringTablePart1 = workbookPart1.AddNewPart<SharedStringTablePart>("rId4");
//GenerateSharedStringTablePart1Content(sharedStringTablePart1);
SetPackageProperties(document);
}
示例13: OpenXmlExcel
public OpenXmlExcel(Stream stream)
{
spreadSheet = SpreadsheetDocument.Create(stream, SpreadsheetDocumentType.Workbook);
WorkbookPart workbookPart = spreadSheet.AddWorkbookPart();
workbookPart.Workbook = new Workbook();
WorksheetPart worksheetPart = InsertWorksheet(spreadSheet.WorkbookPart);
if (spreadSheet.WorkbookPart.GetPartsOfType<SharedStringTablePart>().Count() > 0)
{
shareStringPart = spreadSheet.WorkbookPart.GetPartsOfType<SharedStringTablePart>().First();
}
else
{
shareStringPart = spreadSheet.WorkbookPart.AddNewPart<SharedStringTablePart>();
}
shareStringPart.SharedStringTable = new SharedStringTable();
shareStringPart.SharedStringTable.Count = 1;
shareStringPart.SharedStringTable.UniqueCount = 1;
CurrentWorksheetPart = worksheetPart;
}
示例14: WriteExcelFile
private static void WriteExcelFile(DataSet ds, SpreadsheetDocument spreadsheet)
{
// Create the Excel file contents. This function is used when creating an Excel file either writing
// to a file, or writing to a MemoryStream.
spreadsheet.AddWorkbookPart();
spreadsheet.WorkbookPart.Workbook = new DocumentFormat.OpenXml.Spreadsheet.Workbook();
// My thanks to James Miera for the following line of code (which prevents crashes in Excel 2010)
spreadsheet.WorkbookPart.Workbook.Append(new BookViews(new WorkbookView()));
// If we don't add a "WorkbookStylesPart", OLEDB will refuse to connect to this .xlsx file !
WorkbookStylesPart workbookStylesPart = spreadsheet.WorkbookPart.AddNewPart<WorkbookStylesPart>("rIdStyles");
Stylesheet stylesheet = new Stylesheet();
workbookStylesPart.Stylesheet = stylesheet;
// Loop through each of the DataTables in our DataSet, and create a new Excel Worksheet for each.
uint worksheetNumber = 1;
Sheets sheets = spreadsheet.WorkbookPart.Workbook.AppendChild<Sheets>(new Sheets());
foreach (DataTable dt in ds.Tables)
{
// For each worksheet you want to create
string worksheetName = dt.TableName;
// Create worksheet part, and add it to the sheets collection in workbook
WorksheetPart newWorksheetPart = spreadsheet.WorkbookPart.AddNewPart<WorksheetPart>();
Sheet sheet = new Sheet() { Id = spreadsheet.WorkbookPart.GetIdOfPart(newWorksheetPart), SheetId = worksheetNumber, Name = worksheetName };
sheets.Append(sheet);
// Append this worksheet's data to our Workbook, using OpenXmlWriter, to prevent memory problems
WriteDataTableToExcelWorksheet(dt, newWorksheetPart);
worksheetNumber++;
}
spreadsheet.WorkbookPart.Workbook.Save();
}
示例15: 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);
//.........这里部分代码省略.........