如何找到在List中保留一些值的属性的位置

时间:2016-05-05 14:57:05

标签: c# asp.net excel

//这里我想知道List Flux中每个属性的索引,它从excel中获取值,因为稍后我必须从List中获取这些属性的值。我面临的问题是有时候某些属性在执行期间在指定的行中没有任何值,因此我无法以Flux [0] .VMS,OnHold [0] .VMS,Active [0] .VMS等形式获取它们。索引是模棱两可,所以我需要知道List中每个属性的索引,它具有一定的价值。

    List<Summary> Flux = new List<Summary>();//total influx of each property

                    if (str1 == "Influx" && str1 != "Grand Total")
                    {
                        for (rCnt = rCnt + 7; rCnt < range.Rows.Count; rCnt++)
                        {
                            str = (string)(range.Cells[rCnt, 1] as Excel.Range).Text;
                            if (str == "VMS")
                            {
                                Summary summary = new Summary();
                                summary.VMS = xlWorkSheet.Cells[rCnt, 5].Text;

                                Flux.Add(summary);

                            }
                            if (str == "eService")
                            {
                                Summary summary = new Summary();
                                summary.eService = xlWorkSheet.Cells[rCnt, 5].Text;

                                Flux.Add(summary);

                            }
                            if (str == "RES")
                            {
                                Summary summary = new Summary();
                                summary.Res = xlWorkSheet.Cells[rCnt, 5].Text;

                                Flux.Add(summary);
                            }
                            if (str == "RSC")
                            {
                                Summary summary = new Summary();
                                summary.RSC = xlWorkSheet.Cells[rCnt, 5].Text;

                                Flux.Add(summary);
                            }
                            if (str == "VFS")
                            {
                                Summary summary = new Summary();
                                summary.VFS = xlWorkSheet.Cells[rCnt, 5].Text;

                                Flux.Add(summary);
                            }
                            if (str == "SYS 80")
                            {
                                Summary summary = new Summary();
                                summary.System80 = xlWorkSheet.Cells[rCnt, 5].Text;

                                Flux.Add(summary);
                            }

                            if (str == "HCL")
                            {
                                Summary summary = new Summary();
                                summary.HCL = xlWorkSheet.Cells[rCnt, 5].Text;

                                Flux.Add(summary);
                            }
                            if (str == "Others")
                            {
                                Summary summary = new Summary();
                                summary.Others = xlWorkSheet.Cells[rCnt, 5].Text;

                                Flux.Add(summary);
                            }

                        }
                        break;
                    }

     //while fetching the vaues of each property having some value is the below code,instead of hardcoding the index of each property which has some value, i want to store it somewhere and then fetch it while writing the values in a cell.

 xlWorkSheet.Cells[iCount, jCount] = Flux[0].VMS;        //Daily influx
xlWorkSheet.Cells[iCount + 2, jCount] = Convert.ToString(Convert.ToInt32(OnHold[0].VMS)-Convert.ToInt32(xlWorkSheet.Cells[iCount+4,jCount-1].Text)); //Tickets moved to hold    
xlWorkSheet.Cells[iCount+3, jCount] = Active[0].VMS;     // Ending Backlog
xlWorkSheet.Cells[iCount + 4, jCount] = OnHold[0].VMS;   //Ending Hold

1 个答案:

答案 0 :(得分:0)

因为您说在执行期间只分配了1个属性,我首先想到的是执行以下操作。但这取决于您填写ActiveOnHold的方式;你应该改变那些工作,就像我让这个工作:

Summary summary = new Summary();

                if (str1 == "Influx" && str1 != "Grand Total")
                {
                    for (rCnt = rCnt + 7; rCnt < range.Rows.Count; rCnt++)
                    {
                        str = (string)(range.Cells[rCnt, 1] as Excel.Range).Text;
                        if (str == "VMS")
                        {                                
                            summary.VMS = xlWorkSheet.Cells[rCnt, 5].Text;
                        }
                        if (str == "eService")
                        {
                            summary.eService = xlWorkSheet.Cells[rCnt, 5].Text;
                        }
                        if (str == "RES")
                        {
                            summary.Res = xlWorkSheet.Cells[rCnt, 5].Text;
                        }
                        if (str == "RSC")
                        {
                            summary.RSC = xlWorkSheet.Cells[rCnt, 5].Text;
                        }
                        if (str == "VFS")
                        {
                            summary.VFS = xlWorkSheet.Cells[rCnt, 5].Text;
                        }
                        if (str == "SYS 80")
                        {
                            summary.System80 = xlWorkSheet.Cells[rCnt, 5].Text;
                        }

                        if (str == "HCL")
                        {
                            summary.HCL = xlWorkSheet.Cells[rCnt, 5].Text;

                        }
                        if (str == "Others")
                        {
                            summary.Others = xlWorkSheet.Cells[rCnt, 5].Text;
                        }

                    }
                    break;
                }

 //while fetching the vaues of each property having some value is the below code,instead of hardcoding the index of each property which has some value, i want to store it somewhere and then fetch it while writing the values in a cell.

xlWorkSheet.Cells[iCount, jCount] = summary.VMS;        //Daily influx
xlWorkSheet.Cells[iCount + 2, jCount] = Convert.ToString(Convert.ToInt32(OnHold[0].VMS)-Convert.ToInt32(xlWorkSheet.Cells[iCount+4,jCount-1].Text)); //Tickets moved to hold    
xlWorkSheet.Cells[iCount+3, jCount] = Active[0].VMS;     // Ending Backlog
xlWorkSheet.Cells[iCount + 4, jCount] = OnHold[0].VMS;   //Ending Hold