Skip to content

Commit

Permalink
closes #314
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeebowen committed Sep 17, 2024
1 parent 4b78beb commit 5764348
Show file tree
Hide file tree
Showing 20 changed files with 414 additions and 440 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@
static void CreatePresentation(string filepath)
{
// Create a presentation at a specified file path. The presentation document type is pptx, by default.
PresentationDocument presentationDoc = PresentationDocument.Create(filepath, PresentationDocumentType.Presentation);
PresentationPart presentationPart = presentationDoc.AddPresentationPart();
presentationPart.Presentation = new Presentation();
using (PresentationDocument presentationDoc = PresentationDocument.Create(filepath, PresentationDocumentType.Presentation))
{
PresentationPart presentationPart = presentationDoc.AddPresentationPart();
presentationPart.Presentation = new Presentation();

CreatePresentationParts(presentationPart);

//Dispose the presentation handle
presentationDoc.Dispose();
CreatePresentationParts(presentationPart);
}
}

static void CreatePresentationParts(PresentationPart presentationPart)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,19 @@ Namespace CreatePresentationDocument
Class Program
Public Shared Sub Main(ByVal args As String())

Dim filepath As String = "C:\Users\username\Documents\PresentationFromFilename.pptx"
Dim filepath As String = args(0)
CreatePresentation(filepath)

End Sub

Public Shared Sub CreatePresentation(ByVal filepath As String)
' Create a presentation at a specified file path. The presentation document type is pptx, by default.
Dim presentationDoc As PresentationDocument = PresentationDocument.Create(filepath, PresentationDocumentType.Presentation)
Dim presentationPart As PresentationPart = presentationDoc.AddPresentationPart()
presentationPart.Presentation = New Presentation()
Using presentationDoc As PresentationDocument = PresentationDocument.Create(filepath, PresentationDocumentType.Presentation)
Dim presentationPart As PresentationPart = presentationDoc.AddPresentationPart()
presentationPart.Presentation = New Presentation()

CreatePresentationParts(presentationPart)

'Dispose the presentation handle
presentationDoc.Dispose()
CreatePresentationParts(presentationPart)
End Using
End Sub

Private Shared Sub CreatePresentationParts(ByVal presentationPart As PresentationPart)
Expand Down
15 changes: 8 additions & 7 deletions samples/presentation/working_with_presentations/cs/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Presentation;

CreatePresentation(args[0]);

