代码,每次重新生成
Dim cidx As Integer = 0
Dim ridx As Integer = 0
Dim dt As DataTable = DataTables("表B")
Dim t As Table = Tables("表B")
dt.DataRows.Clear
Dim fr As Row
Dim cname(1) As String
Dim dta As DataTable = DataTables("表A")
Dim sum1, sum2 As Integer
For Each qy As String In dta.GetValues("区域")
Dim qys As List(Of String) = dta.GetValues("企业", "区域 = '" & qy & "'")
If cidx = 0 Then
cname(0) = "区域"
cname(1) = "销售金额"
fr = t.AddNew
fr(cname(0)) = qy
dt.AddNew(qys.Count+1)
ridx = fr.index + qys.Count
Else If cidx = 1 Then
cname(0) = "区域1"
cname(1) = "销售金额1"
fr(cname(0)) = qy
If fr.Index + qys.Count > ridx Then
dt.AddNew(fr.Index + qys.Count - ridx)
ridx = fr.Index + qys.Count
End If
End If
For i As Integer = 0 To qys.Count - 1
Dim r As Row = t.Rows(fr.Index+i+1)
r(cname(0)) = qys(i)
r(cname(1)) = dta.Compute("sum(销售金额)", "区域 = '" & qy & "' and 企业 = '" & qys(i) & "'")
Next
t.Rows(ridx+1)(cname(0)) = "小计"
t.Rows(ridx+1)(cname(1)) = dta.Compute("sum(销售金额)", "区域 = '" & qy & "'")
If cidx = 0 Then
sum1 += val(t.Rows(ridx+1)(cname(1)))
cidx = 1
Else If cidx = 1 Then
sum2 += val(t.Rows(ridx+1)(cname(1)))
cidx = 0
End If
Next
Dim zjr As Row = t.AddNew
zjr("区域") = "总计"
zjr("销售金额") = sum1
zjr("区域1") = "总计"
zjr("销售金额1") = sum2