当前位置:编程学习 > VB >>

{求助}VB中数据传送给EXCEL表格文件导出数据出错

我自己做了一个将VB中数据传送给EXCEL表格  但是表格的第4个字段采集时间每一行的是时间都被写成了 该字段最后一行的时间  本来应该是该字段每行的采集时间都不一样  请问怎样修改程序
接收编号 合格判断 采集日期          采集时间
1 01 2011-9-22  9:02:41
2 02 2011-9-22  9:02:41
3 03 2011-9-22  9:02:41
4 04 2011-9-22  9:02:41
5 05 2011-9-22  9:02:41
1 01 2011-9-22  9:02:41
2 02 2011-9-22  9:02:41
3 03 2011-9-22  9:02:41
4 04 2011-9-22          9:02:41


With Data1.Recordset
      .MoveLast

    If .RecordCount < 1 Then
        MsgBox ("Error   没有记录! ")
        Exit Sub
    End If

    Irowcount = .RecordCount       '记录总数
    Icolcount = .Fields.Count       '字段总数

    ReDim Fieldlen(Icolcount)
    .MoveFirst


    For Irow = 1 To Irowcount + 1
      For Icol = 1 To Icolcount
    Select Case Irow
    Case 1     '在Excel中的第一行加标题
    xlSheet.Cells(Irow, Icol).Value = .Fields(Icol - 1).Name
    Case 2     '将数组FIELDLEN()存为第一条记录的字段长

    If IsNull(.Fields(Icol - 1)) = True Then
        Fieldlen(Icol) = LenB(.Fields(Icol - 1).Name)
           '如果字段值为NULL,则将数组Filelen(Icol)的值设为标题名的宽度
    Else
        Fieldlen(Icol) = LenB(.Fields(Icol - 1))
    End If

    xlSheet.Columns(Icol).ColumnWidth = Fieldlen(Icol)
       'Excel列宽等于字段长
    xlSheet.Cells(Irow, Icol).Value = .Fields(Icol - 1)
       '向Excel的CellS中写入字段值
    Case Else
    Fieldlen(1) = LenB(.Fields(Icol - 1))

    If Fieldlen(Icol) < Fieldlen(1) Then
    xlSheet.Columns(Icol).ColumnWidth = Fieldlen(1)
       '表格列宽等于较长字段长
    Fieldlen(Icol) = Fieldlen(1)
       '数组Fieldlen(Icol)中存放最大字段长度值
    Else
      xlSheet.Columns(Icol).ColumnWidth = Fieldlen(Icol)
    End If

    xlSheet.Cells(Irow, Icol).Value = .Fields(Icol - 1)
    End Select
    Next
    If Irow <> 1 Then
    If Not .EOF Then .MoveNext
    End If
    Next
    With xlSheet
    .Range(.Cells(1, 1), .Cells(1, Icol - 1)).Font.Name = "黑体 "
       '设标题为黑体字
    .Range(.Cells(1, 1), .Cells(1, Icol - 1)).Font.Bold = True
       '标题字体加粗
    .Range(.Cells(1, 1), .Cells(Irow, Icol - 1)).Borders.LineStyle = xlContinuous
       '设表格边框样式
    .Range(.Cells(2, 3), .Cells(Irow - 1, 3)).Cells = Format(xlSheet.Cells((Irow - 1), 3).Value, "yyyy-mm-dd ")
  .Range(.Cells(2, 4), .Cells(Irow - 1, 4)).Cells = Format(xlSheet.Cells((Irow - 1), 4).Value, "h:mm:ss ")
    End With
    xlApp.Visible = True      '显示表格
    xlBook.Save '保存
    Set xlApp = Nothing         '交还控制给Excel
    End With

--------------------编程问答--------------------  .Range(.Cells(2, 3), .Cells(Irow - 1, 3)).Cells = Format(xlSheet.Cells((Irow - 1), 3).Value, "yyyy-mm-dd ")
  .Range(.Cells(2, 4), .Cells(Irow - 1, 4)).Cells = Format(xlSheet.Cells((Irow - 1), 4).Value, "h:mm:ss ")
我感觉是这两句有问题 烦请高手帮忙看看 --------------------编程问答-------------------- 参阅
补充:VB ,  数据库(包含打印,安装,报表)
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,