ByteScout Spreadsheet SDK – C# – View Spreadsheet

  • Home
  • /
  • Articles
  • /
  • ByteScout Spreadsheet SDK – C# – View Spreadsheet

ByteScout Spreadsheet SDK – C# – View Spreadsheet

Form1.cs

using System;
using System.IO;
using System.Text;
using System.Windows.Forms;
using Bytescout.Spreadsheet;

namespace ViewSpreadsheet
{
	public partial class Form1 : Form
	{
		private SpreadsheetControl _spreadsheetControl = null;

		private string _fileName = null;

		public Form1(string[] args)
		{
			if (args.Length > 0 && File.Exists(args[0]))
			{
				_fileName = args[0];
			}

			if (_fileName != null)
			{
				OpenFile(_fileName);
			}

			InitializeComponent();

			Icon = Properties.Resources.xlsview;
		}

		private void OpenFile(string fileName)
		{
			Text = "Spreadsheet Viewer - " + fileName;
			
			Spreadsheet xls = new Spreadsheet();
			xls.RegistrationName = "demo";
			xls.RegistrationKey = "demo";

			try
			{
				xls.LoadFromFile(fileName);
			}
			catch (SpreadsheetProtectionPasswordRequired)
			{
				PasswordDialog passwordDialog = new PasswordDialog();

				if (passwordDialog.ShowDialog(this) == DialogResult.OK)
				{
					xls.LoadFromFile(fileName, CacheType.Memory, ",", Encoding.Default, passwordDialog.Password);
				}
				else
				{
					return;
				}
			}

			lblStub.Visible = false;

			if (_spreadsheetControl == null)
			{
				_spreadsheetControl = new SpreadsheetControl();
				_spreadsheetControl.Dock = DockStyle.Fill;

				Controls.Add(_spreadsheetControl);
				
				_spreadsheetControl.BringToFront();
				_spreadsheetControl.Focus();
			}

			Cursor = Cursors.WaitCursor;

			try
			{
				_spreadsheetControl.Spreadsheet = xls;
			}
			catch (Exception e)
			{
				MessageBox.Show(e.ToString());
			}
			finally
			{
				Cursor = Cursors.Default;
			}
		}

		private void tsmiOpen_Click(object sender, EventArgs e)
		{
			if (openFileDialog.ShowDialog() == DialogResult.OK)
			{
				OpenFile(openFileDialog.FileName);
			}
		}

		private void tsmiAbout_Click(object sender, EventArgs e)
		{
			MessageBox.Show("Spreadsheet Viewer\n\nwww.bytescout.com\n\nCopyright (c) Bytescout Software 2012", "About...");
		}

		private void tsmiExit_Click(object sender, EventArgs e)
		{
			Close();
		}

		private void tsmiVisitWebsite_Click(object sender, EventArgs e)
		{
			System.Diagnostics.Process.Start(@"http://www.bytescout.com");
		}

		private void tsbPromo_Click(object sender, EventArgs e)
		{
			System.Diagnostics.Process.Start(@"http://bytescout.com/products/developer/spreadsheetsdk/bytescoutspreadsheetsdk.html");
		}
	}
}

PasswordDialog.cs

using System;
using System.Windows.Forms;

namespace ViewSpreadsheet
{
    public partial class PasswordDialog : Form
    {
        public PasswordDialog()
        {
            InitializeComponent();
        }

        public string Password
        {
            get { return textBoxPassword.Text; }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            //DialogResult = DialogResult.OK;
            //Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {

        }

        private void checkBoxHide_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxHide.Checked)
                textBoxPassword.PasswordChar = '*';
            else
                textBoxPassword.PasswordChar = '\0';
        }
    }
}

Program.cs

using System;
using System.Windows.Forms;

namespace ViewSpreadsheet
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            Application.Run(new Form1(args));
        }
    }
}


  Click here to get your Free Trial version of the SDK

prev
next