Skip to content

Commit

Permalink
2.0.5
Browse files Browse the repository at this point in the history
  • Loading branch information
mustaddon committed Dec 4, 2021
1 parent f48dc24 commit 313f073
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
40 changes: 25 additions & 15 deletions ArrayToExcel/ArrayToExcel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,25 @@ static MemoryStream CreateExcel(IEnumerable<SheetSchema> sheetSchemas)

AddStyles(workbookpart);

var sheetId = 1u;
var sheetNames = new HashSet<string>();
var sheetId = 0u;

foreach (var sheetSchema in sheetSchemas)
{
sheetId++;

var worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
worksheetPart.Worksheet = new Worksheet();

sheets.Append(new Sheet()
var sheet = new Sheet()
{
Id = workbookpart.GetIdOfPart(worksheetPart),
SheetId = sheetId++,
Name = NormSheetName(sheetSchema.SheetName),
});
SheetId = sheetId,
Name = NormSheetName(sheetSchema.SheetName, sheetId, sheetNames),
};

sheetNames.Add(sheet.Name.Value ?? string.Empty);
sheets.Append(sheet);

var cols = worksheetPart.Worksheet.AppendChild(new Columns());
var sheetData = worksheetPart.Worksheet.AppendChild(new SheetData());
Expand All @@ -69,9 +74,17 @@ static MemoryStream CreateExcel(IEnumerable<SheetSchema> sheetSchemas)
return ms;
}

static string NormSheetName(string value)
static string NormSheetName(string? value, uint sheetId, HashSet<string> existNames)
{
return value.Length > 31 ? value.Substring(0, 28) + "..." : value;
value = _invalidSheetNameChars.Replace(value ?? string.Empty, string.Empty).Trim();

if (string.IsNullOrWhiteSpace(value) || existNames.Contains(value))
return $"Sheet{sheetId}";

if (value.Length > 31)
return $"{value.Substring(0, 28)}...";

return value;
}

static void AddStyles(WorkbookPart workbookPart)
Expand Down Expand Up @@ -190,14 +203,7 @@ static CellValue GetCellValue(object? value)
if (type == typeof(float))
return new(((float)value).ToString(_cultureInfo));

return new(RemoveInvalidXmlChars(value.ToString()));
}

static string RemoveInvalidXmlChars(string text)
{
return string.IsNullOrEmpty(text) ? text
: new Regex(@"(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|[\x00-\x08\x0B\x0C\x0E-\x1F\x7F-\x9F\uFEFF\uFFFE\uFFFF]", RegexOptions.Compiled)
.Replace(text, string.Empty);
return new(_invalidXmlChars.Replace(value.ToString(), string.Empty));
}

static CellValues GetCellType(object? value)
Expand Down Expand Up @@ -246,5 +252,9 @@ static string GetColReference(int index)
static readonly CultureInfo _cultureInfo = CultureInfo.GetCultureInfo("en-US");

static readonly string _digits = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

static readonly Regex _invalidXmlChars = new(@"(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|[\x00-\x08\x0B\x0C\x0E-\x1F\x7F-\x9F\uFEFF\uFFFE\uFFFF]", RegexOptions.Compiled);

static readonly Regex _invalidSheetNameChars = new(@"[:?*\\/\[\]\r\n]|[\uDC00-\uDFFF]|[\uD800-\uDBFF]|[\x00-\x08\x0B\x0C\x0E-\x1F\x7F-\x9F\uFEFF\uFFFE\uFFFF]", RegexOptions.Compiled);
}
}
6 changes: 3 additions & 3 deletions ArrayToExcel/ArrayToExcel.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
<Nullable>enable</Nullable>
<SignAssembly>True</SignAssembly>
<AssemblyOriginatorKeyFile>..\ArrayToExcel.snk</AssemblyOriginatorKeyFile>
<AssemblyVersion>2.0.4</AssemblyVersion>
<FileVersion>2.0.4</FileVersion>
<Version>2.0.4</Version>
<AssemblyVersion>2.0.5</AssemblyVersion>
<FileVersion>2.0.5</FileVersion>
<Version>2.0.5</Version>
<Company></Company>
<Authors>Leonid Salavatov</Authors>
<Copyright>Leonid Salavatov 2021</Copyright>
Expand Down

0 comments on commit 313f073

Please sign in to comment.