VS2017 integrates FastReport. Net and saves the template to the database,

Source: Internet
Author: User

VS2017 integrates FastReport. Net and saves the template to the database,

Based on the idea of separation of development and implementation, it is particularly important to design a general report design form (Fig. 1 ):

Requirements and advantages:

I. The report design form supports calling all documents. One document supports multiple print templates.

II. The report template is stored in the database. One is to support client design and save templates, and the other is to modify all clients at a time to take effect.

III. Click Save to save the template in the database. Click Save to save the template as a file. In this way, the template can be copied.

IV. Preview and print. You do not need to enter the design interface every time to preview or print the template.

Development Environment:

VS2017 + SQL SERVER 2014 + FastReport. Net (2017.1.16)

Due to the large amount of space, this article mainly shares the function of the design button. Gossip!

1. Data Table design.

CREATE TABLE [dbo].[AT_REPORT](    [FORMID] [varchar](20) NOT NULL,    [RPT_NO] [varchar](20) NOT NULL,    [RPT_NAME] [varchar](50) NULL,    [FILEDATA] [varbinary](max) NULL, CONSTRAINT [PK_AT_REPORT] PRIMARY KEY CLUSTERED (    [FORMID] ASC,    [RPT_NO] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

2. Add the FastReportDesign form (Figure 1 ):

1.1 reference the FastReport library file.

  

1.2 code reference.

using System.Data.SqlClient;using FastReport;using FastReport.Utils;using FastReport.Design;

2. Define attributes and variables:

Note: FormID is the Document ID. This attribute is assigned to the call report design form of the document. RptNo and RptName are assigned values when you click the report type in Figure 1.

Public string FormID {get; set ;}= "PRDT"; // document IDprivate string RptNo, RptName; // report ID and name private DataTable RptTable; // data table private DataRow RptRow; // data row (report data source) private bool isSaveAs = false; // save

3. Double-click the design button:

Note: tvwRight is the name of Treeview on the Right of figure 1.

// Design private void btnDes_Click (object sender, EventArgs e) {if (tvwRight. SelectedNode! = Null) {if (! String. IsNullOrEmpty (FormID )&&! String. IsNullOrEmpty (RptNo) {InitializeReport ("DESIGN");} else {MessageBox. Show ("report acquisition failed. "," Information ", MessageBoxButtons. OK, MessageBoxIcon. Information) ;}} else {MessageBox. Show (" select a report first. "," Information ", MessageBoxButtons. OK, MessageBoxIcon. Information );}}

4. Initialization Method:

Note: mymeans. GetDataSet is a self-written class method, mainly used to generate DataSet by SQL.

// Initialize the report private void InitializeReport (string RptMode) {DataSet Ds = mymeans. getDataSet ("SELECT RPT_NO, RPT_NAME, filedata from AT_REPORT where formid = '" + FormID + "' AND RPT_NO = '" + RptNo + "'", "REPORT "); rptTable = Ds. tables [0]; RptRow = RptTable. rows [0]; RegisterDesignerEvents (); DesignReport (RptMode );}

5. Registration event:

Note: The FastReport designer saves and saves the menu as a file. Because we need to save the design template to the database, We need to block the original functions of the system and write them by ourselves. In addition, the Save button does not bring up a dialog box. Therefore, OpenSaveDialogEventHandler is triggered only when you click Save. (Refer to the FastReport built-in example CustomOpenSaveDialogs)

// Menu event registration private void RegisterDesignerEvents () {Config. DesignerSettings. CustomSaveDialog + = new region (region); Config. DesignerSettings. CustomSaveReport + = new OpenSaveReportEventHandler (region );}

6. design template loading:

// Design Report private void DesignReport (string RptMode) {using (Report TargetReport = new Report () {TargetReport. fileName = RptName; if (RptRow ["FILEDATA"]. toString (). length> 0) {byte [] ReportBytes = (byte []) RptRow ["FILEDATA"]; using (MemoryStream Stream = new MemoryStream (ReportBytes) {TargetReport. load (Stream) ;}// operation method: DESIGN-DESIGN; PREVIEW-PREVIEW; PRINT-PRINT if (RptMode = "DESIGN") {TargetReport. design ();} else if (RptMode = "PREVIEW") {TargetReport. prepare (); TargetReport. showPrepared ();} else if (RptMode = "PRINT") {TargetReport. print ();}}}

7. Save As dialog box:

NOTE: If isSaveAs is set to true, the Save As button is clicked for the table.

// Save the menu: Dialog Box private void DesignerSettings_CustomSaveDialog (object sender, OpenSaveDialogEventArgs e) {isSaveAs = true ;}

8. Save the delegate function:

// Save the menu: Delegate function private void DesignerSettings_CustomSaveReport (object sender, OpenSaveReportEventArgs e) {SaveReport (e. Report );}

9. Save the Report Template:

Note: mymeans. ConOpen () is a self-written class method, mainly used to connect to the database.

// Save the private void SaveReport (Report TargetReport) {try {using (MemoryStream msStream = new MemoryStream () {// save TargetReport. save (msStream); RptRow ["FILEDATA"] = msStream. toArray (); if (MyMeans. con = null | MyMeans. con. state! = ConnectionState. open) {mymeans. conOpen ();} SqlCommand Cmd = MyMeans. con. createCommand (); Cmd. commandText = "UPDATE AT_REPORT set filedata = @ filedata where formid = @ formid and RPT_NO = @ RPT_NO"; Cmd. parameters. addWithValue ("@ FILEDATA", msStream. toArray (); Cmd. parameters. addWithValue ("@ FORMID", FormID); Cmd. parameters. addWithValue ("@ RPT_NO", RptNo); Cmd. executeNonQuery (); // save as if (isSaveAs = true) {SaveFileDialog saveFileDialog = new SaveFileDialog (); // set the file type saveFileDialog. filter = "report file (*. frx) | *. frx | C # file (*. cs) | *. cs | FastReport VCL report (*. fr3) | *. fr3 | RDL file (* rdl) | *. rdl "; // set the display sequence of the default file type saveFileDialog. filterIndex = 1; // whether to automatically add the saveFileDialog extension to the file name. addExtension = true; // whether to remember the last opened directory saveFileDialog. restoreDirectory = true; // set the default file name saveFileDialog. fileName = RptName; // press the OK button if (saveFileDialog. showDialog () = DialogResult. OK) {// obtain the file path string localFilePath = saveFileDialog. fileName. toString (); // memory flow byte array byte [] arrBuffer = new byte [msStream. length]; msStream. position = 0; int intBuffer = msStream. read (arrBuffer, 0, arrBuffer. length); msStream. write (arrBuffer, 0, intBuffer); // save FileStream fsStream = new FileStream (localFilePath, FileMode. create); msStream. writeTo (fsStream); // The resource is released fsStream. close (); fsStream = null;} // assign the initial isSaveAs = false ;}} catch (Exception ex) {MessageBox. show (ex. message, "prompt", MessageBoxButtons. OK, MessageBoxIcon. information );}}

Now, let's share some of the main functions and hope to help you.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.