ByteScout Spreadsheet SDK - Compact Framework - WinCE - ByteScout

ByteScout Spreadsheet SDK – Compact Framework – WinCE

  • Home
  • /
  • Articles
  • /
  • ByteScout Spreadsheet SDK – Compact Framework – WinCE

ByteScout Spreadsheet SDK – Compact Framework – WinCE

Form1.vb

Imports System.IO
Imports System.Windows.Forms
Imports Bytescout.Spreadsheet

Public Partial Class Form1
	Inherits Form
	Private _listview As ListView

	Public Sub New()
		InitializeComponent()

		' Create ListView control
		_listview = New ListView()
		_listview.Dock = DockStyle.Fill
		_listview.View = View.Details
		_listview.FullRowSelect = True

		' Create two columns
		_listview.Columns.Add("Column 1", 100, HorizontalAlignment.Left)
		_listview.Columns.Add("Column 2", 140, HorizontalAlignment.Left)

		' Add created control to the form
		Controls.Add(_listview)

		' Read XLS file

		Dim document As Spreadsheet = Nothing

		Try
			' Get current directory
			Dim currentDirectory As [String] = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)

			' Load XLS document from the current directory
			document = New Spreadsheet()
			document.LoadFromFile(currentDirectory & "\SimpleReport.xls")

			Dim worksheet As Worksheet = document.Workbook.Worksheets(0)

			' Read cell values and put them to the list view
			For row As Integer = 0 To worksheet.UsedRangeRowMax
				Dim item As New ListViewItem(DirectCast(worksheet.Cell(row, 0).Value, String))
				item.SubItems.Add(DirectCast(worksheet.Cell(row, 1).Value, String))
				_listview.Items.Add(item)
			Next
		Catch generatedExceptionName As Exception
			Throw
		Finally
			If document IsNot Nothing Then
				document.Dispose()
			End If
		End Try
	End Sub
End Class

Program.vb

Imports System.Windows.Forms

NotInheritable Class Program
	Private Sub New()
	End Sub
	''' <summary>
	''' The main entry point for the application.
	''' </summary>
	<MTAThread> _
	Friend Shared Sub Main()
		Application.Run(New Form1())
	End Sub
End Class

Program.vb

Imports System.Diagnostics
Imports System.IO
Imports Bytescout.Spreadsheet

Class Program
	Friend Shared Sub Main(args As String())
		' Create new Spreadsheet
		Dim document As New Spreadsheet()

		' Add worksheets
		Dim worksheet1 As Worksheet = document.Workbook.Worksheets.Add("Demo worksheet 1")
		Dim worksheet2 As Worksheet = document.Workbook.Worksheets.Add("Demo worksheet 2")

		' Fill cell on the first worksheet
		worksheet1.Cell(0, 0).Value = "This is Demo worksheet 1"

		' Get worksheet by name and fill a cell there
		Dim worksheetByName As Worksheet = document.Workbook.Worksheets.ByName("Demo worksheet 2")
		worksheetByName.Cell(0, 0).Value = "This is Demo worksheet 2"

		' Get current directory
		Dim currentDirectory As [String] = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)

		' Construct output file name
		Dim outputFile As [String] = currentDirectory & "\Worksheets.xls"

		' delete output file if exists 
		If File.Exists(outputFile) Then
			File.Delete(outputFile)
		End If

		' Save document
		document.SaveAs(outputFile)

		' open generated XLS document in default program
		Process.Start(outputFile, "")
	End Sub
End Class

Form1.cs

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

namespace ReadXLS
{
	public partial class Form1 : Form
	{
		private ListView _listview;

		public Form1()
		{
			InitializeComponent();

			// Create ListView control
			_listview = new ListView();
			_listview.Dock = DockStyle.Fill;
			_listview.View = View.Details;
			_listview.FullRowSelect = true;

			// Create two columns
			_listview.Columns.Add("Column 1", 100, HorizontalAlignment.Left);
			_listview.Columns.Add("Column 2", 140, HorizontalAlignment.Left);

			// Add created control to the form
			Controls.Add(_listview);

			// Read XLS file

			Spreadsheet document = null;

			try
			{
				// Get current directory
				String currentDirectory = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);

				// Load XLS document from the current directory
                document = new Spreadsheet();
                document.LoadFromFile(currentDirectory + "\\SimpleReport.xls");

				Worksheet worksheet = document.Workbook.Worksheets[0];

				// Read cell values and put them to the list view
				for (int row = 0; row <= worksheet.UsedRangeRowMax; row++)
				{
					ListViewItem item = new ListViewItem((string) worksheet.Cell(row, 0).Value);
					item.SubItems.Add((string) worksheet.Cell(row, 1).Value);
					_listview.Items.Add(item);
				}
			}
			catch (Exception)
			{
				throw;
			}
			finally
			{
				if (document != null) document.Dispose();
			}
		}
	}
}

&#91;/csharp&#93;

<p><strong>Program.cs</strong></p>
[csharp]
using System;
using System.Windows.Forms;

namespace ReadXLS
{
	static class Program
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[MTAThread]
		static void Main()
		{
			Application.Run(new Form1());
		}
	}
}

Program.cs

using System;
using System.Diagnostics;
using System.IO;
using Bytescout.Spreadsheet;

namespace WriteXLS
{
	class Program
	{
		static void Main(string[] args)
		{
			// Create new Spreadsheet
			Spreadsheet document = new Spreadsheet();

			// Add worksheets
			Worksheet worksheet1 = document.Workbook.Worksheets.Add("Demo worksheet 1");
			Worksheet worksheet2 = document.Workbook.Worksheets.Add("Demo worksheet 2");

			// Fill cell on the first worksheet
			worksheet1.Cell(0, 0).Value = "This is Demo worksheet 1";

			// Get worksheet by name and fill a cell there
			Worksheet worksheetByName = document.Workbook.Worksheets.ByName("Demo worksheet 2");
			worksheetByName.Cell(0, 0).Value = "This is Demo worksheet 2";

			// Get current directory
			String currentDirectory = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);

			// Construct output file name
			String outputFile = currentDirectory + "\\Worksheets.xls";

			// delete output file if exists 
			if (File.Exists(outputFile))
			{
				File.Delete(outputFile);
			}

			// Save document
			document.SaveAs(outputFile);

			// open generated XLS document in default program
			Process.Start(outputFile, "");
		}
	}
}


  Click here to get your Free Trial version of the SDK

Tutorials:

prev
next