EPPlus - InsertRow: Row cannot be less than 1. 参数名称:值
EPPlus - InsertRow: Row cannot be less than 1. Parameter name: value
我有一个 Excel 模板,其中有一个 table(范围:A4:AM5),我需要通过 EPPlus 插入行来扩展它。我的代码在文件中插入单元格值没有问题,但我需要根据第一列中插入的每个新值扩展 table 的行。
我尝试使用 InsertRow(5,1)
方法,但它显示异常
"System.ArgumentOutOfRangeException: 'Row cannot be less than 1.
Parameter name: value'"
并且值呈现:
{System.ArgumentOutOfRangeException: Row cannot be less than 1. Parameter name: value > at OfficeOpenXml.ExcelCellAddress.set_Row(Int32 value) > at OfficeOpenXml.ExcelNamedRangeCollection.InsertRows(Int32 rowFrom, Int32 rows, ExcelNamedRange namedRange) > at OfficeOpenXml.ExcelNamedRangeCollection.Insert(Int32 rowFrom, Int32 colFrom, Int32 rows, Int32 cols, Func`2 filter) > at OfficeOpenXml.ExcelWorksheet.InsertRow(Int32 rowFrom, Int32 rows, Int32 copyStylesFromRow) > at TestInsertRow.Program.Main(String[] args)}
我将代码的功能最小化到这个也显示相同异常的代码中:
class Dnp3
{
private static ExcelPackage _dnp3Package;
private static ExcelWorksheet _worksheet1;
private static FileInfo _templateInfo;
private static FileInfo _newDnp3FileInfo;
public static bool TempFile
{
get
{
if (_templateInfo != null) return true;
return false;
}
set
{
if (value == true)
{
_templateInfo = new FileInfo(@" Existing template path ");
}
}
}
public static bool NewFile
{
get
{
if (_newDnp3FileInfo != null) return true;
return false;
}
set
{
if (value == true)
{
_newDnp3FileInfo = new FileInfo(@" Existing new file path ");
_dnp3Package = new ExcelPackage(_newDnp3FileInfo, _templateInfo);
{
_worksheet1 = Dnp3._dnp3Package.Workbook.Worksheets["DNP3_RTUs"];
{
_worksheet1.DataValidations.Clear();
}
}
}
}
}
public static bool Save
{
set
{
if (value == true)
{
Dnp3._dnp3Package.Save();
}
}
}
public class DNP3_RTUs : Dnp3
{
private int _idobj_nameCol;//IDOBJ_NAME
private int _idobj_aliasCol;//IDOBJ_ALIAS
private int _idobj_customidCol;//IDOBJ_CUSTOMID
private int _idobj_aorgroupCol;//IDOBJ_AORGROUP
public bool Header
{
set
{
int _column = 1;
_idobj_nameCol = 1;
_idobj_aliasCol = 1;
_idobj_customidCol = 1;
_idobj_aorgroupCol = 1;
if (value == true)
{
while (_worksheet1.Cells[Row: 3, Col: _column].Value != null)
{
switch (_worksheet1.Cells[Row: 3, Col: _column].Value)
{
case "IDOBJ_NAME":
_idobj_nameCol = _column;
break;
case "IDOBJ_ALIAS":
_idobj_aliasCol = _column;
break;
case "IDOBJ_CUSTOMID":
_idobj_customidCol = _column;
break;
case "IDOBJ_AORGROUP":
_idobj_aorgroupCol = _column;
break;
}
_column++;
}
}
}
}
public bool AddRow(int _line)
{
_line = _line + 5;
_worksheet1.InsertRow(_line, 2);
return false;
}
public string IDOBJ_NAME(int _line, string _data)
{
_line = _line + 5;//Pq a entrada de dados começa na linha
_worksheet1.Cells[_line, _idobj_nameCol].Value = _data;
_worksheet1.Cells[_line, _idobj_nameCol].Style.Font.Size = 11;
_worksheet1.Cells[_line, _idobj_nameCol].Style.Font.Name = "Calibri";
return "";
}
public string IDOBJ_ALIAS(int _line, string _data)
{
_line = _line + 5;//Pq a entrada de dados começa na linha 5
_worksheet1.Cells[_line, _idobj_aliasCol].Value = _data;
_worksheet1.Cells[_line, _idobj_aliasCol].Style.Font.Size = 11;
_worksheet1.Cells[_line, _idobj_aliasCol].Style.Font.Name = "Calibri";
return "";
}
public string IDOBJ_CUSTOMID(int _line, string _data)
{
_line = _line + 5;//Pq a entrada de dados começa na linha 5
_worksheet1.Cells[_line, _idobj_customidCol].Value = _data;
_worksheet1.Cells[_line, _idobj_customidCol].Style.Font.Size = 11;
_worksheet1.Cells[_line, _idobj_customidCol].Style.Font.Name = "Calibri";
return "";
}
public string IDOBJ_AORGROUP(int _line, string _data)
{
_line = _line + 5;//Pq a entrada de dados começa na linha 5
_worksheet1.Cells[_line, _idobj_aorgroupCol].Value = _data;
_worksheet1.Cells[_line, _idobj_aorgroupCol].Style.Font.Size = 11;
_worksheet1.Cells[_line, _idobj_aorgroupCol].Style.Font.Name = "Calibri";
return "";
}
}
}
class Program
{
static void Main(string[] args)
{
Dnp3.TempFile = true;
Dnp3.NewFile = true;
Dnp3.DNP3_RTUs dNP3_RTUs = new Dnp3.DNP3_RTUs() { Header = true };
int _mappingCount = 1;
dNP3_RTUs.IDOBJ_NAME(_mappingCount, "BOQ_1");
dNP3_RTUs.IDOBJ_ALIAS(_mappingCount,"nome_se_ordem");
dNP3_RTUs.IDOBJ_AORGROUP(_mappingCount, "mnem_se");
dNP3_RTUs.AddRow(_mappingCount);
Dnp3.Save = true;
}
}
我遇到了同样的异常,并且能够通过修复 excel 模板名称集合来解决它。
按 Ctrl+F3(名称管理器)并搜索包含任何#REF 的名称!值和引荐来源网址并将其从模板中删除。
如果 Ctrl + F3 无法找到名称管理器
将鼠标悬停在“sheet.Names”上,您将找到所有名称管理员和
你可以删除它们抛出代码
var refName = sheet.Names.FirstOrDefault(x => x.Name == "nameOfNameManager");
if (refName != null)
{
sheet.Names.Remove("nameOfNameManager");
}
我有一个 Excel 模板,其中有一个 table(范围:A4:AM5),我需要通过 EPPlus 插入行来扩展它。我的代码在文件中插入单元格值没有问题,但我需要根据第一列中插入的每个新值扩展 table 的行。
我尝试使用 InsertRow(5,1)
方法,但它显示异常
"System.ArgumentOutOfRangeException: 'Row cannot be less than 1. Parameter name: value'"
并且值呈现:
{System.ArgumentOutOfRangeException: Row cannot be less than 1. Parameter name: value > at OfficeOpenXml.ExcelCellAddress.set_Row(Int32 value) > at OfficeOpenXml.ExcelNamedRangeCollection.InsertRows(Int32 rowFrom, Int32 rows, ExcelNamedRange namedRange) > at OfficeOpenXml.ExcelNamedRangeCollection.Insert(Int32 rowFrom, Int32 colFrom, Int32 rows, Int32 cols, Func`2 filter) > at OfficeOpenXml.ExcelWorksheet.InsertRow(Int32 rowFrom, Int32 rows, Int32 copyStylesFromRow) > at TestInsertRow.Program.Main(String[] args)}
我将代码的功能最小化到这个也显示相同异常的代码中:
class Dnp3
{
private static ExcelPackage _dnp3Package;
private static ExcelWorksheet _worksheet1;
private static FileInfo _templateInfo;
private static FileInfo _newDnp3FileInfo;
public static bool TempFile
{
get
{
if (_templateInfo != null) return true;
return false;
}
set
{
if (value == true)
{
_templateInfo = new FileInfo(@" Existing template path ");
}
}
}
public static bool NewFile
{
get
{
if (_newDnp3FileInfo != null) return true;
return false;
}
set
{
if (value == true)
{
_newDnp3FileInfo = new FileInfo(@" Existing new file path ");
_dnp3Package = new ExcelPackage(_newDnp3FileInfo, _templateInfo);
{
_worksheet1 = Dnp3._dnp3Package.Workbook.Worksheets["DNP3_RTUs"];
{
_worksheet1.DataValidations.Clear();
}
}
}
}
}
public static bool Save
{
set
{
if (value == true)
{
Dnp3._dnp3Package.Save();
}
}
}
public class DNP3_RTUs : Dnp3
{
private int _idobj_nameCol;//IDOBJ_NAME
private int _idobj_aliasCol;//IDOBJ_ALIAS
private int _idobj_customidCol;//IDOBJ_CUSTOMID
private int _idobj_aorgroupCol;//IDOBJ_AORGROUP
public bool Header
{
set
{
int _column = 1;
_idobj_nameCol = 1;
_idobj_aliasCol = 1;
_idobj_customidCol = 1;
_idobj_aorgroupCol = 1;
if (value == true)
{
while (_worksheet1.Cells[Row: 3, Col: _column].Value != null)
{
switch (_worksheet1.Cells[Row: 3, Col: _column].Value)
{
case "IDOBJ_NAME":
_idobj_nameCol = _column;
break;
case "IDOBJ_ALIAS":
_idobj_aliasCol = _column;
break;
case "IDOBJ_CUSTOMID":
_idobj_customidCol = _column;
break;
case "IDOBJ_AORGROUP":
_idobj_aorgroupCol = _column;
break;
}
_column++;
}
}
}
}
public bool AddRow(int _line)
{
_line = _line + 5;
_worksheet1.InsertRow(_line, 2);
return false;
}
public string IDOBJ_NAME(int _line, string _data)
{
_line = _line + 5;//Pq a entrada de dados começa na linha
_worksheet1.Cells[_line, _idobj_nameCol].Value = _data;
_worksheet1.Cells[_line, _idobj_nameCol].Style.Font.Size = 11;
_worksheet1.Cells[_line, _idobj_nameCol].Style.Font.Name = "Calibri";
return "";
}
public string IDOBJ_ALIAS(int _line, string _data)
{
_line = _line + 5;//Pq a entrada de dados começa na linha 5
_worksheet1.Cells[_line, _idobj_aliasCol].Value = _data;
_worksheet1.Cells[_line, _idobj_aliasCol].Style.Font.Size = 11;
_worksheet1.Cells[_line, _idobj_aliasCol].Style.Font.Name = "Calibri";
return "";
}
public string IDOBJ_CUSTOMID(int _line, string _data)
{
_line = _line + 5;//Pq a entrada de dados começa na linha 5
_worksheet1.Cells[_line, _idobj_customidCol].Value = _data;
_worksheet1.Cells[_line, _idobj_customidCol].Style.Font.Size = 11;
_worksheet1.Cells[_line, _idobj_customidCol].Style.Font.Name = "Calibri";
return "";
}
public string IDOBJ_AORGROUP(int _line, string _data)
{
_line = _line + 5;//Pq a entrada de dados começa na linha 5
_worksheet1.Cells[_line, _idobj_aorgroupCol].Value = _data;
_worksheet1.Cells[_line, _idobj_aorgroupCol].Style.Font.Size = 11;
_worksheet1.Cells[_line, _idobj_aorgroupCol].Style.Font.Name = "Calibri";
return "";
}
}
}
class Program
{
static void Main(string[] args)
{
Dnp3.TempFile = true;
Dnp3.NewFile = true;
Dnp3.DNP3_RTUs dNP3_RTUs = new Dnp3.DNP3_RTUs() { Header = true };
int _mappingCount = 1;
dNP3_RTUs.IDOBJ_NAME(_mappingCount, "BOQ_1");
dNP3_RTUs.IDOBJ_ALIAS(_mappingCount,"nome_se_ordem");
dNP3_RTUs.IDOBJ_AORGROUP(_mappingCount, "mnem_se");
dNP3_RTUs.AddRow(_mappingCount);
Dnp3.Save = true;
}
}
我遇到了同样的异常,并且能够通过修复 excel 模板名称集合来解决它。
按 Ctrl+F3(名称管理器)并搜索包含任何#REF 的名称!值和引荐来源网址并将其从模板中删除。
如果 Ctrl + F3 无法找到名称管理器
将鼠标悬停在“sheet.Names”上,您将找到所有名称管理员和
你可以删除它们抛出代码
var refName = sheet.Names.FirstOrDefault(x => x.Name == "nameOfNameManager");
if (refName != null)
{
sheet.Names.Remove("nameOfNameManager");
}