add Number Format Records cache

This commit is contained in:
ITWeiHan 2021-06-17 01:48:57 +08:00
parent 1f67061d38
commit 8996ca1032
3 changed files with 51 additions and 3 deletions

Binary file not shown.

View File

@ -10,7 +10,7 @@
private const string _ns = Config.SpreadsheetmlXmlns;
private Dictionary<int, StyleRecord> _cellXfs = new Dictionary<int, StyleRecord>();
private Dictionary<int, StyleRecord> _cellStyleXfs = new Dictionary<int, StyleRecord>();
private Dictionary<int, NumberFormatRecord> _numberFormatRecords = new Dictionary<int, NumberFormatRecord>();
public ExcelOpenXmlStyles(ExcelOpenXmlZip zip)
{
using (var Reader = zip.GetXmlReader(@"xl/styles.xml"))
@ -24,7 +24,7 @@
if (Reader.IsStartElement("cellXfs", _ns))
{
if (!XmlReaderHelper.ReadFirstContent(Reader))
return;
continue;
var index = 0;
while (!Reader.EOF)
@ -44,7 +44,7 @@
else if (Reader.IsStartElement("cellStyleXfs", _ns))
{
if (!XmlReaderHelper.ReadFirstContent(Reader))
return;
continue;
var index = 0;
while (!Reader.EOF)
@ -62,6 +62,27 @@
break;
}
}
else if (Reader.IsStartElement("numFmts", _ns))
{
if (!XmlReaderHelper.ReadFirstContent(Reader))
continue;
while (!Reader.EOF)
{
if (Reader.IsStartElement("numFmt", _ns))
{
int.TryParse(Reader.GetAttribute("numFmtId"), out var numFmtId);
var formatCode = Reader.GetAttribute("formatCode");
_numberFormatRecords.Add(numFmtId,new NumberFormatRecord(numFmtId, formatCode));
Reader.Skip();
}
else if (!XmlReaderHelper.SkipContent(Reader))
{
break;
}
}
}
else if (!XmlReaderHelper.SkipContent(Reader))
{
break;
@ -165,6 +186,7 @@
FormatString = formatString;
Type = type;
}
public NumberFormatRecord NumberFormat { get; set; }
}
internal class StyleRecord
@ -172,4 +194,17 @@
public int XfId { get; set; }
public int NumFmtId { get; set; }
}
internal sealed class NumberFormatRecord
{
public NumberFormatRecord(int formatIndexInFile, string formatString)
{
FormatIndexInFile = formatIndexInFile;
FormatString = formatString;
}
public int FormatIndexInFile { get; }
public string FormatString { get; }
}
}

View File

@ -26,6 +26,19 @@ namespace MiniExcelLibs.Tests
this.output = output;
}
/// <summary>
/// [Dynamic Query custom format yyyy.mm.dd can't convert to datetime · Issue #256 · shps951023/MiniExcel]
/// (https://github.com/shps951023/MiniExcel/issues/256)
/// </summary>
[Fact]
public void Issue256()
{
var path = PathHelper.GetSamplePath("xlsx/TestIssue256.xlsx");
var rows = MiniExcel.Query(path, true).ToList();
}
/// <summary>
/// Csv SaveAs by datareader with encoding default show messy code #253
/// </summary>