Crystal Report字符串分割:按指定标签将内容分行并添加对应标识
Crystal Report字符串分割与格式化处理方案
需求说明
待处理字符串:100 Pcs / CASE@@5 CASE / 1 PLT@@Total 500 Pcs / PLT
期望输出格式:
Box Qty= 100 Pcs / CASE
Qty/PLT= 5 CASE / 1 PLT
Pcs/PLT= 500 Pcs / PLT
实现步骤
- 创建公式字段
在Crystal Report中新建一个公式字段(可命名为@FormattedQtyText),使用Crystal Syntax编写以下代码:
// 按分隔符@@拆分原始字符串 Local StringVar Array splitParts := Split({你的字段名}, "@@"); Local StringVar finalText := ""; // 处理第一段,添加Box Qty标识 If UBound(splitParts) >= 1 Then finalText := finalText & "Box Qty= " & splitParts[1] & Chr(13); // 处理第二段,添加Qty/PLT标识 If UBound(splitParts) >= 2 Then finalText := finalText & "Qty/PLT= " & splitParts[2] & Chr(13); // 处理第三段,移除Total前缀后添加Pcs/PLT标识 If UBound(splitParts) >= 3 Then Local StringVar cleanThirdPart := Replace(splitParts[3], "Total ", ""); finalText := finalText & "Pcs/PLT= " & cleanThirdPart; finalText
注意:将代码中的{你的字段名}替换为报表中存储目标字符串的实际字段名
- 调整字段显示属性
将公式字段拖入报表布局后,右键选择Format Field,在Common标签页勾选Can Grow,确保多行内容能自动展开完整显示。
关键逻辑说明
Split函数:按指定分隔符@@将原始字符串拆分为字符串数组Chr(13):Crystal Report中的换行符,实现内容分行展示Replace函数:移除第三段文本中的"Total "前缀,匹配期望的输出格式
内容的提问来源于stack exchange,提问作者David
相关产品推荐
相关产品推荐

