Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading columns in excel to c#

Tags:

c#

excel

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);

            }
        }
    }
}
like image 746
pastel Avatar asked Nov 27 '25 05:11

pastel


1 Answers

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();

                        }


                    }


                }

        }
like image 130
pastel Avatar answered Nov 28 '25 19:11

pastel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!