Imam Ferianto Blogs

sekedar catatan kecil saja

Fastreport merupakan component report yang pada awalnya khusus untuk Borlad Delphi (sekarang embarcardero). Tersedia dalam bentuk VCL (visual componen library) yang memudahkan membuat report dengan drag drop dan interfase WYSYWG.  Sekarang fastreport sudah mensupport banyak language termasuk c# dotnet core. Dari sekian banyak komponen untuk membuat report yang dapat di pakai di dotnet core saya memilih fastreport.

Mengapa saya pilih fastreport? karena lebih cepat didevelop dengan fastreport designernya dan componen ini dapat berjalan di mac os, linux dan windows serta lumayan bagus performancenya. Untungnya lagi ada versi opensourcenya juga sehingga tidak ada lagi kekhawatiran akan biaya lisensi. Kali ini saya ulas agak teknis karena pembahasan tentang fastreport opensource pada dotnet core ini tampaknya masih sedikit di google.

Berikut perbandingan versi fastreport.

Features  FastReport
Open Source
 FastReport
.NET Core
 FastReport
.NET
Supported Projects
WinForms Applications +
.NET Core 3.1/.NET 5 WinForms +
.NET 5 + +
Console Applications + + +
ASP.NET ASPX (WebForms) Applications +
ASP.NET MVC Applications + + +
ASP.NET MVC SPA + + +
ASP.NET MVC PWA + + +
WCF Applications +
Web API Applications + + +
Supported Operation Systems
Windows + + +
Windows Server + + +
Linux + +
macOS + +
Preview
In Application +
In Web browser + + +
In Viewer Application (for Windows) + + +
In FastReport Pages (Android) + + +
Print
Printing on local or network printers with support of tray
selection, duplex, scaling, copy names
+
Print from Web browser + + +
Print from Web browser and PDF + +
Output formats and delivery
Export in HTML, BMP, PNG, JPEG, GIF, TIFF, EMF + + +
Export in PDF * + +
Export in XLSX, DOCX, PPTX, ODS, ODT, RTF, Text, XPS, XML, XAML,
PS, PPML, LaTeX, Json, Dbf, Csv, XLS (Biff8), SVG, ZPL
+ +
Save in prepared report FPX + + +
Sending reports by email, FTP +
Uploading in the clouds Box, Dropbox, GoogleDrive, OneDrive
(SkyDrive)
+
Data Sources
XML, CSV, Json, MS SQL, Postgres, SQLite, Couchbase, MySQL,
MongoDB, RavenDB, Oracle
+ + +
OLE DB, ODBC, Access +
Plugins for DB2, Firebird, NosDB, SharePoint, SqlAnywhere,
SqlCe, VistaDB
+
Application datasets, lists, arrays and business objects + + +
Features and report objects
Internal report script C#, VB.NET + + +
Text, Picture, Line, Shape, Subreport, Table, Matrix, PolyLine,
Polygon, Barcode, Checkbox, ZipCode, CellularText, LinearGauge, SimpleGauge,
RadialGauge, SimpleProgressGauge, Html
+ + +
RichText, Map, SVG + +
Chart, Sparkline + +
Dialogs +
Interactive Reports (clicks, hyperlinks, link on report,
bookmarks, outline)
+ + +
Report Designing
Designer Application (WinForms) ** +
Using with the Online Designer + + +
Designer in Application (for WinForms)  ** +
Report Templates Import
Crystal Reports +
SSRS RDL (RDLC) +
List&Label +
DevExpress +
Localization
29 languages + + +
Integration
Visual Studio ToolBox +
Active Query Builder +
Steema TeeChart +
Distribution
Packages in Nuget.org + +
Sources in Github +
Installation package + +
Sources in installation (Professional and Enterprise editions) + +

Kalau saya sudah cukup dengan versi opensourcenya, tetapi jika menginginkan fitur yang lebih advance bisa membeli lisensi sesuai kebutuhan. Ikuti link berikut untuk mendownload sourcecode dari Fastreport Opensource. Untuk user manual dapat di baca disini atau disini .

Untuk membuat layout report kita bisa menggunakan Fastreport Designer Community Edition yang dapat di Download disini.

Cara membuat report templatenya dapat dilihat pada video youtube berikut (klik pada gambar):

