【修复】读取excel数据出错时,打印出正确的出错位置(之前有可能往后偏移一位,给出错误的位置信息)

main
walon 2021-07-29 18:35:56 +08:00
parent df722abb4a
commit 27e9bd2d1f
4 changed files with 20 additions and 6 deletions

View File

@ -343,7 +343,7 @@ namespace Luban.Job.Cfg.DataCreators
} }
catch (Exception e) catch (Exception e)
{ {
var dce = new DataCreateException(e, stream.CurrentExcelPosition); var dce = new DataCreateException(e, stream.LastReadDataInfo);
dce.Push(bean, f); dce.Push(bean, f);
throw dce; throw dce;
} }

View File

@ -187,7 +187,7 @@ namespace Luban.Job.Cfg.DataCreators
} }
catch (Exception e) catch (Exception e)
{ {
var dce = new DataCreateException(e, stream.CurrentExcelPosition); var dce = new DataCreateException(e, stream.LastReadDataInfo);
dce.Push(bean, f); dce.Push(bean, f);
throw dce; throw dce;
} }

View File

@ -98,7 +98,7 @@ namespace Luban.Job.Cfg.DataCreators
} }
catch (Exception e) catch (Exception e)
{ {
var dce = new DataCreateException(e, stream.CurrentExcelPosition); var dce = new DataCreateException(e, stream.LastReadDataInfo);
throw dce; throw dce;
} }
} }
@ -133,7 +133,7 @@ namespace Luban.Job.Cfg.DataCreators
} }
catch (Exception e) catch (Exception e)
{ {
var dce = new DataCreateException(e, stream.CurrentExcelPosition); var dce = new DataCreateException(e, stream.LastReadDataInfo);
throw dce; throw dce;
} }
} }

View File

@ -6,6 +6,11 @@ using System.Text;
namespace Luban.Job.Cfg.DataSources.Excel namespace Luban.Job.Cfg.DataSources.Excel
{ {
class DataNotEnoughException : System.Exception
{
}
class ExcelStream class ExcelStream
{ {
@ -80,7 +85,9 @@ namespace Luban.Job.Cfg.DataSources.Excel
public string First => _datas[_curIndex].Value?.ToString(); public string First => _datas[_curIndex].Value?.ToString();
public string CurrentExcelPosition => _datas[Math.Min(_curIndex, _datas.Count - 1)].ToString(); public string LastReadDataInfo => _datas[Math.Min(LastReadIndex, _datas.Count - 1)].ToString();
private int LastReadIndex { get; set; }
public int IncludeNullAndEmptySize => _toIndex - _curIndex + 1; public int IncludeNullAndEmptySize => _toIndex - _curIndex + 1;
@ -106,9 +113,11 @@ namespace Luban.Job.Cfg.DataSources.Excel
data = _datas[_curIndex++].Value; data = _datas[_curIndex++].Value;
if (!IsSkip(data)) if (!IsSkip(data))
{ {
LastReadIndex = _curIndex - 1;
return true; return true;
} }
} }
LastReadIndex = _curIndex - 1;
return false; return false;
} }
@ -127,7 +136,7 @@ namespace Luban.Job.Cfg.DataSources.Excel
private object ReadMayNull() private object ReadMayNull()
{ {
return _curIndex <= _toIndex ? _datas[_curIndex++].Value : null; return _curIndex <= _toIndex ? _datas[LastReadIndex = _curIndex++].Value : null;
} }
//public object Read(bool nullable) //public object Read(bool nullable)
@ -142,9 +151,11 @@ namespace Luban.Job.Cfg.DataSources.Excel
var data = _datas[_curIndex++]; var data = _datas[_curIndex++];
if (!IsSkip(data.Value)) if (!IsSkip(data.Value))
{ {
LastReadIndex = _curIndex - 1;
return data; return data;
} }
} }
LastReadIndex = _curIndex - 1;
throw new Exception($"cell:{_datas[_curIndex - 1]} 缺少数据"); throw new Exception($"cell:{_datas[_curIndex - 1]} 缺少数据");
} }
@ -155,9 +166,11 @@ namespace Luban.Job.Cfg.DataSources.Excel
var data = _datas[_curIndex++]; var data = _datas[_curIndex++];
if (!IsSkip(data.Value)) if (!IsSkip(data.Value))
{ {
LastReadIndex = _curIndex - 1;
return data.Value; return data.Value;
} }
} }
LastReadIndex = _curIndex - 1;
throw new Exception($"cell:{_datas[_curIndex - 1]} 缺少数据"); throw new Exception($"cell:{_datas[_curIndex - 1]} 缺少数据");
} }
@ -180,6 +193,7 @@ namespace Luban.Job.Cfg.DataSources.Excel
{ {
if (value == END_OF_LIST) if (value == END_OF_LIST)
{ {
LastReadIndex = _curIndex - 1;
return true; return true;
} }
else else