Skip to content

Commit 9fca4ab

Browse files
committed
clean up docs\spreadsheet\how-to-open-a-spreadsheet-document-for-read-only-access.md
1 parent f687a69 commit 9fca4ab

File tree

5 files changed

+77
-127
lines changed

5 files changed

+77
-127
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
## The SpreadsheetDocument Object
2+
3+
The basic document structure of a SpreadsheetML document consists of the
4+
<xref:DocumentFormat.OpenXml.Spreadsheet.Sheets> and <xref:DocumentFormat.OpenXml.Spreadsheet.Sheet> elements, which reference the
5+
worksheets in the <xref:DocumentFormat.OpenXml.Spreadsheet.Workbook>. A separate XML file is created
6+
for each <xref:DocumentFormat.OpenXml.Spreadsheet.Worksheet>. For example, the SpreadsheetML
7+
for a workbook that has two worksheets name MySheet1 and MySheet2 is
8+
located in the Workbook.xml file and is as follows.
9+
10+
```xml
11+
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
12+
<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
13+
<sheets>
14+
<sheet name="MySheet1" sheetId="1" r:id="rId1" />
15+
<sheet name="MySheet2" sheetId="2" r:id="rId2" />
16+
</sheets>
17+
</workbook>
18+
```
19+
20+
The worksheet XML files contain one or more block level elements such as
21+
<xref:DocumentFormat.OpenXml.Spreadsheet.SheetData>. `sheetData` represents the cell table and contains
22+
one or more <xref:DocumentFormat.OpenXml.Spreadsheet.Row> elements. A `row` contains one or more <xref:DocumentFormat.OpenXml.Spreadsheet.Cell> elements. Each cell contains a <xref:DocumentFormat.OpenXml.Spreadsheet.CellValue> element that represents the value
23+
of the cell. For example, the SpreadsheetML for the first worksheet in a
24+
workbook, that only has the value 100 in cell A1, is located in the
25+
Sheet1.xml file and is as follows.
26+
27+
```xml
28+
<?xml version="1.0" encoding="UTF-8" ?>
29+
<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
30+
<sheetData>
31+
<row r="1">
32+
<c r="A1">
33+
<v>100</v>
34+
</c>
35+
</row>
36+
</sheetData>
37+
</worksheet>
38+
```
39+
40+
Using the Open XML SDK, you can create document structure and
41+
content that uses strongly-typed classes that correspond to
42+
SpreadsheetML elements. You can find these classes in the `DocumentFormat.OpenXML.Spreadsheet` namespace. The
43+
following table lists the class names of the classes that correspond to
44+
the `workbook`, `sheets`, `sheet`, `worksheet`, and `sheetData` elements.
45+
46+
**SpreadsheetML Element**|**Open XML SDK Class**|**Description**
47+
--|--|--
48+
`<workbook/>`|<xref:DocumentFormat.OpenXml.Spreadsheet.Workbook>|The root element for the main document part.
49+
`<sheets/>`|<xref:DocumentFormat.OpenXml.Spreadsheet.Sheets>|The container for the block level structures such as sheet, fileVersion, and others specified in the [!include[ISO/IEC 29500 URL](../includes/iso-iec-29500-link.md)] specification.
50+
`<sheet/>`|<xref:DocumentFormat.OpenXml.Spreadsheet.Sheet>|A sheet that points to a sheet definition file.
51+
`<worksheet/>`|<xref:DocumentFormat.OpenXml.Spreadsheet.Worksheet>|A sheet definition file that contains the sheet data.
52+
`<sheetData/>`|<xref:DocumentFormat.OpenXml.Spreadsheet.SheetData>|The cell table, grouped together by rows.
53+
`<row/>`|<xref:DocumentFormat.OpenXml.Spreadsheet.Row>|A row in the cell table.
54+
`<c/>`|<xref:DocumentFormat.OpenXml.Spreadsheet.Cell>|A cell in a row.
55+
`<v/>`|<xref:DocumentFormat.OpenXml.Spreadsheet.CellValue>|The value of a cell.

docs/spreadsheet/how-to-open-a-spreadsheet-document-for-read-only-access.md

Lines changed: 17 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ ms.suite: office
1111
ms.author: o365devx
1212
author: o365devx
1313
ms.topic: conceptual
14-
ms.date: 12/12/2023
14+
ms.date: 01/10/2025
1515
ms.localizationpriority: high
1616
---
1717
# Open a spreadsheet document for read-only access
@@ -24,25 +24,29 @@ programmatically.
2424

