range.select()给出错误

时间:2016-06-30 09:57:02

标签: c# office-interop worksheet

我是C#的初级用户,我正在尝试使用特定标头存储excel文件中列的值。要查找标题,我正在查找所有列和行并搜索名称。 Buut我在使用FindRange.Select();命令的行上收到错误。在调试期间,当它到达线路时,它会返回到线路  FindRange = xlWorksheet[0].UsedRange.Columns[columnIndex];

(Ps。我从其他在线资源中获取了代码)

这是代码;

int columnCount = xlWorksheet[0].UsedRange.Columns.Count;
                    List<int>[] columnValue = new List<int>[columnCount];
                Excel.Range rngResult = null;
                Excel.Range rng = null;
                int index = 0;

            int rowCount = xlWorksheet[0].UsedRange.Rows.Count;
            Excel.Range FindRange = null;

            for (int columnIndex = 1; columnIndex <= xlWorksheet[0].UsedRange.Columns.Count; columnIndex++)
            {
                FindRange = xlWorksheet[0].UsedRange.Columns[columnIndex];

                try
                {
                    FindRange.Select();
                    rngResult = FindRange.Find(What: "Satış", LookIn: Excel.XlFindLookIn.xlValues, LookAt: Excel.XlLookAt.xlPart, SearchOrder: Excel.XlSearchOrder.xlByRows);

                    if (rngResult != null)
                    {
                        columnValue[index] = new List<int>();

                        for (int rowIndex = 1; rowIndex <= xlWorksheet[0].UsedRange.Rows.Count; rowIndex++)
                        {
                            rng = xlWorksheet[0].UsedRange[rowIndex, columnIndex] as Excel.Range;
                            if (rng.Value != null)
                            {
                                columnValue[index].Add(rng.Value.ToString());

                            }

                            index++;
                        }
                        Array.Resize(ref columnValue, index);
                    }

                }

                catch (Exception E)
                {
                    if (E.InnerException != null)
                    {
                        string err = E.InnerException.Message;
                    }
                }

            }

0 个答案:

没有答案