|
|
|
|
|
Home
»
VB.NET
»
Creating an Excel Spreadsheet programmatically using VB.NET
|
|
|
|
Author Rank :
|
|
|
Page Views :
|
415068
|
|
Downloads :
|
5026
|
|
Rating :
|
Rate it
|
|
Level :
|
Beginner
|
|
|
|
|
Download
Files:
|
|
|
|
|
|
|
|
|
|
|
INTRODUCTION:
The Interoperability services make it very easy to work with COM Capable Applications such as Word and Excel. This article reveals using Excel from a managed application. Excel is the spreadsheet component of Microsoft Office 2000. The majority of Excel programmatic functionality is exposed through Automation via the type library Excel9.olb. The intention of this article is to express that a managed application can interrelate with Excel as a COM server.
The first step is to create a reference in our project to Excel 9.0 Objects Library. By using Tlbimp tool we can generate Excel.dll.
TlbImp Excel9.olb Excel.dll.
By adding Excel.dll to our program we can use the functionality of the Excel.
Now let us see in detail how to create an Excel Spreadsheet? & Set values to the cell using VB.NET. The codes for Creating, make visible, add a new workbook and to set a value for cell in the Excel file is shown below.
- CREATING NEW EXCEL.APPLICATION:
Dim exc As New Application () If exc Is Nothing Then Console.WriteLine("ERROR: EXCEL couldn't be started") Return 0 End If
- TO MAKE APPLICATION VISIBLE:
exc.set_Visible(0, true)
- TO GET THE WORKBOOKS COLLECTION:
Dim workbooks As Workbooks = exc.Workbooks Dim workbook As _Workbook = workbooks.Add XlWBATemplate.xlWBATWorksheet, 0)
- TO GET THE WORKSHEETS COLLECTION:
Dim worksheet As _Worksheet = CType(sheets.get_Item(1), _Worksheet) If worksheet Is Nothing Then Console.WriteLine("ERROR in worksheet == null") End If
- TO SET THE VALUE FOR CELL:
Dim range1 As Range = worksheet.get_Range("C1", Missing.Value) If range1 Is Nothing Then Console.WriteLine("ERROR: range == null") End If Const nCells As Integer = 1 Dim args1(1) As [Object] args1(0) = nCells range1.GetType().InvokeMember("Value", BindingFlags.SetProperty, Nothing, range1, args1)
The Example:
Imports System Imports System.Reflection ' For Missing.Value and BindingFlags Imports System.Runtime.InteropServices ' For COMException Imports Excel Class AutoExcel Public Shared Function Main() As Integer Dim exc As New Application If exc Is Nothing Then Console.WriteLine("ERROR: EXCEL couldn't be started!") Return 0 End If exc.set_Visible(0, True) Dim workbooks As Workbooks = exc.Workbooks Dim workbook As _Workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet, 0) Dim sheets As Sheets = workbook.Worksheets Dim worksheet As _Worksheet = CType(sheets.get_Item(1), _Worksheet) If worksheet Is Nothing Then Console.WriteLine("ERROR: worksheet == null") End If Dim range1 As Range = worksheet.get_Range("C1", Missing.Value) If range1 Is Nothing Then Console.WriteLine("ERROR: range == null") End If Const nCells As Integer = 1 Dim args1(1) As [Object] args1(0) = nCells range1.GetType().InvokeMember("Value", BindingFlags.SetProperty, Nothing, range1, args1) Return 100 End Function 'Main End Class 'AutoExcel
Now let us observe how to send a single dimension array to Excel:
It is similar to set the value for the cell. Only change is we use array as args2[0] = array2. Const nCell As Integer = 5 Dim range2 As Range = worksheet.get_Range("A1", "E1") Dim array2(nCell) As Integer Dim i As Integer For i = 0 To (array2.GetLength(0)) - 1 array2(i) = i + 1 Next i Dim args2(1) As [Object] args2(0) = array2 range2.GetType().InvokeMember("Value", BindingFlags.SetProperty, Nothing, range2, args2) End Function '__unknown
The OutPut:

 Conclusion:
With the help of TlbImp.exe tool we can generate .NET assembly from Type library files and we can use that functionality of Type library file in VB.NET.
|
|
Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post
Here.
|
|
|
|
|
Login
to add your contents and source code to this article
|
|
|
|
|
|
|
|
|
|
|
|
G Gnana Arun Ganesh
G.GNANA ARUN GANESH, an ECE graduate (1997-2001) seeking a profession in either Software or Hardware Company. During Campus interview selected as a Software Engineer at GREEN MICRO SYSTEMS Chennai, a German collaboration company. Due to down stream in software market they have cancelled the offer. His skills includes VB, COM/DCOM, ASP, VB.NET, C# and Embedded systems. He is also a writer of numerous articles for many technical Web sites.
|
|
|
|
|
|
|
|
|
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional
consulting company, our consultants are well-known experts in .NET and many of them
are MVPs, authors, and trainers. We specialize in Microsoft .NET development and
utilize Agile Development and Extreme Programming practices to provide fast pace
quick turnaround results. Our software development model is a mix of Agile Development,
traditional SDLC, and Waterfall models.
|
|
Click here to learn more about C# Consulting. |
|
|
|
|
|
|
|
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon.
Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees.
As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
|
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
|
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
|
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
|
|
|
|
|
|
|
|
|
|
|
|
|