Exforsys
+ Reply to Thread
Results 1 to 2 of 2

Exporting reports on excel sheet..

This is a discussion on Exporting reports on excel sheet.. within the Microsoft .NET forums, part of the Programming Talk category; Hi everyone.. Im new to this concetp, i having some records in the dataset now i want these records to ...

  1. #1
    kannabiran is offline Junior Member Array
    Join Date
    Jan 2007
    Answers
    1

    Exporting reports on excel sheet..

    Hi everyone..
    Im new to this concetp, i having some records in the dataset now i want these records to exports to the excel sheet, with proper alignment with the header text..for example..

    adp=new oledbdataadapter("select obj_type,obj_name from mytable",con)
    adp.fill(ds,"mytable")

    Now i want these two columns to export into the excel sheet.. Help me..im using VB.Net...

    thnks n rgrds
    Kanna....


  2. #2
    techvinny is offline Moderator Array
    Join Date
    Dec 2010
    Answers
    56
    You may try this....

    Code:
    adp=new oledbdataadapter("select obj_type,obj_name from mytable",con)
    adp.fill(ds,"mytable")
    
    'Export to Excel
    Dim m_objOpt as Object = System.Reflection.Missing.Value
    Dim m_objExcel as Excel.Application = new Excel.Application()
    Dim m_objBooks as Excel.Workbooks = m_objExcel.Workbooks
    m_objBooks.Add()   'Add a worksheet
    Dim m_objBook = m_objBooks.Worksheets(1)
    m_objBook.Select()
    
    'Write the column headings
    Dim i As Integer = 1
    For col = 0 To ds.Tables(0).Columns.Count - 1
        m_objBook.cells(1, i).value = ds.Tables(0).Columns(col).ColumnName
        m_objBook.cells(1, i).EntireRow.Font.Bold = True
        i += 1
    Next
    'Write the column values
    i = 2
    Dim k As Integer = 1
    For col = 0 To ds.Tables(0).Columns.Count - 1
           i = 2
           For row = 0 To ds.Tables(0).Rows.Count - 1
               m_objBook.Cells(i, k).Value = ds.Tables(0).Rows(row).ItemArray(col)
               i += 1
           Next
           k += 1
    Next
    'save the file
    Dim filename as String = "c:\File_Exported" & Format(Now(), "dd-MM-yyyy_hh-mm-ss") & ".xls"
    m_objBook.SaveAs(filename)
    
    m_objBook.Close(false, m_objOpt, m_objOpt)
    m_objExcel.Quit()



Latest Article

Network Security Risk Assessment and Measurement

Read More...