尝试读取大型Excel文件时索引超出范围

时间:2018-02-25 19:49:48

标签: c# arrays excel

您好我正在尝试创建一个文件,该文件从excel文件读取125000 ID和用户名,这些文件必须分隔,根据该文件创建令牌,然后创建确认链接。问题是,在某个时间点(超过30 000次迭代之后),索引超出了范围,没有具体原因。

using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ExcelDataReader;
using Excel = Microsoft.Office.Interop.Excel;

namespace CoRegApp
{
    public class Excel_Creation
    {
        static string[] tokens;

        string emailConfirmationLink;
        public List<string> EmailsList = new List<string>();
        public List<string> userList = new List<string>();
        //Create a variable of the token date

        public Excel_Creation() { }

        public void readExcel()
        {
            string filepath = "batch2.xlsx";
            var tokenDate = DateTime.Now.AddDays(4).Date;
            using (FileStream stream = File.Open(filepath, FileMode.Open, FileAccess.Read))
            {
                using (IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream))
                {
                    DataSet result = excelReader.AsDataSet();
                    DataTable firstTable = result.Tables[0];

                    //StringBuilder sb = new StringBuilder();
                    foreach (DataRow dr in firstTable.Rows)
                    {
                        object[] arr = dr.ItemArray;
                        for (int i = 0; i < arr.Length; i++)
                        {
                            string input = ((Convert.ToString(arr[i])));
                            if (!string.IsNullOrWhiteSpace(input))
                            {
                                tokens = input.Split(';');
                                for (i = 0; i < 1; i++)
                                {
                                    string token = EncryptionHelper.CreateToken(tokens[0], tokens[1], tokenDate);
                                    emailConfirmationLink = "https://blablaconfirmation/Validate?token=" + token + "&blablavalidation2";
                                    EmailsList.Add(emailConfirmationLink);
                                    userList.Add((Convert.ToString(arr[i])));
                                    Console.WriteLine(emailConfirmationLink);
                                }
                            }

                            //tokens =().Split(';'));
                        }
                    }
                    excelReader.Close();
                }
            }
        }

        public void MapToExcel()
        {
            //start excel
            Excel.Application excapp = new Microsoft.Office.Interop.Excel.Application();

            //if you want to make excel visible 
            excapp.Visible = true;

            //create a blank workbook
            var workbook = excapp.Workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);

            //Not done yet. You have to work on a specific sheet - note the cast
            //You may not have any sheets at all. Then you have to add one with NsExcel.Worksheet.Add()
            var sheet = (Excel.Worksheet)workbook.Sheets[1]; //indexing starts from 1

            //now the list
            string cellName;
            int counter = 1;
            foreach (string item in EmailsList)
            {
                cellName = "B" + counter.ToString();
                var range = sheet.get_Range(cellName, cellName);
                range.Value2 = item.ToString();
                ++counter;
            }

            string cellName2;
            int counterB = 1;
            foreach (string item in userList)
            {
                cellName2 = "A" + counterB.ToString();
                var range = sheet.get_Range(cellName2, cellName2);
                range.Value2 = item.ToString();
                ++counterB;
            }
        }//end of mapping method
    }
}

1 个答案:

答案 0 :(得分:1)

您已经重复使用了循环变量&#34; i&#34;在&#34; i&#34;:

的循环中
                    for (int i = 0; i < arr.Length; i++)
                    {
                        ...
                            for (i = 0; i < 1; i++)

线索是你没有必要为内循环声明变量。你应该总是期望在for(或foreach)中声明你的循环变量;或者你可能做错了什么。

在这种情况下,会发生什么,它将进入外循环,设置&#34; i&#34;为零,检查我是否小于arr.Length;做一些其他的东西,如果条件是正确的,它将进入内循环(重新设置&#34; i&#34;为零,检查它小于1,内循环的内容,增量i(因为内循环),从该循环中掉出,到达外循环的末尾,再次递增i(所以现在它是2),并且在可能再次循环之前检查arr.Length。 / p>

这个内部循环实际上是没有意义的,因为它总是只进行一次,所以我建议删除该循环,并将引用修复为&#34; i&#34;在其中要么是0,要么留在&#34;我&#34 ;;取决于你的意图(因为它暧昧,我试图引用它)。

如果我可以建议您始终提供变量名称,您可能会发现它不仅有助于阻止您这样做;但它会使你的代码更具可读性。

如果它有帮助,你可以认为for循环就像一个像这样编码的while循环......

                int i = 0;
                while(i < arr.Length)
                {
                    ...
                    i++;
                }

但你已经和#34; i&#34;在&#34; ...&#34;一部分。

编辑:附加:

tokens = input.Split(';');
...
string token = EncryptionHelper.CreateToken(tokens[0], tokens[1], tokenDate);

但在使用索引器之前,没有检查令牌中有多少项。

相关问题