使用“LoadfromCollection”和包含其他列表的列表 [英] Use 'LoadfromCollection' with a list containing another list inside
问题描述
我的问题是,我有一个包含弦数和小数的这个名单在另一个列表,像这样的列表:
公共类excelInventario
{
公共excelInventario(){COLS =新的List<&小数GT;); }
公共字符串codigo {搞定;组; }
公共字符串农布雷{搞定;集;}。
公开名单<&小数GT; COLS {搞定;组; } //利斯塔德columnas
公共小数SUMA {搞定;组; }
公共小数股票{搞定;组; }
公共小数diferencia {搞定;组; }
公共小数PRECIO {搞定;组; }
}
和现在我需要把这个在Excel中。问题是,当我使用的方法 LoadFromCollection(MYLIST)
字符串在Excel中显示良好,但小数的名单不把正确的,但是:
System.Collections.Generic.List`1 [System.Decimal]。
块引用>
我能适应这个方法还是我需要使用一个循环和手动行值逐一放?
我怀疑这第二个选项将是低效的。
---------- -----编辑要添加更多的代码--------------
INT tamcolumnas = excelin [0] .cols.Count;使用
(ExcelPackage包=新ExcelPackage(文件))
{
ExcelWorksheet赫亚= package.Workbook.Worksheets.Add(Comparativo unidades contadas VS股票);
hoja.Cells [A1]值=CODART。
hoja.Cells [B1]值=NOMBRE。
的for(int i = 0; I< tamcolumnas;我++)
{hoja.Cells [1,I + 3] .value的=COL+(I + 1); }
变种MYLIST =新的List< excelInventario>();
hoja.Cells.LoadFromCollection(MYLIST,真);
hoja.Cells [2,3] .LoadFromArrays(MyList.Select((R)=> r.cols.Cast&下;对象方式>)ToArray的()));
在最后一行就是失败。
你说:
System.ArgumentOutOfRangeException
指定的参数是外有效值范围。
块引用>
解决方案由于这些是
列表
你可以得到自动化最接近的是LoadFromArray
,因为这些都不是真正的对象。它不完全是漂亮,因为它需要这么铸造检查的性能命中。否则,它可能是最好使用普通的旧环路。这里是我的意思是:
[TestMethod的]
公共无效ListOfList_Test()
{
//http://stackoverflow.com/questions/33825995/how-to-use-loadfromcollection-in-epplus-with-a-list-containing-another-list-insi
//扔在一些数据
变种MYLIST =新的List< TestExtensions.excelInventario>();
为(VAR I = 0;我小于10;我++)
{
无功行=新TestExtensions.excelInventario
{
codigo =路径.GetRandomFileName(),
NOMBRE = i.ToString(),
COLS =新的List<&小数GT; {我,(十进制)(我* 1.5),(十进制)(我* 2.5)}
};
MyList.Add(行);
}
//创建一个测试文件
VAR网络=新的FileInfo(@C:\temp\ListOfList.xlsx);
如果(fi.Exists)
fi.Delete();
INT tamcolumnas = 10; // excelin [0] .cols.Count;使用
(ExcelPackage包=新ExcelPackage(FI))
{
ExcelWorksheet赫亚= package.Workbook.Worksheets.Add(Comparativo unidades contadas VS股票);
hoja.Cells [A1]值=CODART。
hoja.Cells [B1]值=NOMBRE。
表示(INT I = 0; I&下; tamcolumnas;我++)
{
hoja.Cells [1,i + 3中]。价值=COL+第(i + 1);
}
// VAR MYLIST =新的List< TestExtensions.excelInventario>();
hoja.Cells.LoadFromCollection(MYLIST,真);
//hoja.Cells[2 1,3] .LoadFromArrays(MyList.Select((R)=> r.cols.Cast&下;对象方式>)ToArray的()));
hoja.Cells [2,3] .LoadFromArrays(MyList.Select((R)=> r.cols.Cast&下;对象>()ToArray的())。);
package.Save();
}
}
My problem is that I have a list that contains a few strings and inside this list another list of decimals, something like this:
public class excelInventario { public excelInventario() { cols = new List<decimal>); } public string codigo { get; set; } public string nombre { get; set;} . public List<decimal> cols { get; set; } //Lista de columnas public decimal suma { get; set; } public decimal stock { get; set; } public decimal diferencia { get; set; } public decimal precio { get; set; } }
and now I need to put this in Excel. The problem is that when I use the method
LoadFromCollection(MyList)
the strings appear well in Excel, but the list of decimals is not put correctly, but:System.Collections.Generic.List`1[System.Decimal].
Can I adapt this method or do I need to use a loop and put "manually" the row values one by one?
I suspect this second option it will be inefficient.
---------------EDIT TO ADD MORE CODE--------------
int tamcolumnas=excelin[0].cols.Count; using (ExcelPackage package = new ExcelPackage(file)) { ExcelWorksheet hoja = package.Workbook.Worksheets.Add("Comparativo unidades contadas VS stock"); hoja.Cells["A1"].Value = "CODART"; hoja.Cells["B1"].Value = "NOMBRE"; for(int i=0;i<tamcolumnas;i++) { hoja.Cells[1, i+3].Value = "COL"+(i+1); } var MyList = new List<excelInventario>(); hoja.Cells.LoadFromCollection(MyList,true); hoja.Cells[2, 3].LoadFromArrays(MyList.Select((r) => r.cols.Cast<object>).ToArray()));
in this last line is where fails.
Say:
System.ArgumentOutOfRangeException
The specified argument is outside the range of valid values.
解决方案Since those are
Lists
the closest you can get to automation is theLoadFromArray
since those are not true objects. Its not exactly pretty since it requires casting so check for performance hits. Otherwise, it may be best to use plain old loops. Here is what I mean:[TestMethod] public void ListOfList_Test() { //http://stackoverflow.com/questions/33825995/how-to-use-loadfromcollection-in-epplus-with-a-list-containing-another-list-insi //Throw in some data var MyList = new List<TestExtensions.excelInventario>(); for (var i = 0; i < 10; i++) { var row = new TestExtensions.excelInventario { codigo = Path.GetRandomFileName(), nombre = i.ToString(), cols = new List<decimal> {i, (decimal) (i*1.5), (decimal) (i*2.5)} }; MyList.Add(row); } //Create a test file var fi = new FileInfo(@"c:\temp\ListOfList.xlsx"); if (fi.Exists) fi.Delete(); int tamcolumnas = 10; // excelin[0].cols.Count; using (ExcelPackage package = new ExcelPackage(fi)) { ExcelWorksheet hoja = package.Workbook.Worksheets.Add("Comparativo unidades contadas VS stock"); hoja.Cells["A1"].Value = "CODART"; hoja.Cells["B1"].Value = "NOMBRE"; for (int i = 0; i < tamcolumnas; i++) { hoja.Cells[1, i + 3].Value = "COL" + (i + 1); } //var MyList = new List<TestExtensions.excelInventario>(); hoja.Cells.LoadFromCollection(MyList, true); //hoja.Cells[2, 3].LoadFromArrays(MyList.Select((r) => r.cols.Cast<object>).ToArray())); hoja.Cells[2, 3].LoadFromArrays(MyList.Select((r) => r.cols.Cast<object>().ToArray())); package.Save(); } }
这篇关于使用“LoadfromCollection”和包含其他列表的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!