|
| 1 | +--- |
| 2 | +api_name: |
| 3 | +- Microsoft.Office.DocumentFormat.OpenXML.Packaging |
| 4 | +api_type: |
| 5 | +- schema |
| 6 | +ms.assetid: 2ad4855c-1c83-4dab-b93f-2bae13fac644 |
| 7 | +title: 'How to: Copy a Worksheet Using SAX (Simple API for XML)' |
| 8 | +ms.suite: office |
| 9 | + |
| 10 | +ms.author: o365devx |
| 11 | +author: o365devx |
| 12 | +ms.topic: conceptual |
| 13 | +ms.date: 04/01/2025 |
| 14 | +ms.localizationpriority: high |
| 15 | +--- |
| 16 | +# Copy a Worksheet Using SAX (Simple API for XML) |
| 17 | + |
| 18 | +This topic shows how to use the the Open XML SDK for Office to programmatically copy a large worksheet |
| 19 | +using SAX (Simple API for XML). For more information about the basic structure of a `SpreadsheetML` |
| 20 | +document, see [Structure of a SpreadsheetML document](structure-of-a-spreadsheetml-document.md). |
| 21 | + |
| 22 | +------------------------------------ |
| 23 | +## Why Use the SAX Approach? |
| 24 | + |
| 25 | +The Open XML SDK provides two ways to parse Office Open XML files: the Document Object Model (DOM) and |
| 26 | +the Simple API for XML (SAX). The DOM approach is designed to make it easy to query and parse Open XML |
| 27 | +files by using strongly-typed classes. However, the DOM approach requires loading entire Open XML parts into |
| 28 | +memory, which can lead to slower processing and `Out of Memory` exceptions when working with very large parts. |
| 29 | +The SAX approach reads in the XML in an Open XML part one element at a time without reading in the entire part |
| 30 | +into memory giving noncached, forward-only access to XML data, which makes it a better choice when reading |
| 31 | +very large parts, such as a <xref:DocumentFormat.OpenXml.Packaging.WorksheetPart> with hundreds of thousands of rows. |
| 32 | + |
| 33 | +## Using the DOM Approach |
| 34 | + |
| 35 | +Using the DOM approach, we can take advantage of the Open XML SDK's strongly typed classes. The first step |
| 36 | +is to access the package's `WorksheetPart` and make sure that it is not null. |
| 37 | + |
| 38 | +### [C#](#tab/cs-1) |
| 39 | +[!code-csharp[](../../samples/spreadsheet/copy_worksheet_with_sax/cs/Program.cs#snippet1)] |
| 40 | + |
| 41 | +### [Visual Basic](#tab/vb-1) |
| 42 | +[!code-vb[](../../samples/spreadsheet/copy_worksheet_with_sax/vb/Program.vb#snippet1)] |
| 43 | +*** |
| 44 | + |
| 45 | +Once it is determined that the `WorksheetPart` to be copied is not null, add a new `WorksheetPart` to copy it to. |
| 46 | +Then clone the `WorksheetPart`'s <xref:DocumentFormat.OpenXml.Spreadsheet.Worksheet> and assign the cloned |
| 47 | +`Worksheet` to the new `WorksheetPart`'s Worksheet property. |
| 48 | + |
| 49 | +### [C#](#tab/cs-2) |
| 50 | +[!code-csharp[](../../samples/spreadsheet/copy_worksheet_with_sax/cs/Program.cs#snippet2)] |
| 51 | + |
| 52 | +### [Visual Basic](#tab/vb-2) |
| 53 | +[!code-vb[](../../samples/spreadsheet/copy_worksheet_with_sax/vb/Program.vb#snippet2)] |
| 54 | +*** |
| 55 | + |
| 56 | +At this point, the new `WorksheetPart` has been added, but a new <xref:DocumentFormat.OpenXml.Spreadsheet.Sheet> |
| 57 | +element must be added to the `WorkbookPart`'s <xref:DocumentFormat.OpenXml.Spreadsheet.Sheets>'s |
| 58 | +child elements for it to display. To do this, first find the new `WorksheetPart`'s Id and |
| 59 | +create a new sheet Id by incrementing the `Sheets` count by one then append a new `Sheet` |
| 60 | +child to the `Sheets` element. With this, the copied Worksheet is added to the file. |
| 61 | + |
| 62 | +### [C#](#tab/cs-3) |
| 63 | +[!code-csharp[](../../samples/spreadsheet/copy_worksheet_with_sax/cs/Program.cs#snippet3)] |
| 64 | + |
| 65 | +### [Visual Basic](#tab/vb-3) |
| 66 | +[!code-vb[](../../samples/spreadsheet/copy_worksheet_with_sax/vb/Program.vb#snippet3)] |
| 67 | +*** |
| 68 | + |
| 69 | +## Using the SAX Approach |
| 70 | + |
| 71 | +The SAX approach works on parts, so using the SAX approach, the first step is the same. |
| 72 | +Access the package's <xref:DocumentFormat.OpenXml.Packaging.WorksheetPart> and make sure |
| 73 | +that it is not null. |
| 74 | + |
| 75 | +### [C#](#tab/cs-4) |
| 76 | +[!code-csharp[](../../samples/spreadsheet/copy_worksheet_with_sax/cs/Program.cs#snippet4)] |
| 77 | + |
| 78 | +### [Visual Basic](#tab/vb-4) |
| 79 | +[!code-vb[](../../samples/spreadsheet/copy_worksheet_with_sax/vb/Program.vb#snippet4)] |
| 80 | +*** |
| 81 | + |
| 82 | +With SAX, we don't have access to the <xref:DocumentFormat.OpenXml.OpenXmlElement.Clone*> |
| 83 | +method. So instead, start by adding a new `WorksheetPart` to the `WorkbookPart`. |
| 84 | + |
| 85 | +### [C#](#tab/cs-5) |
| 86 | +[!code-csharp[](../../samples/spreadsheet/copy_worksheet_with_sax/cs/Program.cs#snippet5)] |
| 87 | + |
| 88 | +### [Visual Basic](#tab/vb-5) |
| 89 | +[!code-vb[](../../samples/spreadsheet/copy_worksheet_with_sax/vb/Program.vb#snippet5)] |
| 90 | +*** |
| 91 | + |
| 92 | +Then create an instance of the <xref:DocumentFormat.OpenXml.OpenXmlPartReader> with the |
| 93 | +original worksheet part and an instance of the <xref:DocumentFormat.OpenXml.OpenXmlPartWriter> |
| 94 | +with the newly created worksheet part. |
| 95 | + |
| 96 | +### [C#](#tab/cs-6) |
| 97 | +[!code-csharp[](../../samples/spreadsheet/copy_worksheet_with_sax/cs/Program.cs#snippet6)] |
| 98 | + |
| 99 | +### [Visual Basic](#tab/vb-6) |
| 100 | +[!code-vb[](../../samples/spreadsheet/copy_worksheet_with_sax/vb/Program.vb#snippet6)] |
| 101 | +*** |
| 102 | + |
| 103 | +Then read the elements one by one with the <xref:DocumentFormat.OpenXml.OpenXmlPartReader.Read*> |
| 104 | +method. If the element is a <xref:DocumentFormat.OpenXml.Spreadsheet.CellValue> the inner text |
| 105 | +needs to be explicitly added using the <xref:DocumentFormat.OpenXml.OpenXmlPartReader.GetText*> |
| 106 | +method to read the text, because the <xref:DocumentFormat.OpenXml.OpenXmlPartWriter.WriteStartElement*> |
| 107 | +does not write the inner text of an element. For other elements we only need to use the `WriteStartElement` |
| 108 | +method, because we don't need the other element's inner text. |
| 109 | + |
| 110 | +### [C#](#tab/cs-7) |
| 111 | +[!code-csharp[](../../samples/spreadsheet/copy_worksheet_with_sax/cs/Program.cs#snippet7)] |
| 112 | + |
| 113 | +### [Visual Basic](#tab/vb-7) |
| 114 | +[!code-vb[](../../samples/spreadsheet/copy_worksheet_with_sax/vb/Program.vb#snippet7)] |
| 115 | +*** |
| 116 | + |
| 117 | +At this point, the worksheet part has been copied to the newly added part, but as with the DOM |
| 118 | +approach, we still need to add a `Sheet` to the `Workbook`'s `Sheets` element. Because |
| 119 | +the SAX approach gives noncached, **forward-only** access to XML data, it is only possible to |
| 120 | +prepend element children, which in this case would add the new worksheet to the beginning instead |
| 121 | +of the end, changing the order of the worksheets. So the DOM approach is |
| 122 | +necessary here, because we want to append not prepend the new `Sheet` and since the `WorkbookPart` is |
| 123 | +not usually a large part, the performance gains would be minimal. |
| 124 | + |
| 125 | +### [C#](#tab/cs-8) |
| 126 | +[!code-csharp[](../../samples/spreadsheet/copy_worksheet_with_sax/cs/Program.cs#snippet8)] |
| 127 | + |
| 128 | +### [Visual Basic](#tab/vb-8) |
| 129 | +[!code-vb[](../../samples/spreadsheet/copy_worksheet_with_sax/vb/Program.vb#snippet8)] |
| 130 | +*** |
| 131 | + |
| 132 | +## Sample Code |
| 133 | + |
| 134 | +Below is the sample code for both the DOM and SAX approaches to copying the data from one sheet |
| 135 | +to a new one and adding it to the Spreadsheet document. While the DOM approach is simpler |
| 136 | +and in many cases the preferred choice, with very large documents the SAX approach is better |
| 137 | +given that it is faster and can prevent `Out of Memory` exceptions. To see the difference, |
| 138 | +create a spreadsheet document with many (10,000+) rows and check the results of the |
| 139 | +<xref:System.Diagnostics.Stopwatch> to check the difference in execution time. Increase the |
| 140 | +number of rows to 100,000+ to see even more significant performance gains. |
| 141 | + |
| 142 | +### DOM Approach |
| 143 | + |
| 144 | +### [C#](#tab/cs-0) |
| 145 | +[!code-csharp[](../../samples/spreadsheet/copy_worksheet_with_sax/cs/Program.cs#snippet0)] |
| 146 | + |
| 147 | +### [Visual Basic](#tab/vb-0) |
| 148 | +[!code-vb[](../../samples/spreadsheet/copy_worksheet_with_sax/vb/Program.vb#snippet0)] |
| 149 | +*** |
| 150 | + |
| 151 | +### SAX Approach |
| 152 | + |
| 153 | +### [C#](#tab/cs-99) |
| 154 | +[!code-csharp[](../../samples/spreadsheet/copy_worksheet_with_sax/cs/Program.cs#snippet99)] |
| 155 | + |
| 156 | +### [Visual Basic](#tab/vb-99) |
| 157 | +[!code-vb[](../../samples/spreadsheet/copy_worksheet_with_sax/vb/Program.vb#snippet99)] |
| 158 | +*** |
0 commit comments