2525
---------------------------------------------------------------------------------
2626
## When to Open a Document for Read-Only Access
27+
2728
Sometimes you want to open a document to inspect or retrieve some
2829
information, and you want to do this in a way that ensures the document
2930
remains unchanged. In these instances, you want to open the document for
3031
read-only access. This How To topic discusses several ways to
3132
programmatically open a read-only spreadsheet document.
3233

34+
--------------------------------------------------------------------------------
35+
[!include[Spreadsheet Object](../includes/spreadsheet/spreadsheet-object.md)]
3336

3437
--------------------------------------------------------------------------------
3538
## Getting a SpreadsheetDocument Object
39+
3640
In the Open XML SDK, the <xref:DocumentFormat.OpenXml.Packaging.SpreadsheetDocument> class represents an
3741
Excel document package. To create an Excel document, you create an
38-
instance of the **SpreadsheetDocument** class
42+
instance of the `SpreadsheetDocument` class
3943
and populate it with parts. At a minimum, the document must have a
4044
workbook part that serves as a container for the document, and at least
4145
one worksheet part. The text is represented in the package as XML using
4246
SpreadsheetML markup.
4347

4448
To create the class instance from the document that you call one of the
45-
<xref:DocumentFormat.OpenXml.Packaging.SpreadsheetDocument.Open*> overload methods. Several **Open** methods are provided, each with a different
49+
<xref:DocumentFormat.OpenXml.Packaging.SpreadsheetDocument.Open*> overload methods. Several `Open` methods are provided, each with a different
4650
signature. The methods that let you specify whether a document is
4751
editable are listed in the following table.
4852