Untuk melakukan rendering report pada project dotnet-core, terlebih dahulu create project (jika belum ada)

mkdir app
cd app
dotnet new mvc

Kemudian tambahkan package fastreport dan data component sesuai database yang dipakai

dotnet add package FastReport.OpenSource
dotnet add package FastReport.OpenSource.Export.PdfSimple
dotnet add package FastReport.OpenSource.Data.MsSql

Contoh kode controller implementasi fastreport

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using System.Security.Authentication;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Html;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Data.SqlClient;
using app.Models;

//#required for fastreport
using FastReport;
using FastReport.Utils;
using FastReport.Export.Html;
using FastReport.Web;
using FastReport.Data;
using FastReport.Utils;
using FastReport.Export.PdfSimple;

namespace app.Controllers
{
    [Authorize(Policy = "Authenticated")]
    public class ReportController : Controller
    {
        private readonly MyDBContext _context;
        private readonly IHttpContextAccessor _accessor;
        private readonly ConfigVars _configvar;
        private readonly IWebHostEnvironment _hostingEnvironment;
        private readonly IServiceScopeFactory _serviceScopeFactory;
        private readonly IGeneratePdf _generatePdf;
        private readonly IConfiguration _Configuration;

        public ReportController(
            IWebHostEnvironment hostingEnvironment,
            MyDBContext context,
            IHttpContextAccessor accessor,
            ConfigVars configvar,
            IServiceScopeFactory serviceScopeFactory,
            IGeneratePdf generatePdf,
            IConfiguration configuration)
        {
            _hostingEnvironment = hostingEnvironment;
            _context = context;
            _accessor = accessor;
            _configvar = configvar;
            _serviceScopeFactory = serviceScopeFactory;
            _generatePdf = generatePdf;
            _Configuration=configuration;
        }

        public WebReport GetReport()
        {
            //membuat objek report
            WebReport webReport = new WebReport();

            //load report file
            string reporttemplatepath=Path.Combine(_hostingEnvironment.ContentRootPath,"fastreport");
            string reportfile=Path.Combine(reporttemplatepath,"report1.frx");
           
            //load report file
            webReport.Report.Load(reportfile);

            //register type koneksi
            RegisteredObjects.AddConnection(typeof(MsSqlDataConnection));

            //ambil koneksi string dari konfigurasi
            string connstring = _Configuration.GetConnectionString("mssql_con");

            //optional set dynamic connection
            if(webReport.Report.Dictionary.Connections==null||webReport.Report.Dictionary.Connections.Count==0){
                var mssqlDataConnection = new MsSqlDataConnection();
                mssqlDataConnection.ConnectionString = connstring;
                webReport.Report.Dictionary.Connections.Add(mssqlDataConnection);
            }else{
                webReport.Report.Dictionary.Connections[0].ConnectionString=connstring;
            }
            
            //jika ingin membuat dinamis query tambahkan kode berikut:
            /*
            TableDataSource table = webReport.Report.GetDataSource("x") as TableDataSource;
            if(table!=null) table.SelectCommand = String.Format("select * from x",skey);
            */

            return webReport;
        }

        //tampilkan report dalam html mode
        public IActionResult Index()
        {
            var webReport = GetReport();
            //webReport.Width = Unit.Percentage(100);
            //webReport.Height = Unit.Percentage(100);
            webReport.ShowToolbar = true;
            ViewBag.WebReport = webReport;
            return View();
        }

        //fungsi download report ke pdf
        public IActionResult Pdf()
        {
            var webReport = GetReport();
            webReport.Zoom = 0.85f;
            webReport.Inline = true;
            webReport.Report.Prepare();
            using (MemoryStream ms = new MemoryStream())
            {
                PDFSimpleExport pdfExport = new PDFSimpleExport();
                pdfExport.Export(webReport.Report, ms);
                ms.Flush();
                return File(ms.ToArray(), "application/pdf", Path.GetFileNameWithoutExtension("report1") + ".pdf");
            }    
        }   


    }
}

Contoh kode tampilan html report, Views/Home/Index.cshtml

@{
    ViewData["Title"] = "Home Page";
}


<div>
@await ViewBag.WebReport.Render()
</div>
<script type="text/javascript">
	
</script>

Sekian pembahasan membuat report dengan fastreport di dotnetcore. Semoga bermanfaat.