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
Binary file added samples/xlsx/TestIssue686.xlsx
Binary file not shown.
Binary file added samples/xlsx/TestIssue697.xlsx
Binary file not shown.
Binary file added samples/xlsx/TestTrimColumnNames.xlsx
Binary file not shown.
18 changes: 10 additions & 8 deletions src/MiniExcel/ExcelFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using System;
using System.IO;

internal class ExcelReaderFactory
internal static class ExcelReaderFactory
{
internal static IExcelReader GetProvider(Stream stream, ExcelType excelType, IConfiguration configuration)
{
Expand All @@ -17,19 +17,21 @@ internal static IExcelReader GetProvider(Stream stream, ExcelType excelType, ICo
case ExcelType.XLSX:
return new ExcelOpenXmlSheetReader(stream, configuration);
default:
throw new NotSupportedException($"Please Issue for me");
throw new NotSupportedException("Please Issue for me");
}
}
}

internal class ExcelWriterFactory
internal static class ExcelWriterFactory
{
internal static IExcelWriter GetProvider(Stream stream, object value, string sheetName, ExcelType excelType, IConfiguration configuration, bool printHeader)
{
if (string.IsNullOrEmpty(sheetName))
throw new InvalidDataException("Sheet name can not be empty or null");
throw new ArgumentException("Sheet names can not be empty or null", nameof(sheetName));
if (sheetName.Length > 31 && excelType == ExcelType.XLSX)
throw new ArgumentException("Sheet names must be less than 31 characters", nameof(sheetName));
if (excelType == ExcelType.UNKNOWN)
throw new InvalidDataException("Please specify excelType");
throw new ArgumentException("Excel type cannot be ExcelType.UNKNOWN", nameof(excelType));

switch (excelType)
{
Expand All @@ -38,12 +40,12 @@ internal static IExcelWriter GetProvider(Stream stream, object value, string she
case ExcelType.XLSX:
return new ExcelOpenXmlSheetWriter(stream, value, sheetName, configuration, printHeader);
default:
throw new NotSupportedException($"Please Issue for me");
throw new NotSupportedException($"The {excelType} Excel format is not supported");
}
}
}

internal class ExcelTemplateFactory
internal static class ExcelTemplateFactory
{
internal static IExcelTemplateAsync GetProvider(Stream stream, IConfiguration configuration, ExcelType excelType = ExcelType.XLSX)
{
Expand All @@ -53,7 +55,7 @@ internal static IExcelTemplateAsync GetProvider(Stream stream, IConfiguration co
var valueExtractor = new InputValueExtractor();
return new ExcelOpenXmlTemplate(stream, configuration, valueExtractor);
default:
throw new NotSupportedException($"Please Issue for me");
throw new NotSupportedException("Please Issue for me");
}
}
}
Expand Down
5 changes: 1 addition & 4 deletions src/MiniExcel/MiniExcel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,9 @@ public static int Insert(string path, object value, string sheetName = "Sheet1",
public static int Insert(this Stream stream, object value, string sheetName = "Sheet1", ExcelType excelType = ExcelType.XLSX, IConfiguration configuration = null, bool printHeader = true, bool overwriteSheet = false)
{
stream.Seek(0, SeekOrigin.End);
// reuse code
if (excelType == ExcelType.CSV)
{
var newValue = value is IEnumerable || value is IDataReader ? value : new[]{value}.AsEnumerable();
var newValue = value is IEnumerable || value is IDataReader ? value : new[]{value};
return ExcelWriterFactory.GetProvider(stream, newValue, sheetName, excelType, configuration, false).Insert(overwriteSheet);
}
else
Expand Down Expand Up @@ -88,9 +87,7 @@ public static int[] SaveAs(this Stream stream, object value, bool printHeader =
{
using (var excelReader = ExcelReaderFactory.GetProvider(stream, ExcelTypeHelper.GetExcelType(stream, excelType), configuration))
foreach (var item in excelReader.Query<T>(sheetName, startCell))
{
yield return item;
}
}
public static IEnumerable<dynamic> Query(string path, bool useHeaderRow = false, string sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, string startCell = "A1", IConfiguration configuration = null)
{
Expand Down
362 changes: 165 additions & 197 deletions src/MiniExcel/OpenXml/ExcelOpenXmlSheetReader.cs

Large diffs are not rendered by default.

10 changes: 4 additions & 6 deletions src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.DefaultOpenXml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -308,19 +308,17 @@ private string GetFileValue(int rowIndex, int cellIndex, object value)
private Tuple<string, string, string> GetDateTimeValue(DateTime value, ExcelColumnInfo columnInfo)
{
string cellValue = null;
if (!_configuration.Culture.Equals(CultureInfo.InvariantCulture))
if (!ReferenceEquals(_configuration.Culture, CultureInfo.InvariantCulture))
{
cellValue = value.ToString(_configuration.Culture);
return Tuple.Create("2", "str", cellValue);
}

var oaDate = CorrectDateTimeValue(value);
cellValue = oaDate.ToString(CultureInfo.InvariantCulture);

if (columnInfo?.ExcelFormat != null)
return Tuple.Create(columnInfo.ExcelFormatId.ToString(), (string)null, cellValue);

return Tuple.Create("3", (string)null, cellValue);
var format = columnInfo?.ExcelFormat != null ? columnInfo.ExcelFormatId.ToString() : "3";

return Tuple.Create(format, (string)null, cellValue);
}

private static double CorrectDateTimeValue(DateTime value)
Expand Down
2 changes: 2 additions & 0 deletions src/MiniExcel/OpenXml/OpenXmlConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public class OpenXmlConfiguration : Configuration
public bool IgnoreTemplateParameterMissing { get; set; } = true;
public bool EnableWriteNullValueCell { get; set; } = true;
public bool WriteEmptyStringAsNull { get; set; } = false;
public bool TrimColumnNames { get; set; } = true;
public bool IgnoreEmptyRows { get; set; } = false;
public bool EnableSharedStringCache { get; set; } = true;
public long SharedStringCacheSize { get; set; } = 5 * 1024 * 1024;
public DynamicExcelSheet[] DynamicSheets { get; set; }
Expand Down
40 changes: 20 additions & 20 deletions src/MiniExcel/Utils/ColumnHelper.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
namespace MiniExcelLibs.Utils
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;

namespace MiniExcelLibs.Utils
{
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;

// For Row/Column Index
internal static partial class ColumnHelper
internal static class ColumnHelper
{
private const int GENERAL_COLUMN_INDEX = 255;
private const int MAX_COLUMN_INDEX = 16383;
private static int _IntMappingAlphabetCount = 0;
private static readonly ConcurrentDictionary<int, string> _IntMappingAlphabet = new ConcurrentDictionary<int, string>();
private static readonly ConcurrentDictionary<string, int> _AlphabetMappingInt = new ConcurrentDictionary<string, int>();

static ColumnHelper()
{
_IntMappingAlphabetCount = _IntMappingAlphabet.Count;
Expand All @@ -21,9 +23,8 @@ static ColumnHelper()
public static string GetAlphabetColumnName(int columnIndex)
{
CheckAndSetMaxColumnIndex(columnIndex);
if (_IntMappingAlphabet.TryGetValue(columnIndex, out var value))
return value;
throw new KeyNotFoundException();
return _IntMappingAlphabet.TryGetValue(columnIndex, out var value) ? value
: throw new KeyNotFoundException();
}

public static int GetColumnIndex(string columnName)
Expand All @@ -35,23 +36,23 @@ public static int GetColumnIndex(string columnName)

private static void CheckAndSetMaxColumnIndex(int columnIndex)
{
if (columnIndex >= _IntMappingAlphabetCount)
if (columnIndex < _IntMappingAlphabetCount)
return;
if (columnIndex > MAX_COLUMN_INDEX)
throw new InvalidDataException($"ColumnIndex {columnIndex} over excel vaild max index.");

for (int i = _IntMappingAlphabet.Count; i <= columnIndex; i++)
{
if (columnIndex > MAX_COLUMN_INDEX)
throw new InvalidDataException($"ColumnIndex {columnIndex} over excel vaild max index.");
for (int i = _IntMappingAlphabet.Count; i <= columnIndex; i++)
{
_IntMappingAlphabet.AddOrUpdate(i, IntToLetters(i), (a, b) => IntToLetters(i));
_AlphabetMappingInt.AddOrUpdate(IntToLetters(i), i, (a, b) => i);
}
_IntMappingAlphabetCount = _IntMappingAlphabet.Count;
_IntMappingAlphabet.AddOrUpdate(i, IntToLetters(i), (a, b) => IntToLetters(i));
_AlphabetMappingInt.AddOrUpdate(IntToLetters(i), i, (a, b) => i);
}
_IntMappingAlphabetCount = _IntMappingAlphabet.Count;
}

internal static string IntToLetters(int value)
{
value = value + 1;
string result = string.Empty;
value++;
var result = string.Empty;
while (--value >= 0)
{
result = (char)('A' + value % 26) + result;
Expand All @@ -60,5 +61,4 @@ internal static string IntToLetters(int value)
return result;
}
}

}
Loading