Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reuse code between async/sync methods #602

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ jobs:

steps:
- name: Checkout repository
uses: actions/checkout@v2
uses: actions/checkout@v4

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Setup .NET
uses: actions/setup-dotnet@v1
with:
Expand Down
12 changes: 12 additions & 0 deletions src/MiniExcel/OpenXml/Constants/ExcelContentTypes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace MiniExcelLibs.OpenXml.Constants
{
internal static class ExcelContentTypes
{
internal const string Relationships = "application/vnd.openxmlformats-package.relationships+xml";
internal const string SharedStrings = "application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml";
internal const string Worksheet = "application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml";
internal const string Styles = "application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml";
internal const string Drawing = "application/vnd.openxmlformats-officedocument.drawing+xml";
internal const string Workbook = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml";
}
}
19 changes: 19 additions & 0 deletions src/MiniExcel/OpenXml/Constants/ExcelFileNames.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace MiniExcelLibs.OpenXml.Constants
{
internal static class ExcelFileNames
{
internal const string Rels = "_rels/.rels";
internal const string SharedStrings = "xl/sharedStrings.xml";

internal const string ContentTypes = "[Content_Types].xml";
internal const string Styles = "xl/styles.xml";
internal const string Workbook = "xl/workbook.xml";
internal const string WorkbookRels = "xl/_rels/workbook.xml.rels";
internal static string SheetRels(int sheetId)
=> $"xl/worksheets/_rels/sheet{sheetId}.xml.rels";
internal static string Drawing(int sheetIndex)
=> $"xl/drawings/drawing{sheetIndex + 1}.xml";
internal static string DrawingRels(int sheetIndex)
=> $"xl/drawings/_rels/drawing{sheetIndex + 1}.xml.rels";
}
}
Original file line number Diff line number Diff line change
@@ -1,37 +1,36 @@
using MiniExcelLibs.Zip;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MiniExcelLibs.OpenXml.Models;

