Getting number of sheets present in the excel file using the ASP code

Discussion in 'C#' started by saribobby, Jan 28, 2008.

  1. #1
    Hi,

    how to write the code to know the number of sheets present in the Excel file using ASP Code?


    Thanks in Advance
     
    saribobby, Jan 28, 2008 IP
  2. AstarothSolutions

    AstarothSolutions Peon

    Messages:
    2,680
    Likes Received:
    77
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You should be able to do it using the Excel COM object - be aware that Ms themselves say that the COM object, whilst it can be used in the server environment it isnt really designed for it and so isnt very efficient. Fine for a small website or occasional use but not something you want being called all the time.

    You didnt mention if it is .Net or Classic that you wanted and so the ASP.Net example is below. Add the reference to the Excel 9.0 COM object first then the code would be:
    
    '''' Create objects
        Dim xlExcel As New Excel.Application
        Dim xlBooks As Excel.Workbooks
        Dim xlBook As Excel.Workbook
        Dim Count As Integer
            
    '''' Start Excel
        xlExcel.Visible = False : xlExcel.DisplayAlerts = False
        xlBooks = xlExcel.Workbooks
            
    '''' Open the file
        xlBooks.Open(Server.MapPath(Request.ApplicationPath) & "\XLTemplate.xls")
        xlBook = xlBooks.Item(1)
            
    '''' Get the count
        Count = xlBook.Worksheets.Count
            
    '''' Clean up
        xlBook.Close()
        xlBooks.Close()
        xlExcel.Quit()
        xlExcel = Nothing
        xlBooks = Nothing
    
    Code (ASP.Net):
     
    AstarothSolutions, Jan 29, 2008 IP