Opening an excel sheet with .net

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • derik
    New Member
    • Jan 2007
    • 19

    #1

    Opening an excel sheet with .net

    the code which i hav already throws some error wen i debug
    here is my code
    plz reply me as soon as possible

    Private Sub Page_Load(ByVal sender As _

    System.Object, ByVal e As System.EventArg s) _

    Handles MyBase.Load

    Dim dt As DataTable = _

    CType(Applicati on.Item("MyData Table"), DataTable)



    Response.Conten tType = "applicatio n/ms-excel"



    Response.AddHea der("Content-Disposition", _

    "inline;filenam e=test.xls")



    Response.Write( ConvertDtToTDF( dt))

    End Sub



    'Converts a DataTable to Tab Delimited Format

    Private Function ConvertDtToTDF( ByVal _

    dt As DataTable) As String

    Dim dr As DataRow, ary() As Object, i As Integer

    Dim iCol As Integer



    'Output Column Headers

    For iCol = 0 To dt.Columns.Coun t - 1

    Response.Write( dt.Columns(iCol ).ToString & vbTab)

    Next

    Response.Write( vbCrLf)



    'Output Data

    For Each dr In dt.Rows

    ary = dr.ItemArray

    For i = 0 To UBound(ary)

    Response.Write( ary(i).ToString & vbTab)

    Next

    Response.Write( vbCrLf)

    Next

    End Function
  • radcaesar
    Recognized Expert Contributor
    • Sep 2006
    • 759

    #2
    Whats the exception u got and where ?

    :)

    Comment

    • derik
      New Member
      • Jan 2007
      • 19

      #3
      Originally posted by radcaesar
      Whats the exception u got and where ?

      :)
      Exception Details: System.NullRefe renceException: Object reference not set to an instance of an object.

      For iCol = 0 To dt.Columns.Coun t - 1

      Comment

      • radcaesar
        Recognized Expert Contributor
        • Sep 2006
        • 759

        #4
        This is because the datatable dt has null reference (Not declared correctly) or the method dt.Columns.Coun t - 1 has returning NULL.

        Check out.

        :)

        Comment

        • derik
          New Member
          • Jan 2007
          • 19

          #5
          Originally posted by radcaesar
          This is because the datatable dt has null reference (Not declared correctly) or the method dt.Columns.Coun t - 1 has returning NULL.

          Check out.

          :)
          ya but stilll its not working ... wat can i put instead

          can u suggest me any alternative

          Comment

          • radcaesar
            Recognized Expert Contributor
            • Sep 2006
            • 759

            #6
            Check what ur dt.columncount - 1 had return there.

            Else

            Comment that for loop and try.

            :)

            Comment

            • misogsk
              New Member
              • Jan 2007
              • 39

              #7
              You can also try to use a OleDB connection to your excel file.

              string connectionStrin g = @"Provider=Micr osoft.Jet.OLEDB .4.0; Data Source=Book1.xl s;Extended Properties= ""Excel 8.0;HDR=YES;""" ;

              Now you can create dataadater and command.
              Command.text should for example SELECT col1,col2 FROM [sheet$]
              This fill your dataadapter and then use dataadapter method fill to fill a new dataset
              adapter_name.Fi ll(dataset_name );
              Assign dataset to Datasource property of your control.
              I hope this helps you.

              Comment

              • derik
                New Member
                • Jan 2007
                • 19

                #8
                Originally posted by radcaesar
                Check what ur dt.columncount - 1 had return there.

                Else

                Comment that for loop and try.

                :)
                tanx for ur reply...

                making dat loop as comment is not workink out..

                Comment

                Working...