locked
Import data from Excel-file using C# RRS feed

  • Question

  • Hi!

    Plz explaine me how to load data from excel -file to array using C#?

    Thx for answers!
    Wednesday, February 18, 2009 4:28 PM

Answers

  • taras1989 said:

    well I still need to import data from excel file into my array....

    I'm not saying that your solution won't work for what you're trying to do.  I'm simply saying you may want to pay attention to how you dispose of the COM objects from the Microsoft.Office.Interop.Excel namespace in order to make sure that you get the right value.

    As for the double issue, change the line that says "my_arry[i] = oRange.Text.ToString();" to the following few lines:

    double value = 0;
    double.TryParse(oRange.Text.ToString(), out value);
    my_arry[i] = value;
        

    David Morton - http://blog.davemorton.net/
    Wednesday, February 18, 2009 9:38 PM
    Moderator
  • I've solved my problem!Thx to everyone who helped me!!
    Thursday, February 19, 2009 8:51 AM

All replies

  • I posted some sample code here: Reading an Excel spreadsheet using ADO.NET.

    Alternatively, you could use Excel interop to control Excel directly.

           -Steve
    Wednesday, February 18, 2009 8:12 PM
  • Thx for answer!but I need smth simple...I've found the example whcich is the best suit to my problem:

                      object oMissing = System.Reflection.Missing.Value;

                Excel.ApplicationClass oExcel = new Excel.ApplicationClass();
                Excel.Workbooks oBooks = oExcel.Workbooks;
                Excel._Workbook oBook = null;
                oBook = oBooks.Open(@"D:\Data.xls", oMissing, oMissing,oMissing, oMissing, oMissing, oMissing, oMissing, oMissing,oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);

                Excel.Range oRange; // диапазон
                Excel.Worksheet oSheet; // страница книги                
                oSheet = (Excel.Worksheet)oBook.Worksheets[2]; //переходим на 1-ю страницу

                int i = 6;
                for (i = 5; i <= 16; i++){
                    //oRange = oSheet.get_Range("A" + i, "A" + i);
                    oRange = oSheet.get_Range("E" + i, Type.Missing);
                    my_arry[i] = oRange.Text.ToString;            
                }

                oRange = null;
                oSheet = null;
                oBook = null;
                oBooks = null;
                //oExcel.UserControl = true;
                oExcel.Quit();
                oExcel = null;
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
        }
    the problem is how to develope this code to excel-file which has only "double" variables?
    Wednesday, February 18, 2009 8:37 PM
  • You're likely to have some issues with Excel staying open in the background using this code.  You may want to check out the following link for how to properly handle the interop objects:

    Excel Not closing after starting C# [.NET] Thread
    David Morton - http://blog.davemorton.net/
    Wednesday, February 18, 2009 9:08 PM
    Moderator
  • well I still need to import data from excel file into my array....
    Wednesday, February 18, 2009 9:31 PM
  • taras1989 said:

    well I still need to import data from excel file into my array....

    I'm not saying that your solution won't work for what you're trying to do.  I'm simply saying you may want to pay attention to how you dispose of the COM objects from the Microsoft.Office.Interop.Excel namespace in order to make sure that you get the right value.

    As for the double issue, change the line that says "my_arry[i] = oRange.Text.ToString();" to the following few lines:

    double value = 0;
    double.TryParse(oRange.Text.ToString(), out value);
    my_arry[i] = value;
        

    David Morton - http://blog.davemorton.net/
    Wednesday, February 18, 2009 9:38 PM
    Moderator
  • file:///D:/Data.xls

    well I still have problems.....I am trying to calculate sum of 20 numbers which aren't equal to 0....but the sum is 0 ....any ideas what is wrong?
    here is the piece of my code

      double sum=0;
      for (int i = 1; i <= 20; i++) {
                    oRange = oSheet.get_Range("A" + i, "A"+i);

                    double value = 0;
                    double.TryParse(oRange.Text.ToString(), out value);
                    my_arry[i] = value;
    sum+=value;
                }
    label1.Text = Convert.ToString(sum);
    Wednesday, February 18, 2009 10:30 PM
  • I've solved my problem!Thx to everyone who helped me!!
    Thursday, February 19, 2009 8:51 AM