原来导出在excel下显示的格式如下:
序号     工号      姓名      工序
1        001      陈三      切大段
2        001      陈三      切小段
3        002      李四      切大段
4        003      张五      切大段
现在想改成这样(sql应该不用改的):
序号     工号      姓名      工序
1        001      陈三      切大段
         001      陈三      切小段
2        002      李四      切大段
3        003      张五      切大段因为陈三是同一个人,序号是代表人数。我的程序如下:
Set xlapp = CreateObject("Excel.Application") '创建EXCEL应用类
xlapp.Visible = True '设置EXCEL可见
Set xlbook = xlapp.Workbooks.Open("D:\xls\tua.xls") '打开EXCEL工作簿
Set xlsheet = xlbook.Worksheets(1) '打开EXCEL工作表
xlsheet.Activate '激活工作表  i = 4 '输出数据从第5行开始
    rs.MoveFirst
    Do Until rs.EOF
        i = i + 1
                         
  xlsheet.Cells(i, 1) = i - 4     '人数序号(通过判断记录来增加相关的序号,要修改)
  xlsheet.Cells(i, 2) = rs![nb]   '员工卡号
  xlsheet.Cells(i, 3) = rs![ne]   '员工姓名
  xlsheet.Cells(i, 4) = rs![ay]   '生产工序     If (i - 4) Mod 33 = 0 And i <> 4 Then  '每33条记录为一页
         xlsheet.Range(xlsheet.Cells(i, 1), xlsheet.Cells(i, rs.Fields.Count)).Select
                                    '选中当前添加的最后一行,这是关键
         SendKeys "^{end}"           '向应用程序EXCEL发送一个组合键CTRL+END,使光标移到文件尾
         xlsheet.HPageBreaks.Add Before:=xlapp.ActiveCell   '添加一个分页符  '添加一个分页符
     End If
        
        rs.MoveNext
    Loop
xlapp.Worksheets.PrintPreview
xlapp.DisplayAlerts = False
xlapp.Quit
Set xlapp = Nothing
cn.Close各位,我没什么头绪,帮忙一下,谢谢。。

解决方案 »

  1.   

    这个好办呀.
    定义多两个变量 ii和iii,然后用ii填写人数.dim ii as long
    dim iii as long
    ii=1
    Set xlapp = CreateObject("Excel.Application") '创建EXCEL应用类
    xlapp.Visible = True '设置EXCEL可见
    Set xlbook = xlapp.Workbooks.Open("D:\xls\tua.xls") '打开EXCEL工作簿
    Set xlsheet = xlbook.Worksheets(1) '打开EXCEL工作表
    xlsheet.Activate '激活工作表  i = 4 '输出数据从第5行开始
        rs.MoveFirst
        Do Until rs.EOF
            i = i + 1
                             
      xlsheet.Cells(i, 2) = rs![nb]   '员工卡号
      if val(rs![nb])=iii then
          
      else
          xlsheet.Cells(i, 1) =ii
          ii=ii+1
      end if
      iii=val(rs![nb])
      
      xlsheet.Cells(i, 3) = rs![ne]   '员工姓名
      xlsheet.Cells(i, 4) = rs![ay]   '生产工序     If (i - 4) Mod 33 = 0 And i <> 4 Then  '每33条记录为一页
             xlsheet.Range(xlsheet.Cells(i, 1), xlsheet.Cells(i, rs.Fields.Count)).Select
                                        '选中当前添加的最后一行,这是关键
             SendKeys "^{end}"           '向应用程序EXCEL发送一个组合键CTRL+END,使光标移到文件尾
             xlsheet.HPageBreaks.Add Before:=xlapp.ActiveCell   '添加一个分页符  '添加一个分页符
         End If
            
            rs.MoveNext
        Loop
    xlapp.Worksheets.PrintPreview
    xlapp.DisplayAlerts = False
    xlapp.Quit
    Set xlapp = Nothing
    cn.Close
      

  2.   

    你SQL语言分组不行吗?那样取出来的数据就正常了
      

  3.   

    谢谢你们,午夜逛街,这是什么意思?
    if val(rs![nb])=iii then
      

  4.   

    strsql = "select nb,ne,ay,sum(tnb) as tnb,sum(anb) as anb,sum(bnb) as bnb,sum(bnb) as anb,sum(ha) as ha,(select ha from wk where ay=tmp.ay) as hap,sum(hb) as hb,(select hbp from tuwg where nb=tmp.nb) as hbp,sum(hc) as hc,(select hcp from tuwg where nb=tmp.nb) as hcp,sum(hc) as he,(select hcp from tuwg where nb=tmp.nb) as hepp,sum(hd) as hd,(select hdp from tuwg where nb=tmp.nb) as hdp,sum(hf) as hf,(select hfp from tuwg where nb=tmp.nb) as hfp,sum(hg) as hg,(select hgp from tuwg where nb=tmp.nb) as hgp,sum(hh) as hh,sum(hi) as hi,(select iv from tuwg where nb=tmp.nb) as iv,(select price from wk where ay=tmp.ay) as price,(select pnb from wk where ay=tmp.ay) as pnb,(select tx from wk where ay=tmp.ay) as tx From(select * from 前序 Union All select * from 中序 Union All select * from 后序 Union All select * from 包装 Union All select * from 盖帽 Union All select * from 其它)tmp where da >='" & da1 & "' And da < ='" & da2 & "'" & vc & vd & "group by nb,ne,ay"
    这就是查询语句,好乱,我不想再改了。
    午夜逛街,发的程序,不是很清楚。。
    if val(rs![nb])=iii then