Results 1 to 2 of 2

Thread: Report Generate to Excel

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2008
    Posts
    90

    Report Generate to Excel

    Hi, does anyone know of a workng example of a vb.net application generating a report to excel, where the database is access?

    Thanks
    Stuart

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

    Re: Report Generate to Excel

    I do it with SQL Server, not Access, but the principle should remain the same.
    vb.net Code:
    1. Option Strict On
    2.  
    3. Imports Excel
    4. Imports System.Data.SqlClient
    5.  
    6. Public Class Form1
    7.     Private objExcel As Excel.Application = DirectCast(CreateObject("Excel.Application"), Excel.Application)
    8.     Private bkWorkBook As Excel.Workbook = DirectCast(objExcel.Workbooks.Add(), Excel.Workbook)
    9.     Private shWorkSheet As Excel.Worksheet = DirectCast(bkWorkBook.Sheets("Sheet1"), Excel.Worksheet)
    10.     Private rngRowStart As Excel.Range = DirectCast(shWorkSheet.Cells(1, 1), Excel.Range)
    11.     Private oPageSetup As Excel.PageSetup = DirectCast(shWorkSheet.PageSetup, Excel.PageSetup)
    12.     Private oRange As Excel.Range = DirectCast(shWorkSheet.Columns("A:BZ"), Excel.Range)
    13.  
    14. Private Sub PrintUserList(Optional ByVal pstrStatus As String = vbNullString)
    15.     Dim i As Long
    16.         Dim sSQL As String
    17.         Dim ds As New DataSet
    18.         'this is used to get the number of records - that number will be used
    19.         'as the max setting for the progress bar.  for the count, we need to use
    20.         'a data set
    21.         sSQL = "SELECT user_last_nm, user_first_nm, cost_ctr, machine_nm, active "
    22.         sSQL = sSQL & "FROM mytablename "
    23.         Select Case pstrStatus
    24.             Case "Active"
    25.                 sSQL = sSQL & "WHERE active = 'T' "
    26.             Case "Inactive"
    27.                 sSQL = sSQL & "WHERE active = 'F' "
    28.         End Select
    29.         sSQL = sSQL & "ORDER BY user_last_nm "
    30.         Try
    31.             Dim da As New System.Data.SqlClient.SqlDataAdapter(sSQL, myConnection)
    32.             da.Fill(ds)
    33.             Dim rowcount As Integer = ds.Tables.Item(0).Rows.Count
    34.             'now, for the display, we need to use the datareader
    35.             Dim command As New SqlCommand(sSQL, myConnection)
    36.             Dim reader As SqlDataReader = command.ExecuteReader()
    37.             Me.Cursor = Cursors.WaitCursor
    38.             Me.ProgressBar1.Value = 0
    39.             Me.ProgressBar1.Visible = True
    40.             Me.lblProgress.Visible = True
    41.             Me.ProgressBar1.Maximum = rowcount
    42.             System.Windows.Forms.Application.DoEvents()
    43.             shWorkSheet.Name = "Report Output"
    44.  
    45.             With oPageSetup
    46.                 .CenterHeader = "&""Arial,Bold""&16User Table List " & Chr(10)
    47.                 .PrintTitleRows = "$1:$5"
    48.                 .CenterFooter = "Page &P of &N"
    49.                 .LeftFooter = "&8Prepared By:  " & UserInfo.RealName & vbCr & "Report #AUP1 "
    50.                 .RightFooter = "&8" & Format(Now, "Long Date") & vbCr & "AUP.NET Ver. " & My.Application.Info.Version.Major & "." & My.Application.Info.Version.Minor
    51.                 .FooterMargin = objExcel.InchesToPoints(0.35)
    52.                 .HeaderMargin = objExcel.InchesToPoints(0.35)
    53.                 .LeftMargin = objExcel.InchesToPoints(0.75)
    54.                 .RightMargin = objExcel.InchesToPoints(0.75)
    55.                 .TopMargin = objExcel.InchesToPoints(1.0#)
    56.                 .BottomMargin = objExcel.InchesToPoints(0.75)
    57.                 .FitToPagesWide = 1
    58.                 .FitToPagesTall = 10
    59.                 .Zoom = False
    60.             End With
    61.  
    62.             shWorkSheet.Range("C4").Value = "Last"
    63.             shWorkSheet.Range("C5").Value = "Name"
    64.             shWorkSheet.Range("D4").Value = "First "
    65.             shWorkSheet.Range("D5").Value = "Name"
    66.             shWorkSheet.Range("E4").Value = "Cost"
    67.             shWorkSheet.Range("E5").Value = "Center"
    68.             shWorkSheet.Range("F4").Value = "Machine"
    69.             shWorkSheet.Range("F5").Value = "Name"
    70.             shWorkSheet.Range("G4").Value = "Active"
    71.             shWorkSheet.Range("G5").Value = "Status"
    72.             shWorkSheet.Range("C1:G5").Font.Bold = True
    73.             shWorkSheet.Range("B1:B2").Font.Italic = True
    74.             shWorkSheet.Range("B1:B2").Font.Size = 12
    75.             oRange.VerticalAlignment = Excel.Constants.xlTop
    76.             shWorkSheet.Range("E:E").HorizontalAlignment = Excel.Constants.xlCenter
    77.             rngRowStart = shWorkSheet.Range("C7")
    78.             While reader.Read()
    79.                 For i = 0 To 4
    80.                     rngRowStart.Offset(0, 0).Value = reader.GetString(0)
    81.                     rngRowStart.Offset(0, 1).Value = reader.GetString(1)
    82.                     rngRowStart.Offset(0, 2).Value = reader.GetString(2)
    83.                     If Not reader.IsDBNull(3) Then 'test for nulls
    84.                         rngRowStart.Offset(0, 3).Value = reader.GetString(3)
    85.                     Else
    86.                         rngRowStart.Offset(0, 3).Value = "None Logged"
    87.                     End If
    88.                     If reader.GetString(4) = "T" Then
    89.                         rngRowStart.Offset(0, 4).Value = "Active" 'active status
    90.                     Else
    91.                         rngRowStart.Offset(0, 4).Value = "Inactive"
    92.                     End If
    93.                 Next
    94.                 ProgressBar1.Value = ProgressBar1.Value + 1
    95.                 System.Windows.Forms.Application.DoEvents()
    96.                 lblProgress.Text = Int((ProgressBar1.Value / ProgressBar1.Maximum) * 100) & "% Complete"
    97.                 rngRowStart = rngRowStart.Offset(1, 0)
    98.             End While
    99.             oRange = DirectCast(shWorkSheet.Columns("C:C"), Excel.Range)
    100.             oRange.ColumnWidth = 15
    101.             oRange = DirectCast(shWorkSheet.Columns("F:F"), Excel.Range)
    102.  
    103.             oRange.ColumnWidth = 15
    104.             reader.Close()
    105.             da.Dispose()
    106.         Catch ex As Exception
    107.             MessageBox.Show(ex.Message & " " & ex.Source, "PrintUserList")
    108.         End Try
    109.         Me.Cursor = Cursors.Default
    110.         objExcel.Visible = True
    111.         Me.ProgressBar1.Value = 0
    112.         Me.ProgressBar1.Visible = False
    113.         Me.lblProgress.Text = vbNullString
    114.         Me.lblProgress.Visible = False
    115.     End Sub
    116. End Class

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