Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 尝试读取大型excel文件时索引超出范围_C#_Arrays_Excel - Fatal编程技术网

C# 尝试读取大型excel文件时索引超出范围

C# 尝试读取大型excel文件时索引超出范围,c#,arrays,excel,C#,Arrays,Excel,您好,我正在尝试创建一个文件,该文件从excel文件中读取125000个ID和用户名,这些ID和用户名必须进行分隔,并在此基础上创建令牌,然后创建确认链接。问题在于,在某个时间点(超过30000次迭代之后),该指数没有具体原因而超出范围 using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using System.Text; using Sys

您好,我正在尝试创建一个文件,该文件从excel文件中读取125000个ID和用户名,这些ID和用户名必须进行分隔,并在此基础上创建令牌,然后创建确认链接。问题在于,在某个时间点(超过30000次迭代之后),该指数没有具体原因而超出范围

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
    }
}
使用系统;
使用System.Collections.Generic;
使用系统数据;
使用System.IO;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用ExcelDataReader;
使用Excel=Microsoft.Office.Interop.Excel;
名称空间CoRegApp
{
创建公共类Excel_
{
静态字符串[]标记;
字符串电子邮件确认链接;
public List EmailsList=新列表();
public List userList=new List();
//创建令牌日期的变量
公共Excel_创建(){}
public void readExcel()
{
字符串filepath=“batch2.xlsx”;
var tokenDate=DateTime.Now.AddDays(4.Date);
使用(FileStream stream=File.Open(filepath,FileMode.Open,FileAccess.Read))
{
使用(IExcelDataReader excelReader=ExcelReaderFactory.CreateOpenXmlReader(流))
{
数据集结果=excelReader.AsDataSet();
DataTable firstTable=result.Tables[0];
//StringBuilder sb=新的StringBuilder();
foreach(firstTable.Rows中的数据行dr)
{
对象[]arr=dr.ItemArray;
对于(int i=0;i
您在“i”的循环中重用了循环变量“i”:

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

谢谢您的帮助

事实上,这个问题有点愚蠢。即使我的数组总是被重置为只包含索引[0]和[1]的两项。硬编码以访问这些值似乎会使循环在某个时间点超出范围。我真的不知道。因此,只需更换
string-token=EncryptionHelper.CreateToken(标记[0],标记[1],标记日期)


解决了这个问题

我可以向你保证,肯定有一个具体的原因。调试您的程序,查看中断时的值,并将其与您在代码中所做的任何假设进行比较。我想您还没有阅读我答案的最后一句话,这将解释您“不知道”的原因。另外,你知道你可以把一些东西标记为答案吗?我花了时间和精力在你的代码中查找了2个错误,而其他人则希望搁置你的问题。如果这不够清楚:你如何知道令牌数组中有足够的项可以使用“counter”值作为索引。
                    for (int i = 0; i < arr.Length; i++)
                    {
                        ...
                            for (i = 0; i < 1; i++)
                int i = 0;
                while(i < arr.Length)
                {
                    ...
                    i++;
                }
tokens = input.Split(';');
...
string token = EncryptionHelper.CreateToken(tokens[0], tokens[1], tokenDate);
int counter1 = counter;
counter1--;
string token = EncryptionHelper.CreateToken(tokens[counter1], tokens[counter], tokenDate);