locked
Trying read Data from excel RRS feed

  • Question

  • User832284694 posted

    I am trying to read data from excel using C#.net with help of attached code however it taking hell lot of time to open excel workbook to read data however excel which I am using as input has just two columns and two rows.

    So, could you please help me to solve my problem to read data from excel.

    Line taking a lot of time:

    excelWorkbook = excelApp.Workbooks.Open(workbookPath, 0,false, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true,false, 0, true, false, false); 

    Same issue happen even if I replace above affected line with below line

    excelWorkbook = excelApp.Workbooks.Open(workbookPath);

    Even I am not sure code which I have written is correct or not as I am not able to test it.

    using System;
    using Excel = Microsoft.Office.Interop.Excel;
    
    namespace ExcelExample
    {
    	/// <summary>
    	/// Summary description for Class1.
    	/// </summary>
    	class ExcelClass
    	{
    		/// <summary>
    		/// The main entry point for the application.
    		/// </summary>
    		[STAThread]
    		static void Main(string[] args)
    		{
    			Excel.Application excelApp = new Excel.Application();  // Creates a new Excel Application
    			excelApp.Visible = true;  // Makes Excel visible to the user.
    
    			// The following line if uncommented adds a new workbook
    			//Excel.Workbook newWorkbook = excelApp.Workbooks.Add();
    			
    			// The following code opens an existing workbook
    			string workbookPath = @"E:\Book.xlsx";  // Add your own path here
    
                Excel.Workbook excelWorkbook = null;
    
                try
                {
                    excelWorkbook = excelApp.Workbooks.Open(workbookPath, 0,
                        false, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true,
                        false, 0, true, false, false);
                }
                catch
                {
                    //Create a new workbook if the existing workbook failed to open.
                    excelWorkbook = excelApp.Workbooks.Add();
                }
    			
    			// The following gets the Worksheets collection
    			Excel.Sheets excelSheets = excelWorkbook.Worksheets;
    
    			// The following gets Sheet1 for editing
    			string currentSheet = "Sheet1";
    			Excel.Worksheet excelWorksheet = (Excel.Worksheet)excelSheets.get_Item(currentSheet);
    			
    			// The following gets cell A1 for editing
    			Excel.Range excelCell = (Excel.Range)excelWorksheet.get_Range("A1", "A1");
    
    			// The following sets cell A1's value to "Hi There"
    			excelCell.Value2 = "Hi There";
    		}
    	}
    }
    

    Wednesday, April 5, 2017 4:13 AM

All replies

  • User197322208 posted

    Line taking a lot of time:

    excelWorkbook = excelApp.Workbooks.Open(workbookPath, 0,false, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true,false, 0, true, false, false); 

    If the Excel file is 1.5 GB , then it will take a lot of time... 

    How big is the file , how much time, what is the RPM of the hard drive and how much RAM available has the PC?

    Wednesday, April 5, 2017 6:57 AM