Excel automation

  • Thread starter Thread starter powerranger
  • Start date Start date
P

powerranger

I'd like to know if there is a way to read the worksheet name of an
excel file.
Here is my code:

foreach(Excel.Worksheet sheet in thisworkbook.Sheets)
{
Response.Write (sheet.Name);
}


This code works if the names are like Sheet1, Sheet2, etc.. But if I
change the sheet1 to testsheet, it still dispays as Sheet1. Can
someone helps me? Thanks.
 
I wonder if your sheet is being locked? That's one of the dangers of using
Office automation. Anyway, I was able to get the changed name with this:

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
Dim xcel As New Microsoft.Office.Interop.Excel.Application
Dim wkbks As Microsoft.Office.Interop.Excel.Workbooks
Dim wkshts As Microsoft.Office.Interop.Excel.Sheets
Dim intWksht As Integer
wkbks = xcel.Workbooks
wkbks.Open(Server.MapPath("xcl.xls"))
wkshts = wkbks.Item(1).Sheets
For intWksht = 1 To wkshts.Count
Response.Write(wkshts.Item(intWksht).Name & "<br>")
Next
wkbks.Close()
End Sub

Does this help?

Ken
Microsoft MVP [ASP.NET]
 
BTW, be careful. I think that code was leaving instances of Excel running in
the background. I guess it needs to clean up the objects better.
 
Is there another way to get list of worksheet names using Ado or Oledb?
Sample of code would be helpful. Thanks.
 
Back
Top