static void CreatePresentation(string filepath)
{
// Create a presentation at a specified file path. The presentation document type is pptx, by default.
PresentationDocument presentationDoc = PresentationDocument.Create(filepath, PresentationDocumentType.Presentation);
PresentationPart presentationPart = presentationDoc.AddPresentationPart();
presentationPart.Presentation = new Presentation();

CreatePresentationParts(presentationPart);
using (PresentationDocument presentationDoc = PresentationDocument.Create(filepath, PresentationDocumentType.Presentation))
{
PresentationPart presentationPart = presentationDoc.AddPresentationPart();
presentationPart.Presentation = new Presentation();

// Dispose the presentation handle.
presentationDoc.Dispose();
CreatePresentationParts(presentationPart);
}
}
static void CreatePresentationParts(PresentationPart presentationPart)
{
Expand Down
14 changes: 7 additions & 7 deletions samples/presentation/working_with_presentations/vb/Program.vb
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ Imports DocumentFormat.OpenXml.Presentation
Module MyModule

Sub Main(args As String())
Dim filepath As String = args(0)
CreatePresentation(filepath)
End Sub

Sub CreatePresentation(ByVal filepath As String)

' Create a presentation at a specified file path. The presentation document type is pptx, by default.
Dim presentationDoc As PresentationDocument = PresentationDocument.Create(filepath, PresentationDocumentType.Presentation)
Dim presentationPart As PresentationPart = presentationDoc.AddPresentationPart()
presentationPart.Presentation = New Presentation()
Using presentationDoc As PresentationDocument = PresentationDocument.Create(filepath, PresentationDocumentType.Presentation)
Dim presentationPart As PresentationPart = presentationDoc.AddPresentationPart()
presentationPart.Presentation = New Presentation()

CreatePresentationPartsFromPresentation(presentationPart)

' Dispose the presentation handle.
presentationDoc.Dispose()
CreatePresentationPartsFromPresentation(presentationPart)
End Using
End Sub
Sub CreatePresentationPartsFromPresentation(ByVal presentationPart As PresentationPart)
Dim slideMasterIdList1 As New SlideMasterIdList(New SlideMasterId() With {
Expand Down
51 changes: 25 additions & 26 deletions samples/spreadsheet/create_by_providing_a_file_name/cs/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,31 @@ static void CreateSpreadsheetWorkbook(string filepath)
// <Snippet1>
// Create a spreadsheet document by supplying the filepath.
// By default, AutoSave = true, Editable = true, and Type = xlsx.
SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create(filepath, SpreadsheetDocumentType.Workbook);
// </Snippet1>

// <Snippet2>
// Add a WorkbookPart to the document.
WorkbookPart workbookPart = spreadsheetDocument.AddWorkbookPart();
workbookPart.Workbook = new Workbook();
// </Snippet2>

// <Snippet3>
// Add a WorksheetPart to the WorkbookPart.
WorksheetPart worksheetPart = workbookPart.AddNewPart<WorksheetPart>();
worksheetPart.Worksheet = new Worksheet(new SheetData());

// Add Sheets to the Workbook.
Sheets sheets = workbookPart.Workbook.AppendChild(new Sheets());

// Append a new worksheet and associate it with the workbook.
Sheet sheet = new Sheet() { Id = workbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = "mySheet" };
sheets.Append(sheet);
// </Snippet3>

workbookPart.Workbook.Save();

// Dispose the document.
spreadsheetDocument.Dispose();
using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create(filepath, SpreadsheetDocumentType.Workbook))
{
// </Snippet1>

// <Snippet2>
// Add a WorkbookPart to the document.
WorkbookPart workbookPart = spreadsheetDocument.AddWorkbookPart();
workbookPart.Workbook = new Workbook();
// </Snippet2>

// <Snippet3>
// Add a WorksheetPart to the WorkbookPart.
WorksheetPart worksheetPart = workbookPart.AddNewPart<WorksheetPart>();
worksheetPart.Worksheet = new Worksheet(new SheetData());

// Add Sheets to the Workbook.
Sheets sheets = workbookPart.Workbook.AppendChild(new Sheets());

// Append a new worksheet and associate it with the workbook.
Sheet sheet = new Sheet() { Id = workbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = "mySheet" };
sheets.Append(sheet);
// </Snippet3>

workbookPart.Workbook.Save();
}
}
// </Snippet0>

Expand Down
58 changes: 28 additions & 30 deletions samples/spreadsheet/create_by_providing_a_file_name/vb/Program.vb
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,34 @@ Module MyModule
' <Snippet1>
' Create a spreadsheet document by supplying the filepath.
' By default, AutoSave = true, Editable = true, and Type = xlsx.
Dim spreadsheetDocument As SpreadsheetDocument = SpreadsheetDocument.Create(filepath, SpreadsheetDocumentType.Workbook)
' </Snippet1>

' <Snippet2>
' Add a WorkbookPart to the document.
Dim workbookpart As WorkbookPart = spreadsheetDocument.AddWorkbookPart
workbookpart.Workbook = New Workbook
' </Snippet2>

' <Snippet3>
' Add a WorksheetPart to the WorkbookPart.
Dim worksheetPart As WorksheetPart = workbookpart.AddNewPart(Of WorksheetPart)()
worksheetPart.Worksheet = New Worksheet(New SheetData())

' Add Sheets to the Workbook.
Dim sheets As Sheets = spreadsheetDocument.WorkbookPart.Workbook.AppendChild(Of Sheets)(New Sheets())

' Append a new worksheet and associate it with the workbook.
Dim sheet As Sheet = New Sheet
sheet.Id = spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart)
sheet.SheetId = 1
sheet.Name = "mySheet"

sheets.Append(sheet)
' </Snippet3>

workbookpart.Workbook.Save()

' Dispose the document.
spreadsheetDocument.Dispose()
Using spreadsheetDocument As SpreadsheetDocument = SpreadsheetDocument.Create(filepath, SpreadsheetDocumentType.Workbook)
' </Snippet1>

' <Snippet2>
' Add a WorkbookPart to the document.
Dim workbookpart As WorkbookPart = spreadsheetDocument.AddWorkbookPart
workbookpart.Workbook = New Workbook
' </Snippet2>

