Link Search Menu Expand Document

Swap Cells and Their Values - C#

Spreadsheet SDK sample in C# demonstrating ‘Swap Cells and Their Values’

Program.cs
using System;
using System.Collections.Generic;
using System.Text;
using Bytescout.Spreadsheet;
using System.Diagnostics;
using System.IO;


namespace Sample
{
    class Program
    {
        static void Main(string[] args)
        {
            // Open existing Spreadsheets
            Spreadsheet document1 = new Spreadsheet();
	document1.LoadFromFile("input.xls");

            // Get first cell value
            object value1 = document1.Worksheet(0).Cell("A1").Value;
            // Get another cell value
            object value2 = document1.Worksheet(0).Cell("B1").Value;

            // Swap them
            object value3;
            value3 = value2;
            value2 = value1;
            value1 = value3;

            document1.Worksheet(0).Cell("A1").Value = value1;
            document1.Worksheet(0).Cell("B1").Value = value2;

            // delete output file if exists already
            if (File.Exists("Output.xls")){
                File.Delete("Output.xls");
            }

            // Save document
            document1.SaveAs("Output.xls");

            // Close Spreadsheet
            document1.Close();

            // open generated XLS document in default program
            Process.Start("Output.xls");

        }
    }
}

Download Source Code (.zip)

Return to the previous page Explore Spreadsheet SDK