Coder Social home page Coder Social logo

leo-zubiri / datatabletoexcel Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 99 KB

Manejo de Datatable a Excel para crear reportes basados en plantillas predefinidas. No utiliza librerías, usa la conexión a Excel nativa de VBA

Visual Basic .NET 100.00%
dotnet excel vba-excel

datatabletoexcel's Introduction

ExcelCreation

Net framework. Create an Excel file with a datatable content without use any libraries.

Use Excel.application object of the classic visual basic, you can create a dll and integrate this functionality with netframeworks languages like C# or VBNET

Result

Code

Public Shared Sub CreateExcel(dtable As DataTable)
        Try
            '-------------- Set necessary variables
            Dim ExcelApp As Object
            ExcelApp = CreateObject("Excel.Application")

            Dim wb As Object
            wb = ExcelApp.Workbooks.Add

            Dim sheet As Object
            sheet = wb.Worksheets(1)

            Dim rowAdjust As Integer = 1 'Set content next to headers
            Dim excelCell As String = "B2"


            ' -------------- Write Content
            ' Set Headers
            For col = 0 To dtable.Columns.Count - 1
                sheet.Range(excelCell).Offset(0, col).Value = dtable.Columns.Item(col).ColumnName
            Next

            ' Set the content of the datatable
            For row = 0 To dtable.Rows.Count - 1
                For col = 0 To dtable.Columns.Count - 1
                    'sheet.Cells(row + 1, col + 1).Value = dtable.Rows(row).Item(col)
                    sheet.Range(excelCell).Offset(row + rowAdjust, col).Value = dtable.Rows(row).Item(col)
                Next col
            Next row

            ' --------------- Set style
            ' Header Style
            With sheet.Range(sheet.Range(excelCell),
                             sheet.Range(excelCell).Offset(0, dtable.Columns.Count - 1))
                .WrapText = True
                .RowHeight = 40
                .ColumnWidth = 22
                .VerticalAlignment = -4108 'CENTER
                .HorizontalAlignment = -4108
                .Interior.Color = RGB(25, 94, 131)
                .Font.Bold = True
                .Font.Color = RGB(255, 255, 255) 'WHITE
            End With

            ' Border style
            With sheet.Range(sheet.Range(excelCell),
                             sheet.Range(excelCell).Offset(dtable.Rows.Count, dtable.Columns.Count - 1))
                .VerticalAlignment = -4108 'CENTER
                .HorizontalAlignment = -4108
                With .Borders
                    .LineStyle = 1 'xlContinuous
                End With
            End With

            ' --------------- Save Result
            Dim ruta = My.Computer.FileSystem.SpecialDirectories.MyDocuments
            wb.SaveAs(Path.Combine(ruta, "Test", "Excel.xlsx"))
            wb.Close
            ExcelApp.Quit()
        Catch ex As Exception
            MessageBox.Show(ex.ToString())
        End Try

    End Sub

datatabletoexcel's People

Contributors

leo-zubiri avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.