Skip to content
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
39 changes: 16 additions & 23 deletions src/MiniExcel/OpenXml/ExcelOpenXmlSheetReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -322,12 +322,11 @@ public IEnumerable<IDictionary<string, object>> Query(bool useHeaderRow, string
{
_mergeCells.MergesValues[aR] = cellValue;
}
else if (_mergeCells.MergesMap.ContainsKey(aR))
else if (_mergeCells.MergesMap.TryGetValue(aR, out var mergeKey))
{
var mergeKey = _mergeCells.MergesMap[aR];
object mergeValue = null;
if (_mergeCells.MergesValues.ContainsKey(mergeKey))
mergeValue = _mergeCells.MergesValues[mergeKey];
if (_mergeCells.MergesValues.TryGetValue(mergeKey, out var value))
mergeValue = value;
cellValue = mergeValue;
}
}
Expand Down Expand Up @@ -398,9 +397,8 @@ private void SetCellsValueAndHeaders(object cellValue, bool useHeaderRow, ref Di
}
else
{
if (headRows.ContainsKey(columnIndex))
if (headRows.TryGetValue(columnIndex, out var key))
{
var key = headRows[columnIndex];
cell[key] = cellValue;
}
}
Expand Down Expand Up @@ -467,10 +465,9 @@ private void SetCellsValueAndHeaders(object cellValue, bool useHeaderRow, ref Di
{
foreach (var alias in pInfo.ExcelColumnAliases)
{
if (headersDic.ContainsKey(alias))
if (headersDic.TryGetValue(alias, out var columnId))
{
object newV = null;
var columnId = headersDic[alias];
var columnName = keys[columnId];
item.TryGetValue(columnName, out var itemValue);

Expand All @@ -490,9 +487,8 @@ private void SetCellsValueAndHeaders(object cellValue, bool useHeaderRow, ref Di
{
item.TryGetValue(pInfo.ExcelIndexName, out itemValue);
}
else if (headersDic.ContainsKey(pInfo.ExcelColumnName))
else if (headersDic.TryGetValue(pInfo.ExcelColumnName, out var columnId))
{
var columnId = headersDic[pInfo.ExcelColumnName];
var columnName = keys[columnId];
item.TryGetValue(columnName, out itemValue);
}
Expand Down Expand Up @@ -822,7 +818,7 @@ public IEnumerable<IDictionary<string, object>> QueryRange(bool useHeaderRow, st
// if sheets count > 1 need to read xl/_rels/workbook.xml.rels
var sheets = _archive.entries.Where(w => w.FullName.StartsWith("xl/worksheets/sheet", StringComparison.OrdinalIgnoreCase)
|| w.FullName.StartsWith("/xl/worksheets/sheet", StringComparison.OrdinalIgnoreCase)
);
).ToList();
ZipArchiveEntry sheetEntry = null;
if (sheetName != null)
{
Expand All @@ -832,7 +828,7 @@ public IEnumerable<IDictionary<string, object>> QueryRange(bool useHeaderRow, st
throw new InvalidOperationException("Please check sheetName/Index is correct");
sheetEntry = sheets.Single(w => w.FullName == $"xl/{s.Path}" || w.FullName == $"/xl/{s.Path}" || w.FullName == s.Path || s.Path == $"/{w.FullName}");
}
else if (sheets.Count() > 1)
else if (sheets.Count > 1)
{
SetWorkbookRels(_archive.entries);
var s = _sheetRecords[0];
Expand Down Expand Up @@ -1086,13 +1082,9 @@ public IEnumerable<IDictionary<string, object>> QueryRange(bool useHeaderRow, st
{
_mergeCells.MergesValues[aR] = cellValue;
}
else if (_mergeCells.MergesMap.ContainsKey(aR))
else if (_mergeCells.MergesMap.TryGetValue(aR, out var mergeKey))
{
var mergeKey = _mergeCells.MergesMap[aR];
object mergeValue = null;
if (_mergeCells.MergesValues.ContainsKey(mergeKey))
mergeValue = _mergeCells.MergesValues[mergeKey];
cellValue = mergeValue;
_mergeCells.MergesValues.TryGetValue(mergeKey, out cellValue);
}
}
////2022-09-24跳过endcell结束单元格所以在的列
Expand Down Expand Up @@ -1194,15 +1186,16 @@ public IEnumerable<IDictionary<string, object>> QueryRange(bool useHeaderRow, st
{
foreach (var alias in pInfo.ExcelColumnAliases)
{
if (headersDic.ContainsKey(alias))
if (headersDic.TryGetValue(alias, out var value))
{
object newV = null;
object itemValue = item[keys[headersDic[alias]]];
object itemValue = item[keys[value]];

if (itemValue == null)
continue;

newV = TypeHelper.TypeMapping(v, pInfo, newV, itemValue, rowIndex, startCell, configuration);
newV = TypeHelper.TypeMapping(v, pInfo, newV, itemValue, rowIndex, startCell,
configuration);
}
}
}
Expand All @@ -1213,8 +1206,8 @@ public IEnumerable<IDictionary<string, object>> QueryRange(bool useHeaderRow, st
object itemValue = null;
if (pInfo.ExcelIndexName != null && keys.Contains(pInfo.ExcelIndexName))
itemValue = item[pInfo.ExcelIndexName];
else if (headersDic.ContainsKey(pInfo.ExcelColumnName))
itemValue = item[keys[headersDic[pInfo.ExcelColumnName]]];
else if (headersDic.TryGetValue(pInfo.ExcelColumnName, out var value))
itemValue = item[keys[value]];

if (itemValue == null)
continue;
Expand Down
24 changes: 11 additions & 13 deletions src/MiniExcel/SaveByTemplate/ExcelOpenXmlTemplate.Impl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -482,9 +482,8 @@ private void WriteSheetXml(Stream stream, XmlDocument doc, XmlNode sheetData, bo
rowXml.Replace(key, "");
continue;
}
if (!dic.ContainsKey(propInfo.Key))
if (!dic.TryGetValue(propInfo.Key, out var cellValue))
continue;
var cellValue = dic[propInfo.Key];
if (cellValue == null)
{
rowXml.Replace(key, "");
Expand Down Expand Up @@ -899,9 +898,9 @@ private void ReplaceSharedStringsToStr(IDictionary<int, string> sharedStrings, r
if (t == "s")
{
//need to check sharedstring exist or not
if (sharedStrings.ContainsKey(int.Parse(v.InnerText)))
if (sharedStrings.TryGetValue(int.Parse(v.InnerText), out var shared))
{
v.InnerText = sharedStrings[int.Parse(v.InnerText)];
v.InnerText = shared;
// change type = str and replace its value
c.SetAttribute("t", "str");
}
Expand Down Expand Up @@ -935,11 +934,11 @@ private void UpdateDimensionAndGetRowsInfo(IDictionary<string, object> inputMaps
var r = c.GetAttribute("r");

// ==== mergecells ====
if (this.XMergeCellInfos.ContainsKey(r))
if (this.XMergeCellInfos.TryGetValue(r, out var merCell))
{
if (xRowInfo.RowMercells == null)
xRowInfo.RowMercells = new List<XMergeCell>();
xRowInfo.RowMercells.Add(this.XMergeCellInfos[r]);
xRowInfo.RowMercells.Add(merCell);
}

if (changeRowIndex)
Expand All @@ -962,7 +961,7 @@ private void UpdateDimensionAndGetRowsInfo(IDictionary<string, object> inputMaps
continue;

// TODO: default if not contain property key, clean the template string
if (!inputMaps.ContainsKey(propNames[0]))
if (!inputMaps.TryGetValue(propNames[0], out var cellValue))
{
if (_configuration.IgnoreTemplateParameterMissing)
{
Expand All @@ -975,14 +974,14 @@ private void UpdateDimensionAndGetRowsInfo(IDictionary<string, object> inputMaps
}
}

var cellValue = inputMaps[propNames[0]]; // 1. From left to right, only the first set is used as the basis for the list
//cellValue = inputMaps[propNames[0]] - 1. From left to right, only the first set is used as the basis for the list
if (cellValue is IEnumerable && !(cellValue is string))
{
if (this.XMergeCellInfos.ContainsKey(r))
if (xRowInfo.IEnumerableMercell == null)
{
if (xRowInfo.IEnumerableMercell == null)
if (this.XMergeCellInfos.TryGetValue(r, out var info))
{
xRowInfo.IEnumerableMercell = this.XMergeCellInfos[r];
xRowInfo.IEnumerableMercell = info;
}
}

Expand Down Expand Up @@ -1065,14 +1064,13 @@ private void UpdateDimensionAndGetRowsInfo(IDictionary<string, object> inputMaps
v.InnerText = v.InnerText.Replace($"{{{{{propNames[0]}}}}}", propNames[1]);
break;
}
if (!xRowInfo.PropsMap.ContainsKey(propNames[1]))
if (!xRowInfo.PropsMap.TryGetValue(propNames[1], out var prop))
{
v.InnerText = v.InnerText.Replace($"{{{{{propNames[0]}.{propNames[1]}}}}}", "");
continue;
throw new InvalidDataException($"{propNames[0]} doesn't have {propNames[1]} property");
}
// auto check type https://github.com/shps951023/MiniExcel/issues/177
var prop = xRowInfo.PropsMap[propNames[1]];
var type = prop.UnderlyingTypePropType; //avoid nullable

if (isMultiMatch)
Expand Down
30 changes: 14 additions & 16 deletions src/MiniExcel/Utils/CustomPropertyHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ internal static List<ExcelColumnInfo> SortCustomProps(List<ExcelColumnInfo> prop
// https://github.com/shps951023/MiniExcel/issues/142
//TODO: need optimize performance

var withCustomIndexProps = props.Where(w => w.ExcelColumnIndex != null && w.ExcelColumnIndex > -1);
var withCustomIndexProps = props.Where(w => w.ExcelColumnIndex != null && w.ExcelColumnIndex > -1).ToList();
if (withCustomIndexProps.GroupBy(g => g.ExcelColumnIndex).Any(_ => _.Count() > 1))
throw new InvalidOperationException($"Duplicate column name");
throw new InvalidOperationException("Duplicate column name");

var maxColumnIndex = props.Count - 1;
if (withCustomIndexProps.Any())
if (withCustomIndexProps.Count != 0)
maxColumnIndex = Math.Max((int)withCustomIndexProps.Max(w => w.ExcelColumnIndex), maxColumnIndex);

var withoutCustomIndexProps = props.Where(w => w.ExcelColumnIndex == null || w.ExcelColumnIndex == -1).ToList();
Expand Down Expand Up @@ -136,21 +136,19 @@ internal static List<ExcelColumnInfo> GetExcelCustomPropertyInfos(Type type, str
if (props.Count == 0)
throw new InvalidOperationException($"{type.Name} un-ignore properties count can't be 0");

var withCustomIndexProps = props.Where(w => w.ExcelColumnIndex != null && w.ExcelColumnIndex > -1);
if (withCustomIndexProps.GroupBy(g => g.ExcelColumnIndex).Any(_ => _.Count() > 1))
throw new InvalidOperationException("Duplicate column name");
var maxkey = keys.Last();
var maxIndex = ColumnHelper.GetColumnIndex(maxkey);
foreach (var p in props)
{
var withCustomIndexProps = props.Where(w => w.ExcelColumnIndex != null && w.ExcelColumnIndex > -1);
if (withCustomIndexProps.GroupBy(g => g.ExcelColumnIndex).Any(_ => _.Count() > 1))
throw new InvalidOperationException($"Duplicate column name");
var maxkey = keys.Last();
var maxIndex = ColumnHelper.GetColumnIndex(maxkey);
foreach (var p in props)
if (p.ExcelColumnIndex != null)
{
if (p.ExcelColumnIndex != null)
{
if (p.ExcelColumnIndex > maxIndex)
throw new ArgumentException($"ExcelColumnIndex {p.ExcelColumnIndex} over haeder max index {maxkey}");
if (p.ExcelColumnName == null)
throw new InvalidOperationException($"{p.Property.Info.DeclaringType.Name} {p.Property.Name}'s ExcelColumnIndex {p.ExcelColumnIndex} can't find excel column name");
}
if (p.ExcelColumnIndex > maxIndex)
throw new ArgumentException($"ExcelColumnIndex {p.ExcelColumnIndex} over haeder max index {maxkey}");
if (p.ExcelColumnName == null)
throw new InvalidOperationException($"{p.Property.Info.DeclaringType.Name} {p.Property.Name}'s ExcelColumnIndex {p.ExcelColumnIndex} can't find excel column name");
}
}

Expand Down
9 changes: 5 additions & 4 deletions src/MiniExcel/Utils/ListHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ public static class IEnumerableHelper
{
public static bool StartsWith<T>(this IList<T> span, IList<T> value) where T : IEquatable<T>
{
if (value.Count() == 0)
if (value.Count == 0)
return true;

var b = span.Take(value.Count());
if (b.Count() != value.Count())
var b = span.Take(value.Count);
var bCount = b.Count();
if (bCount != value.Count)
return false;

for (int i = 0; i < b.Count(); i++)
for (int i = 0; i < bCount; i++)
if (!span[i].Equals(value[i]))
return false;

Expand Down
38 changes: 27 additions & 11 deletions src/MiniExcel/WriteAdapter/EnumerableWriteAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,16 @@ public List<ExcelColumnInfo> GetColumns()
_enumerator = _values.GetEnumerator();
if (!_enumerator.MoveNext())
{
_empty = true;
return null;
try
{
_empty = true;
return null;
}
finally
{
(_enumerator as IDisposable)?.Dispose();
_enumerator = null;
}
}
return CustomPropertyHelper.GetColumnInfoFromValue(_enumerator.Current, _configuration);
}
Expand All @@ -58,20 +66,28 @@ public IEnumerable<IEnumerable<CellWriteInfo>> GetRows(List<ExcelColumnInfo> pro
yield break;
}

if (_enumerator is null)
try
{
_enumerator = _values.GetEnumerator();
if (!_enumerator.MoveNext())
if (_enumerator is null)
{
yield break;
_enumerator = _values.GetEnumerator();
if (!_enumerator.MoveNext())
{
yield break;
}
}
}

do
do
{
cancellationToken.ThrowIfCancellationRequested();
yield return GetRowValues(_enumerator.Current, props);
} while (_enumerator.MoveNext());
}
finally
{
cancellationToken.ThrowIfCancellationRequested();
yield return GetRowValues(_enumerator.Current, props);
} while (_enumerator.MoveNext());
(_enumerator as IDisposable)?.Dispose();
_enumerator = null;
}
}


Expand Down
Loading