Results 1 to 10 of 10

Thread: [2005] Create Excel File From DataTable

  1. #1

    Thread Starter
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    [2005] Create Excel File From DataTable

    I figured this was worth posting as I had one hell of a time getting it to work properly. This uses the Microsoft Excel 11.0 Object Library. One of the issues with using this com object was the process hanging in memory until your application closed. This solution addresses this issue.

    The following code will Take the contents of a DataTable and save it as an Excel Document.

    First you need to add reference to the Microsoft Excel 11.0 Object Library

    then import it like so
    Code:
    Imports Microsoft.Office.interop
    The Function to save the file as excel
    Code:
        Private Sub ExportToExcel(ByVal FileName As String, ByVal dt As DataTable)
            Dim oExcel As Excel.ApplicationClass = Nothing
            Dim oBook As Excel.WorkbookClass = Nothing
            Dim oBooks As Excel.Workbooks = Nothing
            Dim oXLWsheet As Excel.Worksheet = Nothing
            Dim oRange As Excel.Range = Nothing
            Dim intColumn, intRow, intColumnValue As Integer
    
    
            Try
                'CREATE A NEW EXCEL OBJECT
                oExcel = New Excel.Application
                'HIDE THE EXCEL APPLICATION
                oExcel.Visible = False
                'CREATE A NEW BOOKS OBJECT
                oBooks = oExcel.Workbooks
                'CREATE A NEW BOOK OBJECT
                oBook = oBooks.Add
                'CREATE A NEW WORKSHEET OBJECT
                oXLWsheet = oBook.Sheets(1)
                'CREATE A NEW CELLS OBJECT (RANGE)
                oRange = oXLWsheet.Cells
    
                'SET THE COLUMN NAMES
                For intColumn = 0 To dt.Columns.Count - 1
                    oRange(1, intColumn + 1) = dt.Columns(intColumn).ColumnName.ToString
                Next
                'SET THE CELLS VALUES
                For intRow = 0 To dt.Rows.Count - 1
                    For intColumnValue = 0 To dt.Columns.Count - 1
                        oRange(intRow + 2, intColumnValue + 1) = dt.Rows(intRow).ItemArray(intColumnValue).ToString
                    Next
                Next
                'SAVE THE WORKSHEET (PROMPT FOR OVERWRITE)
                oXLWsheet.SaveAs(FileName)
                   
            Catch ex As Exception
                MessageBox.Show("Unable to export excel file" & Environment.NewLine & ex.Message, "Unable to export file")
            Finally
                'RELEASE THE CELLS OBJECT
                If Not oRange Is Nothing Then
                    Release(oRange)
                End If
                'RELEASE THE WORKSHEET OBJECT
                If Not oXLWsheet Is Nothing Then
                    Release(oXLWsheet)
                End If
                'CLOSE AND RELEASE THE BOOK OBJECT
                If Not oBook Is Nothing Then
                    oBook.Close(False)
                    Release(oBook)
                End If
                'RELEASE THE BOOKS OBJECT
                If Not oBooks Is Nothing Then
                    Release(oBooks)
                End If
                'QUIT AND RELEASE THE EXCEL OBJECT
                If Not oExcel Is Nothing Then
                    oExcel.Quit()
                    Release(oExcel)
                End If
            End Try
        End Sub
    The following function is also required and is used to release the com objects from memory.

    Code:
        Private Sub Release(ByVal o As Object)
            Try
                While System.Runtime.InteropServices.Marshal.ReleaseComObject(o) <> 0
                    'CONTINUE UNTIL ALL OBJECTS ARE RELEASED
                End While
            Catch
            Finally
                'SET THE OBJECT = NOTHING
                o = Nothing
            End Try
        End Sub
    Here is a module that contains these function
    Attached Files Attached Files
    Last edited by bmahler; Aug 9th, 2007 at 03:26 PM.
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: [2005] Create Excel File From DataTable

    I just used the parts that released the objects from memory, but that seems to work well.

    I have a question. My app creates a report. Running this from the routine that creates the worksheet actually destorys the worksheet before it can be viewed. I need it visible on the screen so the user can either just view it, or use Excel to print it, depending on what they want to do.

    So, I would need to move the Releasing part out of my Sub. So, I moved it to the Form_Closing.
    Code:
    Private Sub frmUserMaint_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
            If Not oRange Is Nothing Then
                Release(oRange)
            End If
            If Not shWorkSheet Is Nothing Then
                Release(shWorkSheet)
            End If
            If Not bkWorkBook Is Nothing Then
                bkWorkBook.Close(False)
                Release(bkWorkBook)
            End If
            If Not objExcel Is Nothing Then
                objExcel.Quit()
                Release(objExcel)
            End If
            GC.Collect()
            GC.WaitForPendingFinalizers()
        End Sub
    When I closed the form, I got a runtime error saying "The object invoked has disconnected from its clients".

    Other than it immediately closed the spreadsheet, which won't work for me, it worked fine when it was part of the Sub itself.

  3. #3

    Thread Starter
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    Re: [2005] Create Excel File From DataTable

    In my example the excel application is not visible. In the case where you wnt it visible and the user to interact, if they close the excel instance that is open, you will get that error. Try just removing the bworkBook.close(False) line.

    also, with this method you can remove the Garbage Collection items
    GC.Collect()
    GC.WaitForPendingFinalizers()

    they are not needed if all objects are released properly
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [2005] Create Excel File From DataTable

    I still have never needed to use ReleaseCOMObject in .NET. I think I can figure out what your issue is but I'll look into it in a while as I have to finish my work that my client is desperately waiting for

    Just quickly, I see you are using ...
    oBooks = oExcel.Workbooks

    But never actually using it. Things like this just leave the door open for potential issues.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    Re: [2005] Create Excel File From DataTable

    I use it afterwords for setting the book object
    oBooks = oExcel.Workbooks
    'CREATE A NEW BOOK OBJECT
    oBook = oBooks.Add

    This was something I found on the msdn that they suggested.
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [2005] Create Excel File From DataTable

    You can also shortcut it by going ...
    oBook = oExcel.Workbooks/Add()
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7

    Thread Starter
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    Re: [2005] Create Excel File From DataTable

    When I was creating this example I was following the instructions from this example from Microsoft http://support.microsoft.com/default...b;en-us;317109
    Quote Originally Posted by Microsoft Support
    To make sure that the Office application quits, make sure that your automation code meets the following criteria:
    • Declare each object as a new variable. For example, change the following line of code
    Code:
    oBook = oExcel.Workbooks.Add()
    to the following:
    Code:
    dim oBooks as Excel.Workbooks
    oBooks = oExcel.Workbooks
    oBook = oBooks.Add()
    • Use System.Runtime.InteropServices.Marshal.ReleaseComObject when you have finished using an object. This decrements the reference count of the RCW.
    • To release the reference to the variable, set the variable equal to Nothing or Null.
    • Use the Quit method of the Office application object to tell the server to shut down.
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

  8. #8
    Lively Member
    Join Date
    Mar 2007
    Posts
    78

    Re: [2005] Create Excel File From DataTable

    bmahler

    i have used your code
    ihave added one line to align the contents of the cells center like the following...

    Code:
     'SET THE CELLS VALUES
                For intRow = 0 To dt.Rows.Count - 1
                    For intColumnValue = 0 To dt.Columns.Count - 1
                        oRange(intRow + 2, intColumnValue + 1) = dt.Rows(intRow).ItemArray(intColumnValue).ToString
                        oRange.HorizontalAlignment = HorizontalAlignment.Center
                    Next
                Next
    after the file is created and data added. i opened it.
    the cells are not aligned centrally.
    they are aligned left.

    what i am missing??

  9. #9
    New Member
    Join Date
    Feb 2008
    Posts
    1

    Re: [2005] Create Excel File From DataTable

    Is there a way to filter the data being saved? I need to save out the data based on dates input by the user. DataTable doesn't have the RowFilter that DataViews do.

  10. #10

    Thread Starter
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    Re: [2005] Create Excel File From DataTable

    You should have those filters in your SQL that generates the Datatable.
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width