Visual basic 20129

Sheelnath Kekre 121 Reputation points
2021-01-11T16:36:59.147+00:00

I created a database application using visual basic 2019.In this project there are various fields to save data to database table. I want to know , how to export this data to Excel sheet using a button on the form with text "Export to Excel".

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,734 questions
0 comments No comments
{count} votes

Accepted answer
  1. Karen Payne MVP 35,436 Reputation points
    2021-01-11T17:20:10.73+00:00

    I would suggest using spreadsheetlight available from NuGet and DocumentFormat.OpenXml. Both are free libraries.

    Why?

    Because these libraries don't use Excel automation but OpenXML for Excel which is many times faster than Excel automation.

    Simple export

    Imports SpreadsheetLight
    
    Public Class ExcelOperations
        Public Sub SimpleExportRaw(pFileName As String, pSheetName As String, pDataTable As DataTable, pColumnHeaders As Boolean)
    
            Using doc As New SLDocument()
                doc.SelectWorksheet(pSheetName)
                doc.ImportDataTable(1, SLConvert.ToColumnIndex("A"), pDataTable, pColumnHeaders)
                doc.SaveAs(pFileName)
            End Using
    
        End Sub
    End Class
    

    Some helpers

    After creating and instance of SLDocument you can (where doc is a SLDocument)

    • AutoFit columns e.g. doc.AutoFitColumn("A")
    • Create and apply styles via doc.CreateStyle which in turn allows you to do things like text alignment, bold, italics etc.

1 additional answer

Sort by: Most helpful
  1. Castorix31 85,796 Reputation points
    2021-01-11T16:41:57.7+00:00
    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.