Skip to content

Commit

Permalink
closes OfficeDev#277
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeebowen committed Dec 6, 2023
1 parent b729aa5 commit 5e1d51d
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,7 @@ This topic shows how to use the classes in the Open XML SDK for
Office to delete text from a cell in a spreadsheet document
programmatically.




## Get a SpreadsheetDocument object

In the Open XML SDK, the [SpreadsheetDocument](https://msdn.microsoft.com/library/office/documentformat.openxml.packaging.spreadsheetdocument.aspx) class represents an Excel document package. To open and work with an Excel document, create an instance of the **SpreadsheetDocument** class from the document. After you create the instance from the document, obtain access to the main workbook part that contains the worksheets. The text in the document is represented in the package as XML using **SpreadsheetML** markup.
[!include[Structure](../includes/spreadsheet/spreadsheet-document-object.md)]

To create the class instance from the document, call one of the [Open()](https://msdn.microsoft.com/library/office/documentformat.openxml.packaging.spreadsheetdocument.open.aspx) methods. Several are provided, each with a different signature. The sample code in this topic uses the [Open(String, Boolean)](https://msdn.microsoft.com/library/office/cc562356.aspx) method with a signature that requires two parameters. The first parameter takes a full path string that represents the document that you want to open. The second parameter is either **true** or **false** and represents whether you want the file to be opened for editing. Any changes that you make to the document will not be saved if this parameter is **false**.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ ms.suite: office
ms.author: o365devx
author: o365devx
ms.topic: conceptual
ms.date: 11/01/2017
ms.date: 12/05/2023
ms.localizationpriority: high
---
# Open a spreadsheet document from a stream
Expand All @@ -36,59 +36,7 @@ Open XML SDK.


--------------------------------------------------------------------------------
## Getting a SpreadsheetDocument Object

In the Open XML SDK, the [SpreadsheetDocument](https://msdn.microsoft.com/library/office/documentformat.openxml.packaging.spreadsheetdocument.aspx) class represents an
Excel document package. To open and work with an Excel document, you
create an instance of the **SpreadsheetDocument** class from the document.
After you create the instance from the document, you can then obtain
access to the main workbook part that contains the worksheets. The text
in the document is represented in the package as XML using SpreadsheetML
markup.

To create the class instance from the document, you call one of the
[Open()](https://msdn.microsoft.com/library/office/documentformat.openxml.packaging.spreadsheetdocument.open.aspx) methods. Several are provided, each
with a different signature. The sample code in this topic uses the [Open(String, Boolean)](https://msdn.microsoft.com/library/office/cc562356.aspx) method with a
signature that requires two parameters. The first parameter takes a full
path string that represents the document that you want to open. The
second parameter is either **true** or **false** and represents whether you want the file to
be opened for editing. Any changes that you make to the document will
not be saved if this parameter is **false**.

The code that calls the **Open** method is
shown in the following example.

### [C#](#tab/cs-0)
```csharp
// Open the document for editing.
using (SpreadsheetDocument document = SpreadsheetDocument.Open(docName, true))
```

### [Visual Basic](#tab/vb-0)
```vb
' Open the document for editing.
Using document As SpreadsheetDocument = SpreadsheetDocument.Open(docName, True)
```
***


After you have opened the spreadsheet document package, you can add a
row to a sheet in the workbook. Each workbook has a workbook part and at
least one [Worksheet](https://msdn.microsoft.com/library/office/documentformat.openxml.spreadsheet.worksheet.aspx). To access the [Workbook](https://msdn.microsoft.com/library/office/documentformat.openxml.spreadsheet.workbook.aspx) assign a reference to the existing
document body, represented by the [WorkbookPart](https://msdn.microsoft.com/library/office/documentformat.openxml.spreadsheet.workbook.workbookpart.aspx), as shown in the following
code example.

### [C#](#tab/cs-1)
```csharp
WorkbookPart wbPart = document.WorkbookPart;
```

### [Visual Basic](#tab/vb-1)
```vb
Dim wbPart As WorkbookPart = document.WorkbookPart
```
***

## The a SpreadsheetDocument Object

The basic document structure of a SpreadsheetML document consists of the
[Sheets](https://msdn.microsoft.com/library/office/documentformat.openxml.spreadsheet.sheets.aspx) and [Sheet](https://msdn.microsoft.com/library/office/documentformat.openxml.spreadsheet.sheet.aspx) elements, which reference the
Expand Down Expand Up @@ -154,47 +102,24 @@ create a new [WorksheetPart](https://msdn.microsoft.com/library/office/documentf
adds the new **WorksheetPart**.

### [C#](#tab/cs-2)
```csharp
// Add a new worksheet.
WorksheetPart newWorksheetPart = spreadsheetDocument.WorkbookPart.AddNewPart<WorksheetPart>();
newWorksheetPart.Worksheet = new Worksheet(new SheetData());
newWorksheetPart.Worksheet.Save();
```
[!code-csharp[](../../samples/spreadsheet/open_from_a_stream/cs/Program.cs#snippet1)]

### [Visual Basic](#tab/vb-2)
```vb
' Add a new worksheet.
Dim newWorksheetPart As WorksheetPart = spreadsheetDocument.WorkbookPart.AddNewPart(Of WorksheetPart)()
newWorksheetPart.Worksheet = New Worksheet(New SheetData())
newWorksheetPart.Worksheet.Save()
```
[!code-vb[](../../samples/spreadsheet/open_from_a_stream/vb/Program.vb#snippet1)]
***


--------------------------------------------------------------------------------
## Sample Code
In this example, the **OpenAndAddToSpreadsheetStream** method can be used
to open a spreadsheet document from an already open stream and append
some text to it. In your program, you can use the following example to
call the **OpenAndAddToSpreadsheetStream**
method that uses a file named Sheet11.xslx.
some text to it. The following is the complete sample code in both C\# and Visual Basic.

### [C#](#tab/cs-3)
```csharp
string strDoc = @"C:\Users\Public\Documents\Sheet11.xlsx";
;
Stream stream = File.Open(strDoc, FileMode.Open);
OpenAndAddToSpreadsheetStream(stream);
stream.Close();
```
[!code-csharp[](../../samples/spreadsheet/open_from_a_stream/cs/Program.cs#snippet2)]

### [Visual Basic](#tab/vb-3)
```vb
Dim strDoc As String = "C:\Users\Public\Documents\Sheet11.xlsx"
Dim stream As Stream = File.Open(strDoc, FileMode.Open)
OpenAndAddToSpreadsheetStream(stream)
stream.Close()
```
[!code-vb[](../../samples/spreadsheet/open_from_a_stream/vb/Program.vb#snippet2)]
***


Expand All @@ -204,10 +129,10 @@ close the stream passed to it. The calling code must do that.
The following is the complete sample code in both C\# and Visual Basic.

### [C#](#tab/cs)
[!code-csharp[](../../samples/spreadsheet/open_from_a_stream/cs/Program.cs)]
[!code-csharp[](../../samples/spreadsheet/open_from_a_stream/cs/Program.cs#snippet0)]

### [Visual Basic](#tab/vb)
[!code-vb[](../../samples/spreadsheet/open_from_a_stream/vb/Program.vb)]
[!code-vb[](../../samples/spreadsheet/open_from_a_stream/vb/Program.vb#snippet0)]

--------------------------------------------------------------------------------
## See also
Expand Down
17 changes: 13 additions & 4 deletions samples/spreadsheet/open_from_a_stream/cs/Program.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
// <Snippet0>
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
using System.IO;
using System.Linq;

FileStream fileStream = new(args[0], FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
OpenAndAddToSpreadsheetStream(fileStream);

static void OpenAndAddToSpreadsheetStream(Stream stream)
{
// Open a SpreadsheetDocument based on a stream.
Expand All @@ -16,10 +14,14 @@ static void OpenAndAddToSpreadsheetStream(Stream stream)
// 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>

Workbook workbook = workbookPart.Workbook ?? new Workbook();

Expand Down Expand Up @@ -50,4 +52,11 @@ static void OpenAndAddToSpreadsheetStream(Stream stream)
// Dispose the document handle.
spreadsheetDocument.Dispose();
}
}
}

// </Snippet0>

// <Snippet2>
var fileStream = File.Open(args[0], FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
OpenAndAddToSpreadsheetStream(fileStream);
// </Snippet2>
15 changes: 14 additions & 1 deletion samples/spreadsheet/open_from_a_stream/vb/Program.vb
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
' <Snippet0>
Imports System.IO
Imports DocumentFormat.OpenXml.Packaging
Imports DocumentFormat.OpenXml.Spreadsheet

Module Program
Sub Main(args As String())

' <Snippet2>
Dim fileStream As FileStream = New FileStream(args(0), FileMode.OpenOrCreate, FileAccess.ReadWrite)
OpenAndAddToSpreadsheetStream(fileStream)
' </Snippet2>

End Sub


Expand All @@ -12,11 +19,15 @@ Module Program
' Open a SpreadsheetDocument based on a stream.
Dim mySpreadsheetDocument As SpreadsheetDocument = SpreadsheetDocument.Open(stream, True)

' <Snippet1>

' Add a new worksheet.
Dim newWorksheetPart As WorksheetPart = mySpreadsheetDocument.WorkbookPart.AddNewPart(Of WorksheetPart)()
newWorksheetPart.Worksheet = New Worksheet(New SheetData())
newWorksheetPart.Worksheet.Save()

' </Snippet1>

Dim sheets As Sheets = mySpreadsheetDocument.WorkbookPart.Workbook.GetFirstChild(Of Sheets)()
Dim relationshipId As String = mySpreadsheetDocument.WorkbookPart.GetIdOfPart(newWorksheetPart)

Expand All @@ -42,4 +53,6 @@ Module Program

'Caller must close the stream.
End Sub
End Module
End Module

' </Snippet0>

0 comments on commit 5e1d51d

Please sign in to comment.