Link Search Menu Expand Document

Convert Template To XLS - VB.NET

Spreadsheet SDK sample in VB.NET demonstrating ‘Convert Template To XLS’

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

Class Program
	Friend Shared Sub Main(args As String())
		' Open spreadsheet from file
		Dim document As New Spreadsheet()

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

		' Find macros and replace them with values
		Replace(worksheet, "<PRODUCT_NAME1>", "Product 1", Nothing)
		Replace(worksheet, "<PRODUCT_NAME2>", "Product 2", Nothing)
		Replace(worksheet, "<PRICE1>", 24.99F, "0.00")
		Replace(worksheet, "<PRICE2>", 29.99F, "0.00")
		Replace(worksheet, "<QANTITY1>", 5, Nothing)
		Replace(worksheet, "<QANTITY2>", 10, Nothing)

		' Delete output file if exists
		If File.Exists("output.xls") Then
		End If

		' Save document

		' Close spreadsheet

		' Open generated XLS document in default application
	End Sub

	Private Shared Sub Replace(worksheet As Worksheet, macro As String, value As Object, format As String)
		Dim cell As Cell = worksheet.Find(macro, True, False, False)

        While cell IsNot Nothing
            cell.Value = value

            If Not [String].IsNullOrEmpty(format) Then
                cell.NumberFormatString = format
            End If

            cell = worksheet.FindNext()
        End While
    End Sub
End Class

Download Source Code (.zip)

Return to the previous page Explore Spreadsheet SDK

Copyright © 2016 - 2021 ByteScout