' <Snippet3>
' Add a WorksheetPart to the WorkbookPart.
Dim worksheetPart As WorksheetPart = workbookpart.AddNewPart(Of WorksheetPart)()
worksheetPart.Worksheet = New Worksheet(New SheetData())

' Add Sheets to the Workbook.
Dim sheets As Sheets = spreadsheetDocument.WorkbookPart.Workbook.AppendChild(Of Sheets)(New Sheets())

' Append a new worksheet and associate it with the workbook.
Dim sheet As Sheet = New Sheet
sheet.Id = spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart)
sheet.SheetId = 1
sheet.Name = "mySheet"

sheets.Append(sheet)
' </Snippet3>

workbookpart.Workbook.Save()
End Using
End Sub
' </Snippet0>

Expand Down
74 changes: 38 additions & 36 deletions samples/spreadsheet/open_from_a_stream/cs/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,56 +7,58 @@
static void OpenAndAddToSpreadsheetStream(Stream stream)
{
// Open a SpreadsheetDocument based on a stream.
SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(stream, true);

if (spreadsheetDocument is not null)
using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(stream, true))
{
// Get or create the WorkbookPart
WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart ?? spreadsheetDocument.AddWorkbookPart();

// <Snippet1>

// Add a new worksheet.
WorksheetPart newWorksheetPart = workbookPart.AddNewPart<WorksheetPart>();
newWorksheetPart.Worksheet = new Worksheet(new SheetData());
newWorksheetPart.Worksheet.Save();

// </Snippet1>
if (spreadsheetDocument is not null)
{
// Get or create the WorkbookPart
WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart ?? spreadsheetDocument.AddWorkbookPart();

Workbook workbook = workbookPart.Workbook ?? new Workbook();
// <Snippet1>

if (workbookPart.Workbook is null)
{
workbookPart.Workbook = workbook;
}
// Add a new worksheet.
WorksheetPart newWorksheetPart = workbookPart.AddNewPart<WorksheetPart>();
newWorksheetPart.Worksheet = new Worksheet(new SheetData());
newWorksheetPart.Worksheet.Save();

Sheets sheets = workbook.GetFirstChild<Sheets>() ?? workbook.AppendChild(new Sheets());
string relationshipId = workbookPart.GetIdOfPart(newWorksheetPart);
// </Snippet1>

// Get a unique ID for the new worksheet.
uint sheetId = 1;
Workbook workbook = workbookPart.Workbook ?? new Workbook();

if (sheets.Elements<Sheet>().Count() > 0)
{
sheetId = (sheets.Elements<Sheet>().Select(s => s.SheetId?.Value).Max() + 1) ?? (uint)sheets.Elements<Sheet>().Count() + 1;
}
if (workbookPart.Workbook is null)
{
workbookPart.Workbook = workbook;
}

// Give the new worksheet a name.
string sheetName = "Sheet" + sheetId;
Sheets sheets = workbook.GetFirstChild<Sheets>() ?? workbook.AppendChild(new Sheets());
string relationshipId = workbookPart.GetIdOfPart(newWorksheetPart);

// Append the new worksheet and associate it with the workbook.
Sheet sheet = new Sheet() { Id = relationshipId, SheetId = sheetId, Name = sheetName };
sheets.Append(sheet);
workbookPart.Workbook.Save();
// Get a unique ID for the new worksheet.
uint sheetId = 1;

// Dispose the document handle.
spreadsheetDocument.Dispose();
if (sheets.Elements<Sheet>().Count() > 0)
{
sheetId = (sheets.Elements<Sheet>().Select(s => s.SheetId?.Value).Max() + 1) ?? (uint)sheets.Elements<Sheet>().Count() + 1;
}

// Give the new worksheet a name.
string sheetName = "Sheet" + sheetId;

// Append the new worksheet and associate it with the workbook.
Sheet sheet = new Sheet() { Id = relationshipId, SheetId = sheetId, Name = sheetName };
sheets.Append(sheet);
workbookPart.Workbook.Save();
}
}
}

// </Snippet0>

// <Snippet2>
var fileStream = File.Open(args[0], FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
OpenAndAddToSpreadsheetStream(fileStream);
//Stream fileStream = File.Open(args[0], FileMode.Open);
using (FileStream fileStream = new FileStream(args[0], FileMode.Open, FileAccess.ReadWrite))
{
OpenAndAddToSpreadsheetStream(fileStream);
}
// </Snippet2>
Loading

0 comments on commit 5764348

Please sign in to comment.