How to open open excel workbook and set cell value with C#

2 Answers

0 votes
/*
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);

                Excel.Range excelCell = (Excel.Range)excelWorksheet.get_Range("A2", "A2");

                excelCell.Value = "1000";
            }
            catch
            {
                Console.WriteLine("Error open: " + WorkbookFileName);
            }
		}
	}
}

 



answered Nov 28, 2015 by avibootz
0 votes
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);

                excelWorksheet.Range["C4"].Value = 5000;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
		}
	}
}

 



answered Nov 28, 2015 by avibootz

Related questions

2 answers 313 views
1 answer 233 views
1 answer 270 views
1 answer 197 views
2 answers 265 views
265 views asked Nov 27, 2015 by avibootz
1 answer 212 views
212 views asked Aug 10, 2018 by avibootz
...