update a value in a cell and all other cells will be automatically changed

For example, I have a spreadsheet excel. I want to read it using C#.

In the spreadsheet, I have two or more cell:

A1 = 0
B1 = A1 + 7

Is it possible that if I change the value of A1 to 1 using C# and then the B1 cell will be automatically updated?

Answers


The second cell will auto update. Here's some code using MS Excel Object Model (include Microsoft.Interop.Excel.dll):

using System;

namespace exceltest2
{
    using Microsoft.Office.Interop.Excel;

    internal class Program
    {
        private static void Main(string[] args)
        {
            Application excel = null;
            Workbook wb = null;
            try
            {
                // run Excel 
                excel = new Application();
                excel.Visible = false;

                // Open file
                wb = excel.Workbooks.Open(
                    @"D:\test.xlsx", Type.Missing, false, // Read-Only?
                    Type.Missing, Type.Missing, Type.Missing, 
                    Type.Missing, Type.Missing, Type.Missing, 
                    Type.Missing, Type.Missing, Type.Missing, 
                    Type.Missing, Type.Missing, Type.Missing);

                // Read worksheets
                Sheets sheets = wb.Worksheets;

                // Select worksheets
                Worksheet ws = (Worksheet)sheets.get_Item("Table1");

                Range range = (Range)ws.get_Range("A1", "A1");

                // Check Values #1
                Console.WriteLine(ws.get_Range("A1", "A1").Value2.ToString());
                Console.WriteLine(ws.get_Range("A2", "A2").Value2.ToString());

                range.Cells[1, 1] = 15;

                // Check Values #2
                Console.WriteLine(ws.get_Range("A1", "A1").Value2.ToString());
                Console.WriteLine(ws.get_Range("A2", "A2").Value2.ToString());
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                wb.Save();
                wb.Close(false, null, null);
                excel.Quit();
                Console.ReadLine();
            }
        }
    }
}

Need Your Help

how to get text file rows with no delimiter into array

c#

I have a text file that I'm trying to input into an array called columns.

DROP and CREATE on Amazon RDS MySQL Instance

mysql amazon-web-services amazon amazon-rds

Is it safe to connect to my MySQL RDS Amazon instance and run a DROP and CREATE on the database (Basically re-create the database)?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.