【问题标题】:For Loop in C# not loopingC#中的for循环不循环
【发布时间】:2011-02-23 15:59:42
【问题描述】:

我有一个实际上不循环的循环问题。我在下面发布了我的代码的简化版本。基本上,使用 NPOI excel 库,我有一个 excel 文件,其中包含第一张和第二张工作表上的数据,所以我需要做一个循环来通过两个工作表。

以下是我到目前为止所做的,但这仅适用于第一张纸然后退出。它无法增加变量 w。如您所见,此代码中还实现了其他循环,它们运行良好,所以我不明白。

这是漫长的一天,也许我错过了一些非常简单的事情。我可能把它放错了或什么的。如果其他人能发现我可能做错了什么,我将不胜感激:)

public class SalesFileProcessor : ISalesProcessor
    {
        public List<FTPSalesRow> ProcessSalesFile(string filename)
        {
            try
            {

            using (FileStream fs = File.Open(filename, FileMode.Open, FileAccess.Read))
            {
                int numberOfSheets = 2;
//Loop through sheets - does not work
                for (int w = 0; w <= numberOfSheets; w++)
                {
                    HSSFWorkbook templateWorkbook = new HSSFWorkbook(fs);

                    HSSFSheet sheet = templateWorkbook.GetSheetAt(w);
                    HSSFRow row = null;


                    for (int i = 1; i <= sheet.LastRowNum; i++)
                    {
                        FTPSalesDetails t = null;
                        int currentColumn = 0;

                        try
                        {
                            ModelContainer ctn = new ModelContainer();

                            row = sheet.GetRow(i);

                            if (row == null)
                            {
                                continue;
                            }

                            t = new FTPSalesDetails
                            {
                                RowNumber = i,
                                InvoiceDate = GetCellValue(row.GetCell(0)),
                                CountrySoldIn = GetCellValue(row.GetCell(1)),
                                NetUnitsSold = GetCellValue(row.GetCell(2)),
                                Item = GetCellValue(row.GetCell(3)),
                                ProductCode = GetCellValue(row.GetCell(5)),
                            };

                            if (t.ProductCode == null && t.NetUnitsSold == null)
                            {
                                return null;
                            }

                            int Qty = int.Parse(t.NetUnitsSold);


                            for (int x = 0; x < Qty; x++)
                            {
                                ItemSale ts = new ItemSale
                                {
                                    ItemID = GetItemID(t.ProductCode),
                                    ManufacturerID = GetManufacturerID("Samsung"),
                                    DateSold = DateTime.Now,
                                };

                                ctn.AddToItemSales(ts);
                                ctn.SaveChanges();
                            }
                        }
                        catch (IndexOutOfRangeException) { }
                    }
                } //End Loop - the one that doesn't work
            }
        }
        catch (IOException exp)
        {
            throw new FTPSalesFileProcessingException("Could not open the Sales data file", exp);
        }
        catch (Exception exp)
        {
            throw new FTPSalesFileProcessingException("An unknown eror occured during processing the file", exp);
        }

        return null;
    }

【问题讨论】:

  • 你试过调试吗?看我和w? :)
  • @Armen,我当然做到了。 numberOfSheets 如您所料,w 似乎保持为零,而 i 正常工作。
  • 您是否单步执行此代码以验证所有值均按预期设置?

标签: c# asp.net for-loop


【解决方案1】:
 if (t.ProductCode == null && t.NetUnitsSold == null)
  {
      return null;
  }

我猜这是被击中了,导致你的整个函数退出。如果您试图退出 for 循环的迭代,请尝试使用 break; 代替,或者按照 Mike M 在 cmets 中指出的那样继续。

【讨论】:

  • 你是对的,这正是正在发生的事情。我得重新考虑一下。理想情况下,“for (int i = 1; i
  • 我还要猜测,如果当前行为空,他可能只想继续到下一行,而不一定要移动到下一张。在这种情况下,您应该使用 continue;不破;。当然,我们都只是猜测:)
  • @Mike M,是的,也许我认为他现在已经弄清楚了。
【解决方案2】:

对于您所说的,假设您的变量都正常,即循环不为空...您是否检查过您在第一次迭代中没有达到这一行?

if (t.ProductCode == null && t.NetUnitsSold == null)
                            {
                                return null;
                            }

【讨论】:

    【解决方案3】:

    查看代码,唯一让我印象深刻的是:

    HSSFSheet sheet = templateWorkbook.GetSheetAt(w);
    HSSFRow row = null;
    
    for (int i = 1; i <= sheet.LastRowNum; i++)
    

    我猜 sheet.LastRowNum 要么等于 0 要么等于 1。

    【讨论】:

      【解决方案4】:

      也许会抛出 indexOutOfRangeException 并且那是因为您只有一次迭代,或者您应该使用

      【讨论】:

        猜你喜欢
        • 2013-11-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-10-20
        • 2011-10-18
        • 1970-01-01
        • 2020-01-27
        相关资源
        最近更新 更多