Skip to content

Commit

Permalink
Put AppVersion 15.0 in docProps/app.xml like LibreOffice Calc.
Browse files Browse the repository at this point in the history
  • Loading branch information
Salvo Isaja committed Mar 4, 2023
1 parent 4b28a19 commit 81c65aa
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/LargeXlsx/LargeXlsx.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>1.7.1</Version>
<Version>1.7.2</Version>
<Authors>Salvo Isaja</Authors>
<Company>Salvo Isaja</Company>
<Copyright>Copyright 2020-2023 Salvatore Isaja</Copyright>
Expand Down
51 changes: 28 additions & 23 deletions src/LargeXlsx/XlsxWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
using System.Linq;
using System.Reflection;
using System.Text;
using System.Xml.Xsl;
using System.Xml;
using SharpCompress.Common;
using SharpCompress.Compressors.Deflate;
using SharpCompress.Writers;
Expand Down Expand Up @@ -74,36 +72,34 @@ public void Dispose()
_currentWorksheet?.Dispose();
_stylesheet.Save(_zipWriter);
_sharedStringTable.Save(_zipWriter);
WriteAppProps();
Save();
SaveDocProps();
SaveContentTypes();
SaveRels();
SaveWorkbook();
SaveWorkbookRels();
_zipWriter.Dispose();
_disposed = true;
}
}

const string PropNS = "http://schemas.openxmlformats.org/officeDocument/2006/extended-properties";

void WriteAppProps()
private void SaveDocProps()
{
// write the assembly name and version to the app.xml.
using (var appStream = _zipWriter.WriteToStream("docProps/app.xml", new ZipWriterEntryOptions()))
using (var xw = XmlWriter.Create(appStream)) {
xw.WriteStartElement("Properties", PropNS);
var asmName = Assembly.GetExecutingAssembly().GetName();
xw.WriteStartElement("Application", PropNS);
xw.WriteValue(asmName.Name);
xw.WriteEndElement();
xw.WriteStartElement("AppVersion", PropNS);
var v = asmName.Version;
// AppVersion must be of the format XX.YYYY
var ver = $"{v.Major:00}.{v.Minor:00}{v.Build:00}";
xw.WriteValue(ver);
xw.WriteEndElement();
xw.WriteEndElement();
var assemblyName = Assembly.GetExecutingAssembly().GetName();
using (var stream = _zipWriter.WriteToStream("docProps/app.xml", new ZipWriterEntryOptions()))
using (var streamWriter = new StreamWriter(stream, Encoding.UTF8))
{
// Looks some applications (e.g. Microsoft's) may consider a file invalid if a specific version number is not found.
// Thus, pretend being version 15.0 like LibreOffice Calc does.
// https://bugs.documentfoundation.org/show_bug.cgi?id=91064
streamWriter.Write("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"
+ "<Properties xmlns=\"http://schemas.openxmlformats.org/officeDocument/2006/extended-properties\">"
+ $"<Application>{Util.EscapeXmlText(assemblyName.Name)}/{assemblyName.Version.Major}.{assemblyName.Version.Minor}.{assemblyName.Version.Build}</Application>"
+ "<AppVersion>15.0000</AppVersion>"
+ "</Properties>");
}
}

private void Save()
private void SaveContentTypes()
{
using (var stream = _zipWriter.WriteToStream("[Content_Types].xml", new ZipWriterEntryOptions()))
using (var streamWriter = new StreamWriter(stream, Encoding.UTF8))
Expand All @@ -124,7 +120,10 @@ private void Save()
+ "<Override PartName=\"/docProps/app.xml\" ContentType=\"application/vnd.openxmlformats-officedocument.extended-properties+xml\"/>"
+ "</Types>");
}
}

private void SaveRels()
{
using (var stream = _zipWriter.WriteToStream("_rels/.rels", new ZipWriterEntryOptions()))
using (var streamWriter = new StreamWriter(stream, Encoding.UTF8))
{
Expand All @@ -134,7 +133,10 @@ private void Save()
+ "<Relationship Id=\"app\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties\" Target=\"docProps/app.xml\"/>"
+ "</Relationships>");
}
}

private void SaveWorkbook()
{
using (var stream = _zipWriter.WriteToStream("xl/workbook.xml", new ZipWriterEntryOptions()))
using (var streamWriter = new StreamWriter(stream, Encoding.UTF8))
{
Expand All @@ -157,7 +159,10 @@ private void Save()
if (_hasFormulasWithoutResult) streamWriter.Write("<calcPr calcCompleted=\"0\" fullCalcOnLoad=\"1\"/>");
streamWriter.Write("</workbook>");
}
}

private void SaveWorkbookRels()
{
using (var stream = _zipWriter.WriteToStream("xl/_rels/workbook.xml.rels", new ZipWriterEntryOptions()))
using (var streamWriter = new StreamWriter(stream, Encoding.UTF8))
{
Expand Down
2 changes: 1 addition & 1 deletion tests/LargeXlsx.Tests/LargeXlsx.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<PackageReference Include="EPPlus" Version="4.5.3.3" />
<PackageReference Include="FluentAssertions" Version="6.10.0" />
<PackageReference Include="nunit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.4.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.4.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
</ItemGroup>

Expand Down

0 comments on commit 81c65aa

Please sign in to comment.