Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

31,051 questions

40,769 answers

573 users

How to open open excel workbook and read ange of cells values with C#

1 Answer

0 votes
/*
Open Your Visual Studio
Create: Visual C# - Console Application Project
Right click on: References 
Select: Add references...
Select: com
Select: Microsoft Office 12.0/15.0/16.0 Object Library
In References list you will see: Microsoft.Office.Interop.Excel
*/

using System;
using Excel = Microsoft.Office.Interop.Excel;

namespace WorkWithExcel
{
	class ExcelClass
	{
		static void Main(string[] args)
		{
		    Excel.Application excelApp = new Excel.Application();  
			excelApp.Visible = true;  

			string WorkbookFileName = "e:/test.xlsx";

            Excel.Workbook excelWorkbook = null;

            try
            {
                excelWorkbook = excelApp.Workbooks.Open(WorkbookFileName);

                Excel.Sheets excelSheets = excelWorkbook.Worksheets;

                string currentSheet = "Sheet1";
            Excel.Worksheet excelWorksheet = (Excel.Worksheet)excelSheets.get_Item(currentSheet);

                System.Array RangeValues = (System.Array)excelWorksheet.Range["C4..D15"].Value2;
                foreach (Object rv in RangeValues)
                {
                    if (rv != null)
                        Console.Write("{0}\n", rv);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
		}
	}
}

 





answered Nov 29, 2015 by avibootz

Related questions

2 answers 325 views
2 answers 150 views
1 answer 73 views
2 answers 123 views
123 views asked Nov 27, 2015 by avibootz
...