Skip to content

Commit e0546d3

Browse files
committed
.
1 parent 75933d3 commit e0546d3

File tree

3 files changed

+61
-13
lines changed

3 files changed

+61
-13
lines changed

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
[![Build status](https://ci.appveyor.com/api/projects/status/q1eqcnbptyjl24hp?svg=true)](https://ci.appveyor.com/project/SimonCropp/verify-openxml)
55
[![NuGet Status](https://img.shields.io/nuget/v/Verify.OpenXML.svg)](https://www.nuget.org/packages/Verify.OpenXML/)
66

7-
Extends [Verify](https://github.com/VerifyTests/Verify) to allow verification of Excel documents via [OpenXML](https://github.com/dotnet/Open-XML-SDK/).<!-- singleLineInclude: intro. path: /docs/intro.include.md -->
7+
Extends [Verify](https://github.com/VerifyTests/Verify) to allow verification of Excel documents via [OpenXML](https://github.com/dotnet/Open-XML-SDK/).<!-- singleLineInclude: intro. path: /docs/intro.include.md -->
88

99
Converts Excel documents (xlsx) to csv for verification.
1010

src/Verify.OpenXml/VerifyOpenXml.cs

Lines changed: 57 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Argon;
2+
using DocumentFormat.OpenXml;
23

34
namespace VerifyTests;
45

@@ -32,7 +33,10 @@ public static void Initialize()
3233

3334
static ConversionResult Convert(Stream stream, IReadOnlyDictionary<string, object> settings)
3435
{
35-
var document = SpreadsheetDocument.Open(stream, false);
36+
var document = SpreadsheetDocument.Open(stream, false, new()
37+
{
38+
AutoSave = false
39+
});
3640
return Convert(document, settings);
3741
}
3842

@@ -46,13 +50,7 @@ static ConversionResult Convert(SpreadsheetDocument document, IReadOnlyDictionar
4650
CellFormats = document.WorkbookPart!.WorkbookStylesPart?.Stylesheet.CellFormats?.Elements<CellFormat>().ToList()
4751
};
4852

49-
Task Cleanup()
50-
{
51-
document.Dispose();
52-
return Task.CompletedTask;
53-
}
54-
document.Save();
55-
List<Target> targets = [new Target("xlsx", _.Csv, _.Name)];
53+
List<Target> targets = [new("xlsx", CloneToStream(document))];
5654
if (sheets.Count == 1)
5755
{
5856
var (csv, _) = sheets[0];
@@ -63,7 +61,14 @@ Task Cleanup()
6361
targets.AddRange(sheets.Select(_ => new Target("csv", _.Csv, _.Name)));
6462
}
6563

66-
return new(info, targets, Cleanup);
64+
return new(info, targets, () =>
65+
{
66+
if (!document.AutoSave)
67+
{
68+
document.Dispose();
69+
}
70+
return Task.CompletedTask;
71+
});
6772
}
6873

6974
static IEnumerable<(StringBuilder Csv, string? Name)> Convert(SpreadsheetDocument document)
@@ -217,4 +222,47 @@ static string EscapeCsvValue(string value)
217222

218223
return value;
219224
}
225+
226+
227+
public static MemoryStream CloneToStream(SpreadsheetDocument sourceDocument)
228+
{
229+
var memoryStream = new MemoryStream();
230+
using (var targetDocument = SpreadsheetDocument.Create(memoryStream, SpreadsheetDocumentType.Workbook))
231+
{
232+
// Clone the workbook part and its content
233+
var sourceWorkbookPart = sourceDocument.WorkbookPart;
234+
var targetWorkbookPart = targetDocument.AddWorkbookPart();
235+
236+
// Copy the workbook
237+
targetWorkbookPart.Workbook = new Workbook();
238+
targetWorkbookPart.Workbook.InnerXml = sourceWorkbookPart!.Workbook.InnerXml;
239+
240+
// Copy styles if they exist
241+
if (sourceWorkbookPart.WorkbookStylesPart != null)
242+
{
243+
var targetStylesPart = targetWorkbookPart.AddNewPart<WorkbookStylesPart>();
244+
targetStylesPart.Stylesheet = new Stylesheet();
245+
targetStylesPart.Stylesheet.InnerXml = sourceWorkbookPart.WorkbookStylesPart.Stylesheet.InnerXml;
246+
}
247+
248+
// Copy shared strings if they exist
249+
if (sourceWorkbookPart.SharedStringTablePart != null)
250+
{
251+
var targetSharedStringsPart = targetWorkbookPart.AddNewPart<SharedStringTablePart>();
252+
targetSharedStringsPart.SharedStringTable = new SharedStringTable();
253+
targetSharedStringsPart.SharedStringTable.InnerXml = sourceWorkbookPart.SharedStringTablePart.SharedStringTable.InnerXml;
254+
}
255+
256+
// Copy worksheets
257+
foreach (var sourceWorksheetPart in sourceWorkbookPart.WorksheetParts)
258+
{
259+
var targetWorksheetPart = targetWorkbookPart.AddNewPart<WorksheetPart>();
260+
targetWorksheetPart.Worksheet = new Worksheet();
261+
targetWorksheetPart.Worksheet.InnerXml = sourceWorksheetPart.Worksheet.InnerXml;
262+
}
263+
264+
targetDocument.Save();
265+
}
266+
return memoryStream;
267+
}
220268
}

src/nuget.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[Documentation](https://github.com/VerifyTests/Verify.Sylvan.Data.Excel)
22

3-
Extends [Verify](https://github.com/VerifyTests/Verify) to allow verification of Excel documents via [Sylvan.Data.Excel](https://github.com/MarkPflug/Sylvan.Data.Excel/).<!-- singleLineInclude: intro. path: /docs/intro.include.md -->
3+
Extends [Verify](https://github.com/VerifyTests/Verify) to allow verification of Excel documents via [OpenXML](https://github.com/dotnet/Open-XML-SDK/).<!-- singleLineInclude: intro. path: /docs/intro.include.md -->
44

55
**See [Milestones](https://github.com/VerifyTests/Verify.Sylvan.Data.Excel/milestones?state=closed) for release notes.**
66

@@ -10,6 +10,6 @@ Extends [Verify](https://github.com/VerifyTests/Verify) to allow verification of
1010

1111
### Entity Framework Extensions<!-- include: zzz. path: /docs/zzz.include.md -->
1212

13-
[Entity Framework Extensions](https://entityframework-extensions.net/?utm_source=simoncropp&utm_medium=Verify.Sylvan.Data.Excel) is a major sponsor and is proud to contribute to the development this project.
13+
[Entity Framework Extensions](https://entityframework-extensions.net/?utm_source=simoncropp&utm_medium=Verify.OpenXML) is a major sponsor and is proud to contribute to the development this project.
1414

15-
[![Entity Framework Extensions](https://raw.githubusercontent.com/VerifyTests/Verify.Sylvan.Data.Excel/refs/heads/main/docs/zzz.png)](https://entityframework-extensions.net/?utm_source=simoncropp&utm_medium=Verify.Sylvan.Data.Excel)<!-- endInclude -->
15+
[![Entity Framework Extensions](https://raw.githubusercontent.com/VerifyTests/Verify.OpenXML/refs/heads/main/docs/zzz.png)](https://entityframework-extensions.net/?utm_source=simoncropp&utm_medium=Verify.OpenXML)<!-- endInclude -->

0 commit comments

Comments
 (0)