通过多个工作表设置单元格值

时间:2019-02-27 04:21:03

标签: c# excel vsto office-interop

我有一个股票报价器,它是作为VSTO构建的,可以获取价格,然后将其插入特定工作表中的单元格中。这些工作表是通过按下用户按钮添加的,用户按钮添加了新工作表,对其进行了格式化,然后将其名称添加到“ accountList”工作表(该工作表跟踪所有这些特殊工作表)。

问题在于它仅写入用户添加的最新工作表。这是工作表循环中发生的事情的一小段。

foreach (Excel.Worksheet currentWorksheet in Globals.ThisAddIn.Application.Worksheets)
            {                    
                tickerRange = currentWorksheet.Range["A6:A1000", Type.Missing]; //location of ticker symbols

                //compare the current worksheet name to any of the names in the accountList
                if (Array.IndexOf(accountList.ToArray(), currentWorksheet.Name) >= 0) { 
                //for every row in the specified range
                foreach (Excel.Range row in tickerRange) //for every row inside the tickerRange variable
                {                        
                    try
                    {
                        quoteCell = row.Offset[0, 5]; //location where market price will be inserted
                        string currentStock = row.Value2; //set ticker Symbol equal to the cell of the range element

                        if (string.IsNullOrEmpty(currentStock) || currentStock.Trim().Length > 4) //if there is nothing in the cell or the length is more than 4 characters, don't call the fetchPrice.
                        {
                             badValue = true;
                        }//end if
                        else if (Regex.IsMatch(currentStock, "[ ]|[0-9]")) //if the cell has whitespace or numbers, don't call fetchPrice. This would result in bad output
                        {
                             badValue = true;
                        }
                        else
                        {
                            currentStock = currentStock.ToUpper();
                        }

                        if (!badValue) //if the dictionary contains the ticker symbol, no need to call fetchPrice again this loop, just get the value out of dictionary
                        {
                            price = tickerDictionary[currentStock];
                            quoteCell.Value2 = price;
                            //volumeCell.Value2 = (stockObject.minuteVolume / stockObject.currentVolume)*100;
                            //break;
                        }
                    } //end try
                    catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException rbe) //i forget why i had to catch this in the first place. but it came up once, and now it's caught. so there's that. 
                    {
                        Console.WriteLine("Runtime Binder Exception caught");
                    }//end catch
                    catch (System.Runtime.InteropServices.COMException ce)
                    {
                        stopTicker();
                    }
                } //end 'row' foreach
            } //end 'sheet' foreach

在调试过程中观察局部变量,我可以看到accountList中填充了正确的信息,并且foreach循环正在影响其他工作表,但是quoteCell.Value2 = price不会更新除我最新的工作表之外的任何其他工作表的价格

我在这里想念一些愚蠢的东西吗?

1 个答案:

答案 0 :(得分:0)

我使用this link here发现了问题,它警告不要使用foreach的over COM对象,因为它们的索引访问器不可靠。

在切换了几件事之后,用老式的for循环的ol代替了我的foreach,我开始做生意了。这是更新的代码:

  var trafficLayer = new google.maps.TrafficLayer();
  trafficLayer.setMap(map);
}