@@ -53,21 +57,21 @@ Open(Stream, Boolean)|[Open(Stream, Boolean](/dotnet/api/documentformat.openxml.
5357
Open(String, Boolean, OpenSettings)|[Open(String, Boolean, OpenSettings)](/dotnet/api/documentformat.openxml.packaging.spreadsheetdocument.open?view=openxml-3.0.1#documentformat-openxml-packaging-spreadsheetdocument-open(system-string-system-boolean-documentformat-openxml-packaging-opensettings))|Create an instance of the SpreadsheetDocument class from the specified file.
5458
Open(Stream, Boolean, OpenSettings)|[Open(Stream, Boolean, OpenSettings)](/dotnet/api/documentformat.openxml.packaging.spreadsheetdocument.open?view=openxml-3.0.1#documentformat-openxml-packaging-spreadsheetdocument-open(system-io-stream-system-boolean-documentformat-openxml-packaging-opensettings))|Create an instance of the SpreadsheetDocument class from the specified I/O stream.
5559

56-
The table earlier in this topic lists only those **Open** methods that accept a Boolean value as the
60+
The table earlier in this topic lists only those `Open` methods that accept a Boolean value as the
5761
second parameter to specify whether a document is editable. To open a
58-
document for read-only access, specify **False** for this parameter.
62+
document for read-only access, specify `False` for this parameter.
5963

60-
Notice that two of the **Open** methods create
64+
Notice that two of the `Open` methods create
6165
an instance of the SpreadsheetDocument class based on a string as the
6266
first parameter. The first example in the sample code uses this
63-
technique. It uses the first **Open** method in
67+
technique. It uses the first `Open` method in
6468
the table earlier in this topic; with a signature that requires two
6569
parameters. The first parameter takes a string that represents the full
6670
path file name from which you want to open the document. The second
67-
parameter is either **true** or **false**. This example uses **false** and indicates that you want to open the
71+
parameter is either `true` or `false`. This example uses `false` and indicates that you want to open the
6872
file as read-only.
6973

70-
The following code example calls the **Open**
74+
The following code example calls the `Open`
7175
Method.
7276

7377
### [C#](#tab/cs-0)
@@ -78,7 +82,7 @@ Method.
7882
***
7983

8084

81-
The other two **Open** methods create an
85+
The other two `Open` methods create an
8286
instance of the SpreadsheetDocument class based on an input/output
8387
stream. You might use this approach, for example, if you have a
8488
Microsoft SharePoint Foundation 2010 application that uses stream
@@ -99,12 +103,12 @@ Suppose you have an application that uses the Open XML support in the
99103
System.IO.Packaging namespace of the .NET Framework Class Library, and
100104
you want to use the Open XML SDK to work with a package as
101105
read-only. Whereas the Open XML SDK includes method overloads that
102-
accept a **Package** as the first parameter,
106+
accept a `Package` as the first parameter,
103107
there is not one that takes a Boolean as the second parameter to
104108
indicate whether the document should be opened for editing.
105109

106110
The recommended method is to open the package as read-only at first,
107-
before creating the instance of the **SpreadsheetDocument** class, as shown in the second
111+
before creating the instance of the `SpreadsheetDocument` class, as shown in the second
108112
example in the sample code. The following code example performs this
109113
operation.
110114

@@ -115,62 +119,7 @@ operation.
115119
[!code-vb[](../../samples/spreadsheet/open_for_read_only_access/vb/Program.vb#snippet3)]
116120

117121
---------------------------------------------------------------------------------
118-
## Basic Document Structure
119-
The basic document structure of a SpreadsheetML document consists of the
120-
<xref:DocumentFormat.OpenXml.Spreadsheet.Sheets> and <xref:DocumentFormat.OpenXml.Spreadsheet.Sheet> elements, which reference the
121-
worksheets in the <xref:DocumentFormat.OpenXml.Spreadsheet.Workbook>. A separate XML file is created
122-
for each <xref:DocumentFormat.OpenXml.Spreadsheet.Worksheet>. For example, the SpreadsheetML
123-
for a workbook that has two worksheets name MySheet1 and MySheet2 is
124-
located in the Workbook.xml file and is as follows.
125-
126-
```xml
127-
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
128-
<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
129-
<sheets>
130-
<sheet name="MySheet1" sheetId="1" r:id="rId1" />
131-
<sheet name="MySheet2" sheetId="2" r:id="rId2" />
132-
</sheets>
133-
</workbook>
134-
```
135-
136-
The worksheet XML files contain one or more block level elements such as
137-
<xref:DocumentFormat.OpenXml.Spreadsheet.SheetData>. **sheetData** represents the cell table and contains
138-
one or more <xref:DocumentFormat.OpenXml.Spreadsheet.Row> elements. A **row** contains one or more <xref:DocumentFormat.OpenXml.Spreadsheet.Cell> elements. Each cell contains a <xref:DocumentFormat.OpenXml.Spreadsheet.CellValue> element that represents the value
139-
of the cell. For example, the SpreadsheetML for the first worksheet in a
140-
workbook, that only has the value 100 in cell A1, is located in the
141-
Sheet1.xml file and is as follows.
142-
143-
```xml
144-
<?xml version="1.0" encoding="UTF-8" ?>
145-
<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
146-
<sheetData>
147-
<row r="1">
148-
<c r="A1">
149-
<v>100</v>
150-
</c>
151-
</row>
152-
</sheetData>
153-
</worksheet>
154-
```
155-
156-
Using the Open XML SDK, you can create document structure and
157-
content that uses strongly-typed classes that correspond to
158-
SpreadsheetML elements. You can find these classes in the **DocumentFormat.OpenXML.Spreadsheet** namespace. The
159-
following table lists the class names of the classes that correspond to
160-
the **workbook**, **sheets**, **sheet**, **worksheet**, and **sheetData** elements.
161-
162-
SpreadsheetML Element|Open XML SDK Class|Description
163-
--|--|--
164-
workbook|DocumentFormat.OpenXml.Spreadsheet.Workbook|The root element for the main document part.
165-
sheets|DocumentFormat.OpenXml.Spreadsheet.Sheets|The container for the block level structures such as sheet, fileVersion, and others specified in the [!include[ISO/IEC 29500 URL](../includes/iso-iec-29500-link.md)] specification.
166-
sheet|DocumentFormat.OpenXml.Spreadsheet.Sheet|A sheet that points to a sheet definition file.
167-
worksheet|DocumentFormat.OpenXml.Spreadsheet.Worksheet|A sheet definition file that contains the sheet data.
168-
sheetData|DocumentFormat.OpenXml.Spreadsheet.SheetData|The cell table, grouped together by rows.
169-
row|DocumentFormat.OpenXml.Spreadsheet.Row|A row in the cell table.
170-
c|DocumentFormat.OpenXml.Spreadsheet.Cell|A cell in a row.
171-
v|DocumentFormat.OpenXml.Spreadsheet.CellValue|The value of a cell.
172122

173-
--------------------------------------------------------------------------------
174123
## Sample Code
175124

176125
The following is the complete sample code in both C\# and Visual Basic.
@@ -180,6 +129,7 @@ The following is the complete sample code in both C\# and Visual Basic.
180129

181130
### [Visual Basic](#tab/vb)
182131
[!code-vb[](../../samples/spreadsheet/open_for_read_only_access/vb/Program.vb#snippet0)]
132+
***
183133

184134
--------------------------------------------------------------------------------
185135
## See also

docs/spreadsheet/how-to-open-a-spreadsheet-document-from-a-stream.md

Lines changed: 1 addition & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -36,63 +36,8 @@ Open XML SDK.
3636

3737

3838
--------------------------------------------------------------------------------
39-
## The SpreadsheetDocument Object
40-
41-
The basic document structure of a SpreadsheetML document consists of the
42-
<xref:DocumentFormat.OpenXml.Spreadsheet.Sheets> and <xref:DocumentFormat.OpenXml.Spreadsheet.Sheet> elements, which reference the
43-
worksheets in the workbook. A separate XML file is created for each
44-
worksheet. For example, the SpreadsheetML for a workbook that has two
45-
worksheets name MySheet1 and MySheet2 is located in the Workbook.xml
46-
file and is shown in the following code example.
47-
48-
```xml
49-
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
50-
<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
51-
<sheets>
52-
<sheet name="MySheet1" sheetId="1" r:id="rId1" />
53-
<sheet name="MySheet2" sheetId="2" r:id="rId2" />
54-
</sheets>
55-
</workbook>
56-
```
57-
58-
The worksheet XML files contain one or more block level elements such as
59-
<xref:DocumentFormat.OpenXml.Spreadsheet.SheetData>. **sheetData** represents the cell table and contains
60-
one or more <xref:DocumentFormat.OpenXml.Spreadsheet.Row> elements. A **row** contains one or more <xref:DocumentFormat.OpenXml.Spreadsheet.Cell> elements. Each cell contains a <xref:DocumentFormat.OpenXml.Spreadsheet.CellValue> element that represents the value
61-
of the cell. For example, the **SpreadsheetML**
62-
for the first worksheet in a workbook, that only has the value 100 in
63-
cell A1, is located in the Sheet1.xml file and is shown in the following
64-
code example.
65-
66-
```xml
67-
<?xml version="1.0" encoding="UTF-8" ?>
68-
<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
69-
<sheetData>
70-
<row r="1">
71-
<c r="A1">
72-
<v>100</v>
73-
</c>
74-
</row>
75-
</sheetData>
76-
</worksheet>
77-
```
78-
79-
Using the Open XML SDK, you can create document structure and
80-
content that uses strongly-typed classes that correspond to
81-
SpreadsheetML elements. You can find these classes in the **DocumentFormat.OpenXML.Spreadsheet** namespace. The
82-
following table lists the class names of the classes that correspond to
83-
the **workbook**, **sheets**, **sheet**, **worksheet**, and **sheetData** elements.
84-
85-
SpreadsheetML Element|Open XML SDK Class|Description
86-
--|--|--
87-
workbook|DocumentFormat.OpenXml.Spreadsheet.Workbook|The root element for the main document part.
88-
sheets|DocumentFormat.OpenXml.Spreadsheet.Sheets|The container for the block level structures such as sheet, fileVersion, and others specified in the [!include[ISO/IEC 29500 URL](../includes/iso-iec-29500-link.md)] specification.
89-
sheet|DocumentFormat.OpenXml.Spreadsheet.Sheet|A sheet that points to a sheet definition file.
90-
worksheet|DocumentFormat.OpenXml.Spreadsheet.Worksheet|A sheet definition file that contains the sheet data.
91-
sheetData|DocumentFormat.OpenXml.Spreadsheet.SheetData|The cell table, grouped together by rows.
92-
row|DocumentFormat.OpenXml.Spreadsheet.Row|A row in the cell table.
93-
c|DocumentFormat.OpenXml.Spreadsheet.Cell|A cell in a row.
94-
v|DocumentFormat.OpenXml.Spreadsheet.CellValue|The value of a cell.
9539

40+
[!include[Spreadsheet Object](../includes/spreadsheet/spreadsheet-object.md)]
9641

9742
--------------------------------------------------------------------------------
9843
## Generating the SpreadsheetML Markup to Add a Worksheet

samples/spreadsheet/open_for_read_only_access/cs/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
// <Snippet0>
21
using DocumentFormat.OpenXml.Packaging;
32
using System.IO;
43
using System.IO.Packaging;
54

5+
// <Snippet0>
66
static void OpenSpreadsheetDocumentReadonly(string filePath)
77
{
88
// <Snippet1>

samples/spreadsheet/open_for_read_only_access/vb/Program.vb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
' <Snippet0>
21
Imports System.IO
32
Imports System.IO.Packaging
43
Imports DocumentFormat.OpenXml.Packaging
@@ -9,6 +8,7 @@ Module Program
98

109

1110

11+
' <Snippet0>
1212
Public Sub OpenSpreadsheetDocumentReadOnly(ByVal filePath As String)
1313
' <Snippet1>
1414
' Open a SpreadsheetDocument based on a file path.
@@ -51,5 +51,5 @@ Module Program
5151
' The rest of the code will not be called.
5252
End Using
5353
End Sub
54-
End Module
55-
' </Snippet0>
54+
' </Snippet0>
55+
End Module

0 commit comments

Comments
 (0)