Wrox Programmer Forums
|
.NET Framework 2.0 For discussion of the Microsoft .NET Framework 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the .NET Framework 2.0 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 26th, 2008, 12:19 PM
Registered User
 
Join Date: Aug 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Excel reports in vb.net

I am in need of pulling data from Access and dumping the data into Excel and formatting into a useable report.
I have a front end where the user would choose certain criteria to generate a query which will be sent to the database.
The returned recordset gets sent to a spreadsheet which I will then format to suit.

I need help properly sending the query and returning the data. Thanks!!!

~Justin
 
Old March 3rd, 2014, 03:53 AM
Registered User
 
Join Date: Feb 2014
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default RE: Excel reports in vb.net

Hi, you can try the following VB.NET code.
Note you need to fill in TODO's: DB connection string and SQL query.
Code:
        Dim connString As String = "Provider=Microsoft.ACE.OLEDB.12.0;data source=C:\...\TODO.accdb"
        Dim results As New DataTable("Report")

        ' Retrieve Access DB data and fill DataTable.
        Using conn As New OleDbConnection(connString)
            Dim cmd As New OleDbCommand("SELECT * FROM TODO WHERE TODO", conn)
            conn.Open()
            Dim adapter As New OleDbDataAdapter(cmd)
            adapter.Fill(results)
        End Using

        ' Create new Excel report.
        Dim report As New ExcelFile()
        Dim reportSheet As ExcelWorksheet = report.Worksheets.Add(results.TableName)

        ' Insert DataTable to Excel sheet.
        Dim options As New InsertDataTableOptions("A1")
        options.ColumnHeaders = True
        reportSheet.InsertDataTable(results, options)

        ' Format header row.
        Dim headerFont As ExcelFont = reportSheet.Rows(0).Style.Font
        headerFont.Weight = ExcelFont.BoldWeight
        headerFont.Size = CInt(LengthUnitConverter.Convert(14, LengthUnit.Point, LengthUnit.Twip))

        ' AutoFit filled columns.
        Dim columnCount As Integer = reportSheet.CalculateMaxUsedColumns()
        For i As Integer = 0 To columnCount - 1
            reportSheet.Columns(i).AutoFit()
        Next

        ' Save Excel file.
        report.Save("Report.xls")
I hope this can get anyone to start coding the similar requirements. Also you will notice that I used this VB.NET Excel component to generate a "Report.xls" file. It has great performances and it does not use excel automation, so it is quite suited for tasks like generating large reports.





Similar Threads
Thread Thread Starter Forum Replies Last Post
ASP.NET 1.1,VB.NET,crystal reports, SQl server gvi Crystal Reports 1 September 11th, 2008 02:55 AM
Crystal Reports 11 from VB to VB.net koushik_b2002 Crystal Reports 2 June 1st, 2008 01:58 AM
Crystal reports and VB.NET minnug Crystal Reports 0 May 20th, 2007 10:09 PM
exporting crystal reports through vb.net nikks9 Crystal Reports 0 May 16th, 2006 04:16 AM
Crystal Reports in VB.Net PatMcM VB.NET 2002/2003 Basics 0 January 31st, 2006 03:37 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.