pastel
pastel

Reputation: 51

Reading columns in excel to c#

Hi I am trying to read an excel file to c# but i want to convert column data to a string array and then to a string. I think i have done that here but am not sure. Anyways when i try to read the header of the excel column for my second if statement i get an error saying "Index was outside the bounds of the array.". I am kind of a beginner and thus don't understand what i am doing wrong.

if (xlApp != null)
{
    //Doing excel things
    int i = SheetNum;
    Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(filename, 0, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true);
    Excel.Sheets sheets = xlWorkbook.Worksheets;
    Excel.Worksheet worksheet = (Excel.Worksheet)sheets.get_Item(i);
    Excel.Range usedRange = worksheet.UsedRange;
    Excel.Range colRange = usedRange.Columns;
    Excel.Range firstRow = worksheet.UsedRange.Rows[1];

    int rowCount = colRange.Rows.Count;
    int colCount = colRange.Columns.Count;

    for (int m = 1; m < colCount; m++)
    {
        for (int n = 0; n <= rowCount; n++)
        {
            System.Array myHeadvalues = (System.Array)firstRow.Cells.Value;
            string[] HeaderData = myHeadvalues.OfType<object>().Select(o => o.ToString()).ToArray();
            Excel.Range some = worksheet.UsedRange.Columns[m];
            System.Array myNamevalues = (System.Array)some.Cells.Value;
            string[] result1 = myNamevalues.OfType<object>().Select(o => o.ToString()).ToArray();

            //String types
            if (result1.Contains("Name") && HeaderData.Contains("Name"))
            {
                string tagval = HeaderData[n].ToString() + ".Array" + 
                string[] result = result1.Skip(1).ToArray();
                string Inputs = result[n + 1].ToString();
                System.Console.WriteLine(tagval, Inputs);

            }

            //Error Appears here//
            else if (result1.Contains("Description") && HeaderData.Contains("Description"))
            {
                string tagval = HeaderData[n].ToString() + ".Array" + 
                string[] result = result1.Skip(1).ToArray();
                string Inputs = result[n].ToString();
                System.Console.WriteLine(tagval, Inputs);

            }
        }
    }
}

Upvotes: 3

Views: 3917

Answers (2)

Alexei - check Codidact
Alexei - check Codidact

Reputation: 23078

It is off-topic (I cannot comment yet), but it might be useful: it looks like you are using automation or something similar. I recommend using a library like ClosedXML (this is free), as it does need the Excel application to read and write Excel files. In most of the cases, the library will do the work faster than the Excel application.

Upvotes: 1

pastel
pastel

Reputation: 51

Btw i fixed my code now it reads across columns with no issue. If there is anyway i can speed it up please let me know. Thanks.

List<string> ColumnValues = new List<string>();
                for (int n = 2; n < rowCount; n++)
                {
                    for (int m = 1; m < colCount; m++)
                    {
                        Excel.Range some = worksheet.UsedRange.Columns[m];

                        System.Array myvalues = (System.Array)some.Cells.Value;
                        string[] Data = myvalues.OfType<object>().Select(o => o.ToString()).ToArray();
                        ColumnValues = Data.ToList();
                        System.Console.WriteLine(ColumnValues);
                        //String Types
                        if (ColumnValues.Contains("Name"))
                        {
                            string tagval = "Product_Array[0].Name";
                            string[] result = ColumnValues.Skip(1).ToArray();

                        }


                    }


                }

        }

Upvotes: 2

Related Questions