.Net Core RDLC Report, Coravel and Email
ReportViewerCore.NETCore (15.1.18)
Download Extension:
Winform, design report
Add NuGet Package (WinForm)
// RDLC report (pdf, CSV, word, excel)
-- New Project, Windows Forms App (.NET Framework)
-- toolbox, add a reportView (report view)
-- add dataSet (link database)
-- add a report (.rdlc)
-- link data source, design report (above order is very important)
-- add packages
-- Microsoft.ReportViewer.WinForms
-- System.Data.SqlClient
- Extension (Tools)
-- Microsoft RDLC Report Designer (2023)
// Schedule
-- add Coravel package
// Email sender
-- Repository
-- DisplayWeightRepository.cs
-- Controller
-- WeighReportController.cs (call ReportService)
-- ReportService
-- EmailSendService.cs
-- IEmailSendService.cs
-- RdlcReportService.cs (call .rdlc)
-- IRdlcReportService.cs
-- SendDailyReportsService.cs (scheduled call)
-- SendWeeklyReportsService.cs
-- SendMonthlyReportsService.cs
// API - get PDF report
[HttpGet]
[Route("api/weighing/report/{beginDate}/{endDate}")]
public async Task<ActionResult> GetWeighingsByDate(DateTime beginDate, DateTime endDate)
{
string reportName = "WeighReport1";
string reportType = "PDF";
if (!ModelState.IsValid)
return BadRequest(ModelState);
try
{
var result = _mapper.Map<List<WeighReportViewModel>>(await _repository.GetWeighingsByDateAsync(beginDate, endDate));
if (result == null)
{
_logger.LogInformation($"Can not found data!");
return NotFound();
}
var reportArray = _rdlcReportService.GenerateReportAsync(reportName, reportType, result);
return File(reportArray, getMimeType(reportType), getReportName(reportName, reportType));
// return Ok(result);
}
catch (Exception ex)
{
_logger.LogInformation($"Exception: " + ex.Message);
}
return
BadRequest("Could not get weighing records");
}
// support file types
private string getMimeType(string reportType)
{
var mimeType = "application/pdf";
switch (reportType.ToUpper())
{
default:
case "PDF":
mimeType = "application/pdf";
break;
case "HTML5":
mimeType = "text/html";
break;
case "EXCELOPENXML":
mimeType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
break;
case "WORDOPENXML":
mimeType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
break;
}
return mimeType;
}
private string getReportName(string reportName, string reportType)
{
var outputFileName = reportName + ".pdf";
switch (reportType.ToUpper())
{
default:
case "PDF":
outputFileName = reportName + ".pdf";
break;
case "HTML5":
outputFileName = reportName + ".html";
break;
case "EXCELOPENXML":
outputFileName = reportName + ".xlsx";
break;
case "WORDOPENXML":
outputFileName = reportName + ".docx";
break;
}
return outputFileName;
}
// Service
public byte[] GenerateReportAsync(string reportName, string reportType, List<WeighReportViewModel> dataList)
{
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
Encoding.GetEncoding("utf-8");
try
{
var rdlc_name_array = this.GetType().GetTypeInfo().Assembly.GetManifestResourceNames();
Assembly assembly = Assembly.GetExecutingAssembly();
if (assembly != null)
{
LocalReport localReport = new LocalReport();
// var parameters = new[] { new ReportParameter("Title", "Invoice 4/2020") };
Stream reportDefinition = assembly.GetManifestResourceStream(rdlc_name_array[0]);
localReport.LoadReportDefinition(reportDefinition);
localReport.DataSources.Add(new ReportDataSource("DataSet1", dataList));
// localReport.SetParameters(parameters);
byte[] result = localReport.Render(reportType.ToUpper());
return result;
}
return null;
}
catch (Exception ex)
{
_logger.LogInformation($"Exception: " + ex.Message);
}
return null;
}
// Some code
string fileDirPath = Assembly.GetExecutingAssembly().Location.Replace("NWIWebAPI.dll", string.Empty);
string rdlcFilePath = string.Format("{0}System.API\\ReportFiles\\{1}.rdlc", fileDirPath, reportName);
// localReport.ReportPath = rdlcFilePath;
// localReport.ReportPath = "D:\\template-app-sample\\rdlc-report\\WebAPIRdlc\\NWIWebAPI\\System.API\\ReportFiles\\WeighReport1.rdlc";
//get *.rdlc name array - "NWIWebAPI.System.API.ReportFiles.Report1.rdlc" Build Action = Embedded resource
var n1 = this.GetType().Assembly.GetManifestResourceNames();
var n2 = Assembly.GetExecutingAssembly().GetManifestResourceNames();
//"NWIWebAPI.System.API.ReportFiles.Report1.rdlc"
// Stream reportDefinition = Assembly.GetExecutingAssembly().GetManifestResourceStream(rdlc_name_array[0]);
Last updated