二、VS2022
界面設計:
后端代碼
?
項目文件
三、核心代碼
private void button1_Click(object sender, EventArgs e)
{
StiDesigner designer = new StiDesigner(report);
StiDesigner.SavingReport += StiDesigner_SavingReport;
report.RegData("TableVo", new WeightVo() { Code = "", Name = "" });
report.Compile();
report.Design();
}
private void StiDesigner_SavingReport(object sender, StiSavingObjectEventArgs e)
{
string outputDirectory = AppDomain.CurrentDomain.BaseDirectory;
report.Save($"{outputDirectory}\\Report.mrt");
MessageBox.Show("保存成功!");
}
private void button2_Click(object sender, EventArgs e)
{
string outputDirectory = AppDomain.CurrentDomain.BaseDirectory;
report.Load($"{outputDirectory}\\Report.mrt");
report.RegData("TableVo", new WeightVo() { Code = "001", Name = "這是一個打印測試模板" });
report.Compile();
report.Show();
}
private void button3_Click(object sender, EventArgs e)
{
string outputDirectory = AppDomain.CurrentDomain.BaseDirectory;
report.Load($"{outputDirectory}\\Report.mrt");
report.RegData("TableVo", new WeightVo() { Code = "001", Name = "這是一個打印測試模板" });
report.Compile();
report.Print();
}
四、性能優(yōu)化
?首次加載慢?
提前初始化和編譯模板,減少運行時延遲:
report = new StiReport();
report.Load(strPath);
report.Dictionary.Synchronize(); // 提前加載并同步 ?:ml-citation{ref="2" data="citationList"}
?多線程打印卡頓?
避免重復加載模板,直接渲染已編譯的報表:
//report.Load(strPath); // 注釋掉重復加載
report.Render();
StiPrinterSettings settings = new StiPrinterSettings(); // 配置打印參數(shù) ?:ml-citation{ref="2" data="citationList"}
五、文字自動縮放
通過設計器調整文本框屬性以適配內容:
選中文本框 → 設置 AutoWidth 和 AutoHeight 為 true。
在 Font 屬性中啟用 Auto 選項 ?6。
調整文本框尺寸后運行驗證效果。
五、常見問題處理
?組件查找與操作
StiComponent component = report.GetComponentByName("ComponentName"); // 按名稱查找組件 ?:ml-citation{ref="7" data="citationList"}
component.Bookmark = "{Categories.CategoryName}"; // 添加書簽 ?:ml-citation{ref="7" data="citationList"}
?排序設置
DataBand1.Sort = new string?:ml-citation{ref="2" data="citationList"} { "ASC", "Name" }; // 按字段升序排列 ?:ml-citation{ref="7" data="citationList"}
StiReport 核心定義與功能
?StiReport? 是 ?Stimulsoft? 公司開發(fā)的一款跨平臺報表生成工具,主要用于設計和生成動態(tài)數(shù)據(jù)驅動的專業(yè)報表。其核心特性包括:
?多平臺支持?
支持在 ?WinForms?、?ASP.NET?、?.NET Core?、?JavaScript?、?WPF? 等環(huán)境中使用,適用于桌面、Web 及移動端應用開發(fā)?。
?動態(tài)數(shù)據(jù)綁定?
可靈活綁定數(shù)據(jù)庫表、對象列表等數(shù)據(jù)源,支持運行時動態(tài)更新數(shù)據(jù)。例如,通過 RegData 方法注冊數(shù)據(jù)表并同步字典以識別新字段?。
?模板設計與導出?
通過 .mrt 模板文件定義報表布局,支持導出為 ?PDF?、?Excel?、?HTML? 等格式,并集成打印功能?。
典型使用場景
?企業(yè)級應用?
適用于財務系統(tǒng)、銷售統(tǒng)計等需要復雜報表展示的場景?。
?多平臺開發(fā)?
在混合技術棧(如 WinUI、PHP、Java)中統(tǒng)一報表生成邏輯,減少重復開發(fā)成本?。
?動態(tài)數(shù)據(jù)需求?
處理頻繁變化的數(shù)據(jù)源,例如實時庫存報告或訂單狀態(tài)更新?。
技術實現(xiàn)要點
?模板加載與優(yōu)化?
首次加載模板時建議預編譯并同步數(shù)據(jù)字典,避免運行時延遲?。
?數(shù)據(jù)源管理?
動態(tài)更新數(shù)據(jù)前需清除舊數(shù)據(jù)源(Dictionary.DataSources.Clear()),防止殘留數(shù)據(jù)沖突?。
與其他報表工具的區(qū)別
StiReport 強調 ?跨平臺兼容性? 和 ?動態(tài)數(shù)據(jù)交互能力?,其設計器提供可視化操作界面,降低代碼依賴,適合快速生成復雜格式的報表?。
閱讀原文:原文鏈接
該文章在 2025/3/25 10:37:47 編輯過