namespace MiniExcelLibs.OpenXml
namespace MiniExcelLibs.OpenXml.Constants
{
internal partial class ExcelOpenXmlSheetWriter : IExcelWriter
internal static class ExcelXml
{
private readonly Dictionary<string, ZipPackageInfo> _zipDictionary = new Dictionary<string, ZipPackageInfo>();

static ExcelOpenXmlSheetWriter()
static ExcelXml()
{
_defaultRels = MinifyXml(_defaultRels);
_defaultWorkbookXml = MinifyXml(_defaultWorkbookXml);
_defaultStylesXml = MinifyXml(_defaultStylesXml);
_defaultWorkbookXmlRels = MinifyXml(_defaultWorkbookXmlRels);
_defaultSheetRelXml = MinifyXml(_defaultSheetRelXml);
_defaultDrawing = MinifyXml(_defaultDrawing);
DefaultRels = MinifyXml(DefaultRels);
DefaultWorkbookXml = MinifyXml(DefaultWorkbookXml);
DefaultStylesXml = MinifyXml(DefaultStylesXml);
DefaultWorkbookXmlRels = MinifyXml(DefaultWorkbookXmlRels);
DefaultSheetRelXml = MinifyXml(DefaultSheetRelXml);
DefaultDrawing = MinifyXml(DefaultDrawing);
}

private static readonly string _defaultRels = @"<?xml version=""1.0"" encoding=""utf-8""?>
private static string MinifyXml(string xml) => xml.Replace("\r", "").Replace("\n", "").Replace("\t", "");

internal static readonly string EmptySheetXml = $@"<?xml version=""1.0"" encoding=""utf-8""?><x:worksheet xmlns:x=""http://schemas.openxmlformats.org/spreadsheetml/2006/main""><x:dimension ref=""A1""/><x:sheetData></x:sheetData></x:worksheet>";

internal static readonly string DefaultRels = @"<?xml version=""1.0"" encoding=""utf-8""?>
<Relationships xmlns=""http://schemas.openxmlformats.org/package/2006/relationships"">
<Relationship Type=""http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument"" Target=""xl/workbook.xml"" Id=""Rfc2254092b6248a9"" />
</Relationships>";

private static readonly string _defaultWorkbookXmlRels = @"<?xml version=""1.0"" encoding=""utf-8""?>
internal static readonly string DefaultWorkbookXmlRels = @"<?xml version=""1.0"" encoding=""utf-8""?>
<Relationships xmlns=""http://schemas.openxmlformats.org/package/2006/relationships"">
{{sheets}}
<Relationship Type=""http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles"" Target=""/xl/styles.xml"" Id=""R3db9602ace774fdb"" />
<Relationship Type=""http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings"" Target=""/xl/sharedStrings.xml"" Id=""R3db9602ace778fdb"" />
</Relationships>";

private static readonly string _noneStylesXml = @"<?xml version=""1.0"" encoding=""utf-8""?>
internal static readonly string NoneStylesXml = @"<?xml version=""1.0"" encoding=""utf-8""?>
<x:styleSheet xmlns:x=""http://schemas.openxmlformats.org/spreadsheetml/2006/main"">
<x:fonts>
<x:font />
Expand All @@ -53,7 +52,7 @@ static ExcelOpenXmlSheetWriter()
</x:cellXfs>
</x:styleSheet>";

private static readonly string _defaultStylesXml = @"<?xml version=""1.0"" encoding=""utf-8""?>
internal static readonly string DefaultStylesXml = @"<?xml version=""1.0"" encoding=""utf-8""?>
<x:styleSheet xmlns:x=""http://schemas.openxmlformats.org/spreadsheetml/2006/main"">
<x:numFmts count=""1"">
<x:numFmt numFmtId=""0"" formatCode="""" />
Expand Down Expand Up @@ -157,65 +156,46 @@ static ExcelOpenXmlSheetWriter()
</x:cellStyles>
</x:styleSheet>";

private static readonly string _defaultWorkbookXml = @"<?xml version=""1.0"" encoding=""utf-8""?>
internal static readonly string DefaultWorkbookXml = @"<?xml version=""1.0"" encoding=""utf-8""?>
<x:workbook xmlns:r=""http://schemas.openxmlformats.org/officeDocument/2006/relationships""
xmlns:x=""http://schemas.openxmlformats.org/spreadsheetml/2006/main"">
<x:sheets>
{{sheets}}
</x:sheets>
</x:workbook>";

private static readonly string _defaultSheetRelXml = @"<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes""?>
internal static readonly string DefaultSheetRelXml = @"<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes""?>
<Relationships xmlns=""http://schemas.openxmlformats.org/package/2006/relationships"">
{{format}}
</Relationships>";
private static readonly string _defaultDrawing = @"<?xml version=""1.0"" encoding=""utf-8"" standalone=""yes""?>
internal static readonly string DefaultDrawing = @"<?xml version=""1.0"" encoding=""utf-8"" standalone=""yes""?>
<xdr:wsDr xmlns:a=""http://schemas.openxmlformats.org/drawingml/2006/main""
xmlns:r=""http://schemas.openxmlformats.org/officeDocument/2006/relationships""
xmlns:xdr=""http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing"">
{{format}}
</xdr:wsDr>";
private static readonly string _defaultDrawingXmlRels = @"<?xml version=""1.0"" encoding=""utf-8"" standalone=""yes""?>
internal static readonly string DefaultDrawingXmlRels = @"<?xml version=""1.0"" encoding=""utf-8"" standalone=""yes""?>
<Relationships xmlns=""http://schemas.openxmlformats.org/package/2006/relationships"">
{{format}}
</Relationships>";

private static readonly string _defaultSharedString = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?><sst xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\" count=\"0\" uniqueCount=\"0\"></sst>";
private static string MinifyXml(string xml) => xml.Replace("\r", "").Replace("\n", "").Replace("\t", "");

private string GetStylesXml()
{
var styleXml = string.Empty;
internal static readonly string DefaultSharedString = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?><sst xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\" count=\"0\" uniqueCount=\"0\"></sst>";

if (_configuration.TableStyles == TableStyles.None)
{
styleXml = _noneStylesXml;
}
else if (_configuration.TableStyles == TableStyles.Default)
{
styleXml = _defaultStylesXml;
}
internal static readonly string StartTypes = @"<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes""?><Types xmlns=""http://schemas.openxmlformats.org/package/2006/content-types""><Default ContentType=""application/vnd.openxmlformats-officedocument.spreadsheetml.printerSettings"" Extension=""bin""/><Default ContentType=""application/xml"" Extension=""xml""/><Default ContentType=""image/jpeg"" Extension=""jpg""/><Default ContentType=""image/png"" Extension=""png""/><Default ContentType=""image/gif"" Extension=""gif""/><Default ContentType=""application/vnd.openxmlformats-package.relationships+xml"" Extension=""rels""/>";
internal static string ContentType(string contentType, string partName) => $"<Override ContentType=\"{contentType}\" PartName=\"/{partName}\" />";
internal static readonly string EndTypes = "</Types>";

return styleXml;
}
internal static string WorksheetRelationship(SheetDto sheetDto)
=> $@"<Relationship Type=""http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet"" Target=""/{sheetDto.Path}"" Id=""{sheetDto.ID}"" />";

private string GetDrawingRelationshipXml(int sheetIndex)
{
var drawing = new StringBuilder();
foreach (var i in _files.Where(w => w.IsImage && w.SheetId == sheetIndex + 1))
{
drawing.AppendLine($@"<Relationship Type=""http://schemas.openxmlformats.org/officeDocument/2006/relationships/image"" Target=""{i.Path2}"" Id=""{i.ID}"" />");
}
internal static string ImageRelationship(FileDto image)
=> $@"<Relationship Type=""http://schemas.openxmlformats.org/officeDocument/2006/relationships/image"" Target=""{image.Path2}"" Id=""{image.ID}"" />";

return drawing.ToString();
}
internal static string DrawingRelationship(int sheetId)
=> $@"<Relationship Type=""http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing"" Target=""../drawings/drawing{sheetId}.xml"" Id=""drawing{sheetId}"" />";

private string GetDrawingXml(int sheetIndex)
{
var drawing = new StringBuilder();
foreach (var file in _files.Where(w => w.IsImage && w.SheetId == sheetIndex + 1))
{
drawing.Append($@"<xdr:oneCellAnchor>
internal static string DrawingXml(FileDto file, int fileIndex)
=> $@"<xdr:oneCellAnchor>
<xdr:from>
<xdr:col>{file.CellIndex - 1/* why -1 : https://user-images.githubusercontent.com/12729184/150460189-f08ed939-44d4-44e1-be6e-9c533ece6be8.png*/}</xdr:col>
<xdr:colOff>0</xdr:colOff>
Expand All @@ -225,7 +205,7 @@ private string GetDrawingXml(int sheetIndex)
<xdr:ext cx=""609600"" cy=""190500"" />
<xdr:pic>
<xdr:nvPicPr>
<xdr:cNvPr id=""{_files.IndexOf(file) + 1}"" descr="""" name=""2a3f9147-58ea-4a79-87da-7d6114c4877b"" />
<xdr:cNvPr id=""{fileIndex + 1}"" descr="""" name=""2a3f9147-58ea-4a79-87da-7d6114c4877b"" />
<xdr:cNvPicPr>
<a:picLocks noChangeAspect=""1"" />
</xdr:cNvPicPr>
Expand All @@ -247,49 +227,9 @@ private string GetDrawingXml(int sheetIndex)
</xdr:spPr>
</xdr:pic>
<xdr:clientData />
</xdr:oneCellAnchor>");
}

return drawing.ToString();
}

private void GenerateWorkBookXmls(
out StringBuilder workbookXml,
out StringBuilder workbookRelsXml,
out Dictionary<int, string> sheetsRelsXml)
{
workbookXml = new StringBuilder();
workbookRelsXml = new StringBuilder();
sheetsRelsXml = new Dictionary<int, string>();
var sheetId = 0;
foreach (var s in _sheets)
{
sheetId++;
if (string.IsNullOrEmpty(s.State))
{
workbookXml.AppendLine($@"<x:sheet name=""{ExcelOpenXmlUtils.EncodeXML(s.Name)}"" sheetId=""{sheetId}"" r:id=""{s.ID}"" />");
}
else
{
workbookXml.AppendLine($@"<x:sheet name=""{ExcelOpenXmlUtils.EncodeXML(s.Name)}"" sheetId=""{sheetId}"" state=""{s.State}"" r:id=""{s.ID}"" />");
}
</xdr:oneCellAnchor>";

workbookRelsXml.AppendLine($@"<Relationship Type=""http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet"" Target=""/{s.Path}"" Id=""{s.ID}"" />");

//TODO: support multiple drawing
//TODO: ../drawings/drawing1.xml or /xl/drawings/drawing1.xml
var sheetRelsXml = $@"<Relationship Type=""http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing"" Target=""../drawings/drawing{sheetId}.xml"" Id=""drawing{sheetId}"" />";
sheetsRelsXml.Add(s.SheetIdx, sheetRelsXml);
}
}

private string GetContentTypesXml()
{
var sb = new StringBuilder(@"<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes""?><Types xmlns=""http://schemas.openxmlformats.org/package/2006/content-types""><Default ContentType=""application/vnd.openxmlformats-officedocument.spreadsheetml.printerSettings"" Extension=""bin""/><Default ContentType=""application/xml"" Extension=""xml""/><Default ContentType=""image/jpeg"" Extension=""jpg""/><Default ContentType=""image/png"" Extension=""png""/><Default ContentType=""image/gif"" Extension=""gif""/><Default ContentType=""application/vnd.openxmlformats-package.relationships+xml"" Extension=""rels""/>");
foreach (var p in _zipDictionary)
sb.Append($"<Override ContentType=\"{p.Value.ContentType}\" PartName=\"/{p.Key}\" />");
sb.Append("</Types>");
return sb.ToString();
}
internal static string Sheet(SheetDto sheetDto, int sheetId)
=> $@"<x:sheet name=""{ExcelOpenXmlUtils.EncodeXML(sheetDto.Name)}"" sheetId=""{sheetId}""{(string.IsNullOrWhiteSpace(sheetDto.State) ? string.Empty : $" state=\"{sheetDto.State}\"")} r:id=""{sheetDto.ID}"" />";
}
}
41 changes: 41 additions & 0 deletions src/MiniExcel/OpenXml/Constants/WorksheetXml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System.Globalization;

namespace MiniExcelLibs.OpenXml.Constants
{
internal class WorksheetXml
{
internal const string StartWorksheet = @"<?xml version=""1.0"" encoding=""utf-8""?><x:worksheet xmlns:x=""http://schemas.openxmlformats.org/spreadsheetml/2006/main"">";
internal const string StartWorksheetWithRelationship = @"<?xml version=""1.0"" encoding=""utf-8""?><x:worksheet xmlns:r=""http://schemas.openxmlformats.org/officeDocument/2006/relationships"" xmlns:x=""http://schemas.openxmlformats.org/spreadsheetml/2006/main"" >";
internal const string EndWorksheet = "</x:worksheet>";

internal const string StartDimension = @"<x:dimension ref=""";
internal const string DimensionPlaceholder = " />";
internal static string Dimension(string dimensionRef)
=> $"{StartDimension}{dimensionRef}\"/>";

internal const string StartSheetData = "<x:sheetData>";
internal const string EndSheetData = "</x:sheetData>";

internal static string StartRow(int rowIndex)
=> $"<x:row r=\"{rowIndex}\">";
internal const string EndRow = "</x:row>";

internal const string StartCols = "<x:cols>";
internal static string Column(int? colIndex, double? columnWidth)
=> $@"<x:col min=""{colIndex.GetValueOrDefault() + 1}"" max=""{colIndex.GetValueOrDefault() + 1}"" width=""{columnWidth?.ToString(CultureInfo.InvariantCulture)}"" customWidth=""1"" />";
internal const string EndCols = "</x:cols>";

internal static string EmptyCell(string cellReference, string styleIndex)
=> $"<x:c r=\"{cellReference}\" s=\"{styleIndex}\"></x:c>";
//t check avoid format error ![image](https://user-images.githubusercontent.com/12729184/118770190-9eee3480-b8b3-11eb-9f5a-87a439f5e320.png)
internal static string Cell(string cellReference, string cellType, string styleIndex, string cellValue, bool preserveSpace = false)
=> $"<x:c r=\"{cellReference}\"{(cellType == null ? string.Empty : $" t=\"{cellType}\"")} s=\"{styleIndex}\"{(preserveSpace ? " xml:space=\"preserve\"" : string.Empty)}><x:v>{cellValue}</x:v></x:c>";

internal static string Autofilter(string dimensionRef)
=> $"<x:autoFilter ref=\"{dimensionRef}\" />";

internal static string Drawing(int sheetIndex)
=> $"<x:drawing r:id=\"drawing{sheetIndex}\" />";

}
}
3 changes: 1 addition & 2 deletions src/MiniExcel/OpenXml/ExcelOpenXmlSheetReader.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using MiniExcelLibs.Exceptions;
using MiniExcelLibs.Utils;
using MiniExcelLibs.Utils;
using MiniExcelLibs.Zip;
using System;
using System.Collections.Generic;
Expand Down
Loading
Loading