News Category

 Document Operation

Document Operation (16)

By running VBA within the Office applications, developers/programmers can build customized solutions and programs to enhance the capabilities of those applications. The VBA function of Excel is very powerful. Below I will show you how to use VBA by Spire.XLS.

VBA is the acronym for VB.NET for Applications. It is an implementation of Microsoft's event-driven programming language VB.NET 6 and its associated integrated development environment (IDE), which are built into most Microsoft Office applications. VBA is closely related to VB.NET and uses the VB.NET Runtime Library, but can normally only run code within a host application rather than as a standalone program. It can be used to control one application from another via OLE Automation.

Spire.XLS for .NET is a professional Excel .NET component that can be linked into any type of .NET 2.0, 3.5 or 4.0 projects, either ASP.NET web sites or Windows Forms application. Spire.XLS for .NET offers a combination of APIs and GUI controls for speeding up Excel programming in .NET platform-create new Excel documents from scratch, edit existing Excel documents and convert Excel files. At the same time, Spire.XLS supports VBA and it can load/Save Excel VBA.

Here comes to the steps:

  • Write a template with VBA program with which you can execute your work in Excel.
  • Create another workbook to load the VBA template.

In this demo, it generates a new worksheet named "test" with the VBA template we provide.

Please check the codes as below:

[C#]
using Spire.Xls;

namespace NumberFormat
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a workbook
            Workbook workbook = new Workbook();

            //Initailize worksheet
            workbook.LoadFromFile("VBASample.xls");
            Worksheet sheet = workbook.Worksheets[0];

            //VBA function
            sheet.Range["A1"].Text = "test";

            //Save the file
            workbook.SaveToFile("Sample.xls",ExcelVersion.Version97to2003);

            //Launch the file
            System.Diagnostics.Process.Start("Sample.xls");
        }
    }
}
[VB.NET]
Imports Spire.Xls

Module Module1

    Sub Main()
        'Create a workbook
        Dim workbook As New Workbook()

        'Initailize worksheet
        workbook.LoadFromFile("VBASample.xls")
        Dim sheet As Worksheet = workbook.Worksheets(0)

        'VBA function
        sheet.Range("A1").Text = "test"

        'Save doc file.
        workbook.SaveToFile("Sample.xls",ExcelVersion.Version97to2003)

        'Launching the MS Word file.
        System.Diagnostics.Process.Start("Sample.xls")
    End Sub
End Module

Save Excel Document in C#, VB.NET

2011-01-24 09:49:11 Written by Administrator

Automation of an Excel file allows us to doing various operations in C#/VB.NET. Any loss in these operations may result in unexpected negative consequences for developers and the clients of the developers. That means we must find a solution that enables us to Save Excel with no loss in quality of our operations. This section will demonstrate how to fast save Excel file with perfect performance as directly operations in Excel files.

Spire.Xls for .NET is a professional component that enables developers directly manages Excel operation regardless whether Microsoft Excel is installed on or not. With Spire.Xls for .NET, we can save Excel to what we want it to be. Any kind of trial and evaluation on Spire.Xls for .NET is always welcomed; so now please feel free to download Spire.Xls for .NET and then follow our guide to save perfect Excel or try other function of Spire.Xls for .NET.

Spire.Xls for .NET allows us to create a new Excel file, write data in to it, edit the input data and then save Excel file.

[C#]
using Spire.Xls;
namespace Excel_save
{
   class Program
    {
        static void Main(string[] args)
        {
           //Create a new workbook
            Workbook workbook = new Workbook();
           //Initialize worksheet        
            Worksheet sheet = workbook.Worksheets[0];           
           //Append text
            sheet.Range["A1"].Text = "Demo: Save Excel in .NET";
           //Save it as Excel file
            workbook.SaveToFile("Sample.xls",ExcelVersion.Version97to2003);
           //Launch the file
           System.Diagnostics.Process.Start(workbook.FileName);
        }
    }
}
[VB.NET]
Imports Spire.Xls
Namespace Excel_save
	Class Program
		Private Shared Sub Main(args As String())
			'Create a new workbook
			Dim workbook As New Workbook()
			'Initialize worksheet        
			Dim sheet As Worksheet = workbook.Worksheets(0)
			'Append text
			sheet.Range("A1").Text = "Demo: Save Excel in .NET"
			'Save it as Excel file
			workbook.SaveToFile("Sample.xls",ExcelVersion.Version97to2003)
			'Launch the file
			System.Diagnostics.Process.Start(workbook.FileName)
		End Sub
	End Class
End Namespace
Page 2 of 2