using Microsoft.AspNetCore.Mvc;
using Microsoft.Reporting.NETCore;
using System.Diagnostics;
using System.Net.Mime;
namespace RdlcReportApp.Controllers
{
public class ReportsController : Controller
{
public IActionResult Index()
{
return View();
}
public IActionResult Export()
{
// 載入報表
var report = new LocalReport();
// 取得報表資料
var items = _northwindcontext.Employees.ToList().ToDataTable();
// 取得報表路徑
var assembly = typeof(Reports.Const).Assembly;
using var rs = assembly.GetManifestResourceStream("KH.Lab.MyRDLC.Reports.RDLCs.Report1.rdlc");
report.LoadReportDefinition(rs);
// 設定報表資料來源
report.DataSources.Add(new ReportDataSource("Employees", items));
// 生成報表
var result = report.Render("EXCEL");
// 回傳檔案
return File(result, MediaTypeNames.Application.Octet, "員工資料表.xlsx");
}
public IActionResult Print()
{
// 載入報表
var report = new LocalReport();
// 取得報表資料
var items = _northwindcontext.Employees.ToList().ToDataTable();
// 取得報表路徑
var assembly = typeof(Reports.Const).Assembly;
using var rs = assembly.GetManifestResourceStream("KH.Lab.MyRDLC.Reports.RDLCs.Report1.rdlc");
report.LoadReportDefinition(rs);
// 設定報表資料來源
report.DataSources.Add(new ReportDataSource("Employees", items));
// 生成報表
var result = report.Render("PDF");
// 回傳檔案
return File(result, MediaTypeNames.Application.Pdf, "員工資料表.pdf");
}
}
}
using AspNetCore.Reporting;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
using System.Net.Mime;
namespace RdlcReportApp.Controllers
{
public class ReportsController : Controller
{
public IActionResult Index()
{
return View();
}
public IActionResult Export()
{
// 取得報表路徑
var reportPath = Path.Combine(Directory.GetCurrentDirectory(), "RDLCs", "Report1.rdlc");
// 載入報表
var report = new LocalReport(reportPath);
// 取得報表資料
var items = _northwindcontext.Employees.ToList().ToDataTable();
// 設定報表資料來源
report.AddDataSource(nameof(Employees), items);
// 生成報表
var result = report.Execute(RenderType.Excel);
// 回傳檔案
return File(result.MainStream, MediaTypeNames.Application.Octet, "員工資料表.xlsx");
}
public IActionResult Print()
{
// 取得報表路徑
var reportPath = Path.Combine(AppContext.BaseDirectory, "RDLCs", "Report1.rdlc");
// 載入報表
var report = new LocalReport(reportPath);
// 取得報表資料
var items = _northwindcontext.Employees.ToList().ToDataTable();
// 設定報表資料來源
report.AddDataSource(nameof(Employees), items);
// 生成報表
var result = report.Execute(RenderType.Pdf);
// 回傳檔案
return File(result.MainStream, MediaTypeNames.Application.Pdf, "員工資料表.pdf");
}
}
}
var builder = WebApplication.CreateBuilder(args);
// 增加服務
builder.Services.AddControllersWithViews();
var app = builder.Build();
// 配置中間件
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run();
public IActionResult List2()
{
// 載入報表
var report = new LocalReport();
// 取得報表路徑
var assembly = typeof(Reports.Const).Assembly;
using var rs = assembly.GetManifestResourceStream("KH.Lab.MyRDLC.Reports.RDLCs.Report3.rdlc");
report.LoadReportDefinition(rs);
// 取得報表資料
var items = _northwindcontext.Orders.ToList();
var ds = new DeliveryDataSet();
foreach (var item in items)
{
DataRow dr = ds.Tables["DeliveryOrder"].NewRow();
string barcode = DateTime.Now.ToString("yyyyMMddHHmmssfff");
dr["DeliveryNumber"] = barcode;
dr["ShipName"] = item.ShipName;
dr["ShipAddress"] = item.ShipAddress;
dr["BarCode"] = BarcodeHelper.CreateBarCodeBytes(barcode, 300, 100);
dr["QRCode"] = BarcodeHelper.CreateQRCodeBytes(barcode, 200, 200);
ds.Tables["DeliveryOrder"].Rows.Add(dr);
}
// 設定報表資料來源
report.DataSources.Add(new ReportDataSource("DeliveryOrder", ds.Tables["DeliveryOrder"]));
// 生成報表
var result = report.Render("PDF");
// 回傳檔案
return File(result, MediaTypeNames.Application.Pdf, "配送單2.pdf");
}
public IActionResult List2()
{
// 取得報表路徑
var reportPath = Path.Combine(AppContext.BaseDirectory, "RDLCs", "Report3.rdlc");
// 載入報表
var report = new LocalReport(reportPath);
// 取得報表資料
var items = _northwindcontext.Orders.ToList();
var ds = new DeliveryDataSet();
foreach (var item in items)
{
DataRow dr = ds.Tables["DeliveryOrder"].NewRow();
string barcode = DateTime.Now.ToString("yyyyMMddHHmmssfff");
dr["DeliveryNumber"] = barcode;
dr["ShipName"] = item.ShipName;
dr["ShipAddress"] = item.ShipAddress;
dr["BarCode"] = BarcodeHelper.CreateBarCodeBytes(barcode, 300, 100);
dr["QRCode"] = BarcodeHelper.CreateQRCodeBytes(barcode, 200, 200);
ds.Tables["DeliveryOrder"].Rows.Add(dr);
}
// 設定報表資料來源
report.AddDataSource("DeliveryOrder", ds.Tables["DeliveryOrder"]);
// 生成報表
var result = report.Execute(RenderType.Pdf);
// 回傳檔案
return File(result.MainStream, MediaTypeNames.Application.Pdf, "配送單2.pdf");
}
public static class DataTableHelper
{
public static DataTable ToDataTable<T>(this List<T> items)
{
var dataTable = new DataTable(typeof(T).Name);
//Get all the properties
PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo prop in Props)
{
Type type = Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType;
type = type.IsEnum ? Enum.GetUnderlyingType(type) : type;
//Setting column names as Property names
dataTable.Columns.Add(prop.Name, type);
}
foreach (T item in items)
{
var values = new object[Props.Length];
for (int i = 0; i < Props.Length; i++)
{
//inserting property values to datatable rows
values[i] = Props[i].GetValue(item, null) ?? DBNull.Value;
}
dataTable.Rows.Add(values);
}
//put a breakpoint here and check datatable
return dataTable;
}
}