Find Code:
All Words
Any of the Words
Exact Phrase
Home
:
Code
:
Forums
:
Submit
:
Mailing List
:
About
:
Contact
Code
All
VB.NET
ASP.NET
C#
VB Classic
ASP Classic
Snippets
Popular
Resources
Submit Code
Forums
Articles
Tips
Links
Books
Contest
Link to us
VB .Net - Ms Excel - Determines Worksheets
Author:
Heinz Prelle
E-mail:
Click to e-mail author
Website:
http://www.visual-basic5.de
Submitted:
8/24/2008
Version:
VB 2005
Compatibility:
VB 2005, VB 2008
Category:
File Manipulation
Views:
18333
This example determines all tables in an Excel Workbook.
Declarations:
Imports Microsoft.Office.Interop Imports Microsoft.Office.Interop.Excel Imports System.IO
Code:
Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Try Dim al As New ArrayList(Me.GetExcelWorksheets(Filename:=Helpers.AppPath & "\test.xlsx")) If al IsNot Nothing Then For Each items As Object In al Me.ListBox1.Items.Add(items) Next Else MessageBox.Show("No table names could be determined.", "Info") End If Catch ex As Exception MessageBox.Show(ex.Message.ToString(), "Info") End Try End Sub Private Function GetExcelWorksheets(ByVal Filename As String) As ArrayList If Filename = String.Empty Then Return Nothing End If Dim ExcelObject As Excel.Application = CreateObject("Excel.Application") Dim ExcelWorkbook As Excel.Workbook = Nothing Try Dim al As ArrayList = New ArrayList ExcelWorkbook = ExcelObject.Workbooks.Open(Filename) For Each ExcelWorkSheets As Excel.Worksheet In ExcelWorkbook.Worksheets al.Add(ExcelWorkSheets.Name) Next If al.Count > 0 Then Return al Else Return Nothing End If Catch ex As Exception MessageBox.Show(ex.Message.ToString(), "Info") Finally If ExcelWorkbook IsNot Nothing Then ExcelWorkbook.Close() If ExcelObject IsNot Nothing Then ExcelObject = Nothing End Try Return Nothing End Function Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load With Me : .CenterToScreen() : End With End Sub
Home
|
Forums
|
Submit
|
Books
|
Mailing List
|
Advertising
|
About
|
Contact
© 2024 A1VBCode. All rights reserved.
Legal disclaimer & terms of use
Privacy statement