Develop a universal report component with VB.NET. The component has a flexible, dynamic reporting function, and the user can select its report content, but report data not only can control print, but also save in multiple file formats. The method and process it implemented now is as follows:
Realization: Use Excel's powerful spreadsheet function to send our data to Excel. At the same time, a certain control is provided to the user an operation interface to achieve the purpose. This total process is packaged into a component for
The software is directly called.
Development steps
1, start Visual Studio.net. Select the Visual Basic project in the new project, select the class library in the template, enter the class library file name in the name, such as Report Once, enter the class library development environment, and change the Class1 to CREPORT. Add a reference to the project menu, find Microsoft
After the Excel 9.0 Object Library is determined, Excel is referenced. In the top of the class name written on Imports System Imports System.Windows.Forms, Imports Excel.ApplicationClass, Imports Excel.XlLineStyle, Imports Excel.XlPattern, Imports Excel.XlBorderWeight, Imports Excel.Constants, Imports Excel.XlBordersIndex eight reference statement. Here we reference the NET's system namespace and the namespace of Excel apps and constants. which is:
Imports system
Imports System.Windows.Forms
Imports Excel.ApplicationClassClass
Imports Excel.xllinestyle
Imports Excel.xlpattern
Imports Excel.xlborterweight
Imports Excel.Constants
Imports Excel.xlbordersindex
2. Define the namespace to reopot, namely:
Namespace reopot
3. Define the public class CREPORT that is called for application software. Define private variables FRMSetup of class CREPORT, which will initiate an interface for user operation. Define the attribute DataGrid of class CREPORT, which provides users to transfer the data grid name to be printed, define the attribute DATATABLE of class CREPORT, which is used to transfer the data table name to be printed. Define the public method RUN of class CREPORT, which launches the component. Write their functions separately. code show as below:
Public Class Creport
DIM FRMSETUP AS New FRMPRINTSETUP ()
Public property DataGrid () AS DataGrid
Get
'DataTable = MDATABLE
END GET
Set (ByVal Value As DataGrid)
FRMSetup.DataTable = value.datasource
FRMSetup.maintitle = value.captiontext
End set
End Property
Public property DataTable () AS DataTable
Get
'DataTable = MDATABLE
END GET
Set (ByVal Value As DataTable)
FRMSETUP.DATATABLE = Value
End set
End Property
Public Sub Run ()
FRMSetup.showdialog ()
FRMSetUp = Nothingend Sub
END CLASS
4. Define public categories CPUBLIC
Excel, this class mainly defines Excel's program objects, launching Excel and prints and saves reports. code show as below:
'This class is not changed (public)
Public Class CPBLICEXCEL
Public xlapp as new excel.Application () 'Excel program object
'Public gblnsaveReport as boolean' This Report is output to become a XLS file
'Public GSTRPATH AS STRING' This Report Output Became the file name of XLS
Private ExcelwasNotrunning as boolean 'The status of the original Excel program
Private Declare Function FindWindow Lib "User32" Alias "FindWindowa" (Byval
LpClassName As String, Byval Lpwindowname As String) As long
Private Declare Function SendMessage Lib "User32" Alias "SendMessagea" (ByVal
HWND As Long, Byval WMSG As Long, Byval WParam As long, ByVal LPARAM As long
As long
'Identify if Excel is running
Private function detectexcel () as boolean
Const WM_USER = 1024
DIM HWND As Long
HWnd = findwindow ("xlmain", 0)
IF HWND <> 0 Then 'hwnd <> 0 means Excel IS
Running.
SendMessage (HWnd, WM_USER 18, 0, 0)
Detectexcel = true
Else
Detectexcel = false
END IF
END FUNCTION
'Open Excel
Public Sub OpenExcelsheet ()
ExcelwasNotrunning = NOT DetecTexcel ()
IF ExcelwasNotrunning Then
XLAPP = getObject ("", "excel.application")
Else
XLAPP = getObject (, "Excel.Application")
END IF
XLapp.Workbooks.add () '(Excelsheetname)
End Sub
'Print preview report
Public Sub PrintPreview ()
XLapp.caption = "Report"
XLapp.displayAlerts = false
XLapp.visible = true
XLapp.Activeworkbook.printpreview ()
XLAPP.Visible = FALSE
End Sub
'Printing report
Public Sub Print ()
XLapp.displayAlerts = false
XLAPP.Visible = FALSE
XLapp.Activeworkbook.printout ()
End Sub
'Save Report Public Sub Saveas (Byval FileName As String)
Dim file as system.IO.FILE
Try
'Ness file.exists (filename) = true kil
'file.delete (filename)
'End if
XLapp.ActiveWorkbook.saveas (filename)
Catch exception 'when y = 0' catch the error.
MsgBox (ex.totring) 'Show Friendly Error Message.
'Beep ()' Beep after Error Processing.
END TRY
End Sub
END CLASS