繁体   English   中英

C#中的For循环不循环

[英]For Loop in C# not looping

我有一个实际上没有循环的循环问题。 我在下面发布了我的代码的简化版本。 基本上,使用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;
    }
 if (t.ProductCode == null && t.NetUnitsSold == null)
  {
      return null;
  }

我猜这被击中了,导致整个功能退出。 如果您要退出for循环的该迭代,请尝试break; 而是继续执行,如Mike M在评论中指出的。

对于您所说的,假设您的变量都可以,那是循环不为空...您是否检查过第一次迭代中未达到这一行?

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

查看代码,对我来说唯一显而易见的是:

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

for (int i = 1; i <= sheet.LastRowNum; i++)

我猜那sheet.LastRowNum等于0或1。

也许会抛出indexOutOfRangeException,那是因为您只有一次迭代,或者不是<=而是使用<。 工作表编号是否从零开始?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM