Stimulsoft.Report的代码实现功能自学整理(一)「建议收藏」
大家好,又见面了,我是你们的朋友全栈君。
一、编译环境
Visual Studio 2017,Win7 64位,Stimulsoft版本 2016.1.0.0。
二、报表环境的汉化(代码实现)
安装完Stimulsoft后,在路径下 C:\Program Files (x86)\Stimulsoft Reports.Net 2016.1 Trial\Localization中会发现很多xml文件,这些文件就是语言文件,其中 zh-CHS.xml 是简体中文的文件,zh-CHT.xml是繁体中文的文件。可以把语言文件拿出来跟着项目走,放在项目的一个文件夹中。然后通过代码加载这个文件。
Stimulsoft.Base.Localization.StiLocalization.Load(Application.StartupPath + "\\zh-CHS");
三、创建一个报表对象并添加一个新页,绑定数据集
public StiReport MyReport = new StiReport();
public DataSet fcgb;
fcgb = DBHelper.ExecuteAllQuerySql("select FormNo,ProdCode,ProdName,PUnitAmt,Unit,Price from FTHB");
fcgb.Tables[0].TableName = "tb_B";
MyReport.RegData("tb_B", fcgb);
StiPage page = MyReport.Pages[0];
其中DBHelper是自己写的数据库操作类,这个类可以自己按照实际需求来写。
四、设置报表显示内容及格式,因为是我自己做记录自己看,直接上代码
//创建分组头
StiGroupHeaderBand MyGroupReportHeaderBand_1 = new StiGroupHeaderBand();
MyGroupReportHeaderBand_1.Height = 0.5;
MyGroupReportHeaderBand_1.Name = "MyReportGHeaderBand_1";
MyGroupReportHeaderBand_1.KeepGroupHeaderTogether = true;
MyGroupReportHeaderBand_1.KeepGroupTogether = true;
//再有条件就往后接着添加"{tb_B.DepCode}{别的条件}",这是按FormNo分组
MyGroupReportHeaderBand_1.Condition = new Stimulsoft.Report.Components.StiGroupConditionExpression("{tb_B.FormNo}");
page.Components.Add(MyGroupReportHeaderBand_1);
//创建显示数据的部分
StiDataBand MyReportDataBody = new StiDataBand();
MyReportDataBody.DataSourceName = "tb_B";//要跟reageData时起的别字一样
MyReportDataBody.Height = 1;
MyReportDataBody.Name = "MyReportDataBody";
page.Components.Add(MyReportDataBody);
//创建页脚部分
StiGroupFooterBand footerBand = new StiGroupFooterBand();
footerBand.Height = 0.5;
footerBand.Name = "G_FooterBand_1";
page.Components.Add(footerBand);
//往分组头部分添加内容,可用循环
//foreach (DataColumn dataColumn in fcgb.Tables[0].Columns)
StiText headerTextB = new StiText(new RectangleD(pos, 0, columnWidth, 0.5));
headerTextB.Text.Value = dataColumn.Caption;
headerTextB.HorAlignment = StiTextHorAlignment.Center;
headerTextB.Name = "BodyClum" + nameIndex.ToString();
headerTextB.Brush = new StiSolidBrush(Color.LightGreen);
headerTextB.Border.Side = StiBorderSides.All;
MyGroupReportHeaderBand_1.Components.Add(headerTextB);
//往页脚加平均值
//MoneyToUpper是一个金额转人民币大写的函数,网上有
StiText headerTextFooter = new StiText(new RectangleD(pos, 0, columnWidth, 0.5));
headerTextFooter.Text.Value = "平均值: {MoneyToUpper(Round(Avg(tb_B." + ClumName + "),2))}"; ;
headerTextFooter.HorAlignment = StiTextHorAlignment.Center;
headerTextFooter.Name = "BodyOPriceAvg";
headerTextFooter.Brush = new StiSolidBrush(Color.LightGreen);
headerTextFooter.Border.Side = StiBorderSides.All;
footerBand.Components.Add(headerTextFooter);
//这是设置不同行的颜色不同
StiCondition condition = new StiCondition();
condition.BackColor = Color.CornflowerBlue;
condition.TextColor = Color.Black;
condition.Expression = "(Line & 1) == 1";
condition.Item = StiFilterItem.Expression;
//往表体部分写内容,可用循环
StiText dataText = new StiText(new RectangleD(pos, 0, columnWidth, 1));
dataText.Name = "BodyDataText" + nameIndex.ToString();
dataText.Text.Value = "{tb_B." + ClumName + "}";
dataText.Border.Side = StiBorderSides.All;
dataText.WordWrap = true;//自动折行
dataText.CanGrow = true;//适应折行大小
dataText.Conditions.Add(condition);//设置奇偶行颜色
MyReportDataBody.Components.Add(dataText);
五、关于自定义的外部函数
因为我没找到如何使报表调用外部函数,但是他可以再报表中的代码编写页面中自己定义函数。报表保存后用记事本打开,发现时xml格式的,而自己添加的函数脚本是在Script节点下的。
<Script>
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Data;
using Stimulsoft.Controls;
using Stimulsoft.Base.Drawing;
using Stimulsoft.Report;
using Stimulsoft.Report.Dialogs;
using Stimulsoft.Report.Components;
namespace Reports
{
public class Report : Stimulsoft.Report.StiReport
{
public Report() {
this.InitializeComponent();
}
#region StiReport Designer generated code - do not modify
#endregion StiReport Designer generated code - do not modify
}
}
</Script>
所以我通过报表自带的获取script的方法,吧这些脚本取出来保存成字符串,然后定位到#region,在这前面插入要写的函数。
string script = MyReport.Script;
MyReport.Script = script.Insert(script.LastIndexOf("#region"), RFunStr());
MyReport.Dictionary.Synchronize();
MyReport.Save("Report.mrt");
MyReport.Dispose();
MyReport = null;
然后再赋值回去,再保存报表。
六、设计界面
我新建了一个窗体,在里面放了一个Stimulsoft.Report.Design.StiRibbonDesignerControl对象,这个是从工具栏跟拖动按钮似的拖上去的。
控件加载报表对象的方法
this.stiRibbonDesignerControl1.Report = this.MyReport;
MyReport要重新加载报表文件,注册数据集
MyReport = new StiReport();
MyReport.Load("Reportstream.mrt");
MyReport.RegData("tbB", fcgb);
七、预览界面
新建立了一个窗体,里面放了一个预览控件
预览控件预览报表的方法
this.MyReport.Render();
this.stiRibbonViewerControl1.Report = this.MyReport;
同样的,MyReport要重新加载报表文件,注册数据集
八,运行效果
设计
预览
九、拿到没安装报表的电脑上正常运行需要的动态库
添加:
2019/3/12 打印时合并相同值的单元格
StiText dataText = new StiText();
dataText.ProcessingDuplicates = StiProcessingDuplicatesType.Merge;
靠在一起的能合并,不靠在一起的没法合并
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/158805.html原文链接:https://javaforall.cn
相关文章
- [图像]Canny检测的Matlab实现(含代码)「建议收藏」
- FOC和SVPWM的C语言代码实现「建议收藏」
- 一致性哈希算法原理及代码实现「建议收藏」
- Vue(27)vue-codemirror实现在线代码编译器「建议收藏」
- 用腾讯云 AI 录音文件识别,实现短视频字幕批量处理,1行代码搞定语音转文字
- 聊聊损失函数1. 噪声鲁棒损失函数简析 & 代码实现
- 【Windows 逆向】OD 调试器工具 ( 分析 OD 硬件断点处的关键代码 | 添加硬件断点 | 关键代码 | MOV 指令 | EAX 寄存器值分析 | 使用命令查看 esi+0cc 地址 )
- 【视频】CNN(卷积神经网络)模型以及R语言实现回归数据分析|附代码数据
- 模仿Redis队列实现代码设计(仿redis队列代码)
- 代码连接Redis集群一步一步实现(代码连接redis集群)
- 破解Oracle程序的有效武器Oracle代码网站(oracle代码网站)
- php与XML、XSLT、Mysql的结合运用实现代码
- asp.net(C#)防sql注入组件的实现代码
- JS非图片动态loading效果实现代码
- asp.netmvc从数据库中读取图片的实现代码
- JavaScript浏览器验证代码(来自discuz)
- js操作二级联动实现代码
- asp中获取当前页面的地址与参数的函数代码
- jQuery点击后一组图片左右滑动的实现代码
- form表单中去掉默认的enter键提交并绑定js方法实现代码
- jquery+json实现数据列表分页示例代码
- c#集合快速排序类实现代码分享
- Qt实现桌面雪花飘落代码
- asp实现检查ip地址是否为内网或者私有ip地址的代码分享