以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.net/bbs/index.asp)
--  专家坐堂  (http://foxtable.net/bbs/list.asp?boardid=2)
----  [求助] 单元格合并和汇总  (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=179278)

--  作者:riverzhang163
--  发布时间:2022/8/16 11:29:00
--  [求助] 单元格合并和汇总
想达到附件文件的效果,客人列合并,第二级款号列合并,每个款号做相应的数量统计

语句如下,但是无法达到效果,请专家指正,谢谢!

Dim doc As New PrintDoc

Dim rt As New prt.RenderTable

Dim rx As New prt.RenderText \'定义一个文本对象

rt.Width = "Auto" \'表格宽度为自动,也就是等于各列设置宽度之和

Doc.PageSetting.LeftMargin = 8 \'设置左边距

Doc.PageSetting.RightMargin = 5 \'设置右边距

Doc.PageSetting.TopMargin = 10 \'设置上边距

Doc.PageSetting.BottomMargin = 10 \'设置下边距

rx.text = "未满箱明细"

rx.Style.FontBold = True \'字体加粗

rx.Style.FontSize = 16 \'大体大小为16

rx.Style.TextAlignHorz = prt.AlignHorzEnum.Center \'水平居中排列

rx.Style.Spacing.Bottom = 3 \'和下面的对象(表格)距离3毫米

doc.Body.Children.Add(rx) \'加入到报表中

Dim tb As Table = Tables("未满箱明细_table1")

Dim ColNames As String() = New String(){"客人代码","款号","前幅","后幅","客人订单号","客人交期","数量","已完成数量"}

Dim MergeCols As Integer = 4 \'指定要合并的列数

tb.Sort = "客人代码,款号,前幅,后幅,客人交期" \'根据合并列排序

rt.Width = "Auto"

rt.SplitHorzBehavior = prt.SplitBehaviorEnum.SplitIfNeeded

rt.Style.Font = tb.Font

rt.Style.TextAlignVert = prt.AlignVertEnum.Center

rt.RepeatGridLinesVert = True \'换页后重复表格线

For c As Integer = 0 To ColNames.Length - 1

    Dim lr As Integer \' 用于保存合并区域的起始行

    rt.Cells(0,c).Text = ColNames(c)

    rt.Cells(0,c).Style.TextAlignHorz = prt.AlignHorzEnum.Center

    rt.Cols(c).Width = tb.Cols(ColNames(c)).PrintWidth

    If tb.Cols(ColNames(c)).IsNumeric OrElse tb.Cols(ColNames(c)).IsDate Then

        rt.Cols(c).Style.TextAlignHorz = prt.AlignHorzEnum.Right

    End If

    For r As Integer = 0 To tb.Rows.Count - 1

        If c <= MergeCols - 1 Then \'如果是要合并的列

            Dim Merge As Boolean = True

            If r = 0 Then

                Merge = False

            Else

                For n As Integer = 0 To c

                    If tb.Rows(r)(ColNames(n)) <> tb.Rows(r - 1)(ColNames(n))

                        Merge = False

                        Exit For

                    End If

                Next

            End If

            If Merge Then

                rt.Cells(lr,c).SpanRows = rt.Cells(lr,c).SpanRows + 1

            Else

                rt.Cells(r + 1, c).Text = tb.Rows(r)(ColNames(c))

                Dim rw As Row = tb.Rows(r)

                rt.Cells(r+1,2).Image = GetImage(rw("前幅"))

                rt.Cells(r+1,3).Image = GetImage(rw("后幅"))

                rt.Cells(r + 1, c).VertSplitBehavior = prt.CellSplitBehaviorEnum.Copy \'换页后重复单元格

                lr = r + 1

            End If

        Else

            rt.Cells(r + 1, c).Text = tb.Rows(r)(ColNames(c))

        End If

    Next

Next

rt.Style.Gridlines.All = New prt.Linedef(Color.Gray)

rt.CellStyle.Spacing.All = 0.5

rt.Rows(0).Style.TextAlignHorz = prt.AlignHorzEnum.Center

rt.RowGroups(0,1).Header = prt.TableHeaderEnum.All

Dim drs As List(Of DataRow) \'定义一个行集合

Dim cp As List(Of String) = tb.DataTable.GetValues("款号",tb.Filter) \'以集合形式,从指定列中获取并返回不重复的值

Dim kh As List(Of String) = tb.DataTable.GetValues("客人代码",tb.Filter) \'以集合形式,从指定列中获取并返回不重复的值

For p As Integer = 0 To math.Ceiling(tb.Rows.Count) - 1

    Dim cnt As Integer

    rt.Width = "Auto" \'表格宽度为自动,也就是等于各列设置宽度之和

    rt.SplitHorzBehavior = prt.SplitBehaviorEnum.SplitIfNeeded \'表格宽度超出页宽时,可以水平换页

    rt.Style.GridLines.All = New prt.LineDef(0.3,Color.LightGray) \'线宽0.3mm,深灰色网格线

    rt.CellStyle.Spacing.All = 1 \'单元格内距设为1毫米

    rt.Style.Font = tb.Font \'字体为数据表字体

    For c As Integer = 0 To tb.Cols.Count - 1 \'生成列标题

        rt.Cells(0,c).Text = tb.Cols(c).Name \'用绑定表的列名作为表格列标题文本

        rt.Cols(c).Width = tb.Cols(c).PrintWidth \'数据表列宽为表格列宽

    Next

    cp = tb.DataTable.GetValues("款号",tb.Filter) \'返回产品不重复的值

    For Each pd As String In cp \'分产品打印 msgbox(cp)

        kh = tb.DataTable.GetValues("客人代码",tb.Filter And " 款号 = \'" & pd & "\'")

        For Each pdd As String In kh \'分客户打印

            Dim Filter = ""

            If tb.Filter > "" Then

                Filter = "款号 = \'" & pd & "\' and 客人代码 = \'" & pdd & "\' And " & tb.Filter

            Else

                Filter = "款号 = \'" & pd & "\' and 客人代码 = \'" & pdd & "\'"

            End If

            dim c1 as integer = rt.Rows.Count

For Each dr As DataRow In drs \'逐行打印此产品的订单

   cnt = rt.Rows.Count

        For c As Integer = 0 To tbl.Cols.Count - 1

            rt.Cells(cnt, c).Text = dr(tbl.Cols(c).Name)

        Next

    Next

rt.Cells(c1,0).SpanRows = drs.count

    cnt = rt.Rows.Count

    rt.Cells(cnt, 0).Text = pdd & "合计 "  \'打印产品的小计

    rt.Cells(cnt, 0).style.TextAlignHorz = prt.AlignHorzEnum.right \'水平居中排列

    rt.Cells(cnt, 0).Style.FontBold = True \'字体加粗

    rt.Cells(cnt, 0).Style.FontSize = 10.5 \'大体大小为16

    rt.Cells(cnt, 2).Style.FontBold = True \'字体加粗

    rt.Cells(cnt, 2).Style.FontSize = 10.5 \'大体大小为16

    rt.Cells(cnt, 3).Style.FontBold = True \'字体加粗

    rt.Cells(cnt, 3).Style.FontSize = 10.5 \'大体大小为16

    rt.Cells(cnt,0).SpanCols = 2 \'5行第2个单元格向右合并3(用于显示地址)

    rt.Cells(cnt,2).Text = tbl.compute("Sum(数量)", "客人代码 = \'" & pd & "\'") & ""

 Next

Next

Next

Next

doc.Body.Children.Add(rt)

doc.preview()

       

 


此主题相关图片如下:微信图片_20220816110529.png
按此在新窗口浏览图片

[此贴子已经被作者于2022/8/16 11:30:41编辑过]

--  作者:有点蓝
--  发布时间:2022/8/16 11:31:00
--  
错误的效果是什么?正确的效果是什么?
--  作者:riverzhang163
--  发布时间:2022/8/16 11:44:00
--  
错误是显示不出来,无法预览,正确的效果应当是上图附件的效果


--  作者:有点蓝
--  发布时间:2022/8/16 11:49:00
--  
无法预览提示什么错误?
--  作者:riverzhang163
--  发布时间:2022/8/16 11:55:00
--  
会变化,现在显示的是 “从字符串“满箱 = false”到类型“Long”的转换无效。”,我是把语句设在按钮里面的,但是这句我没写过呀

“.NET Framework 版本:2.0.50727.8806
Foxtable 版本:2018.10.9.1
错误所在事件:窗口,未满箱明细,生成报表,Click
详细错误信息:
从字符串“满箱 = false”到类型“Long”的转换无效。
输入字符串的格式不正确。


--  作者:有点蓝
--  发布时间:2022/8/16 11:59:00
--  
调试技巧:http://www.foxtable.com/webhelp/scr/1485.htm,看哪一句代码出错
--  作者:riverzhang163
--  发布时间:2022/8/16 15:17:00
--  

帮忙看一下这段有什么问题,运行到这里就不行了。


cp = tb.DataTable.GetValues("款号",tb.Filter) \'返回产品不重复的值

    For Each pd As String In cp \'分产品打印 msgbox(cp)

        kh = tb.DataTable.GetValues("客人代码",tb.Filter And " 款号 = \'" & pd & "\'")

        For Each pdd As String In kh \'分客户打印

            Dim Filter = ""

            If tb.Filter > "" Then

                Filter = "款号 = \'" & pd & "\' and 客人代码 = \'" & pdd & "\' And " & tb.Filter

            Else

                Filter = "款号 = \'" & pd & "\' and 客人代码 = \'" & pdd & "\'"

            End If

            dim c1 as integer = rt.Rows.Count

For Each dr As DataRow In drs \'逐行打印此产品的订单

   cnt = rt.Rows.Count

        For c As Integer = 0 To tbl.Cols.Count - 1

            rt.Cells(cnt, c).Text = dr(tbl.Cols(c).Name)

        Next

    Next


--  作者:有点蓝
--  发布时间:2022/8/16 15:49:00
--  
kh = tb.DataTable.GetValues("客人代码",tb.Filter & " And  款号 = \'" & pd & "\'")
--  作者:riverzhang163
--  发布时间:2022/8/16 16:39:00
--  
图片点击可在新窗口打开查看太牛了,确实是这句有问题,但是运行后显示 ”And" 运算符前缺少操作数,请再帮一下忙,这一块我真的不会写,谢谢!
--  作者:有点蓝
--  发布时间:2022/8/16 17:09:00
--  
应该是tb.Filter没有设置条件,判断一下如果tb.Filter没有条件就不要合并到一起