Sub Initialize
  On Error Goto errmsg
  ''''''''''''''''''''''''''''''''''''''''''''''''
  '程序名:ExportToExcel
  '设计者:wnight88
  '功    能:导出Excel代理
  ''''''''''''''''''''''''''''''''''''''''''''''''
  Print |Content-Type:application/vnd.ms-excel;charset=GBK|
  Print |<meta http-equiv="Content-Type" content="text/html; charset=GBK">|
  Dim s As New notessession
  Dim doc As NotesDocument
  Dim contextDoc As NotesDocument
  Dim db As NotesDatabase
  Dim dcs As NotesDocumentCollection
    
  Dim dateTime As New NotesDateTime(2000-01-01)
  Dim nextDateTime As NotesDateTime
  Dim stDate As NotesDateTime
  Dim edDate As NotesDateTime
  Dim dtDate As notesdatetime
  Dim ritem As notesitem
  Dim car As String
  Dim tnum As Integer
  tnum=0
    
  Dim searchformula As String
  Dim view As NotesView
  Dim htmlstr As String
    
  Set db = s.CurrentDatabase
  Set contextDoc=s.documentcontext
  car=contextDoc.car(0)
  Set view=db.GetView("viewDocByTime")
    
  Set stDate=New notesdatetime(contextDoc.startDate(0))
  Set edDate=New notesdatetime(contextDoc.endDate(0))
    
    
  If Cstr(stDate.DateOnly)="" And Cstr(edDate.DateOnly)="" Then
    Dim result As Variant
    Dim j As Integer
    Dim DateStr As String    
    Dim MStr As String    
    Dim DStr As String    
    result = Evaluate("@DbColumn('': '' ;'': @Subset(@DbName;-1); 'viewDocByTime' ; 1)", doc)
    
    For j = 0 To Ubound(result)
      Set dcs=view.GetAllDocumentsByKey(result(j),True)
      If dcs.count>0 Then
        
        MStr=Cstr(Format$(result(j),"mm"))
        
        DStr=Cstr(Format$(result(j),"dd"))
        
        'DateStr=Cstr(Cint(MStr))+"月"+Cstr(Cint(DStr))+"日"
        DateStr=Cstr(Format$(result(j),"yyyy年mm月dd日"))
        'htmlstr = htmlstr & "<style>TD{FONT-FAMILY: 宋体; FONT-SIZE: 10pt;}</style><center><h2><b>|+DateStr+|用车情况</b><h2></center>"
        htmlstr = htmlstr & "<table border=1>"
        htmlstr = htmlstr+ "<tr>"
        htmlstr = htmlstr+ "<td colspan='8' align='center'><b>"+DateStr$+"用车情况"+"</b></td>"
        htmlstr = htmlstr+ "</tr>"
        htmlstr = htmlstr & "<tr>"
        htmlstr = htmlstr & "<td width='8%'    height='19'    align='center' bgcolor='#ffffff'><b>司机</b></td>"
        htmlstr = htmlstr & "<td width='8%'    height='19'    align='center' bgcolor='#ffffff'><b>时间</b></td>"
        htmlstr = htmlstr & "<td width='15%'    height='19'    align='center' bgcolor='#ffffff'><b>部门</b></td>"
        htmlstr = htmlstr & "<td width='15%'    height='19'    align='center' bgcolor='#ffffff'><b>目的地</b></td>"
        htmlstr = htmlstr & "<td width='10%'    height='19'    align='center' bgcolor='#ffffff'><b>车号</b></td>"
        htmlstr = htmlstr & "<td width='15%'    height='19'    align='center' bgcolor='#ffffff'><b>出发时间</b></td>"    
        htmlstr = htmlstr & "<td width='15%'    height='19'    align='center' bgcolor='#ffffff'><b>预计回站时间</b></td>"
        htmlstr = htmlstr & "<td width='30%'    height='19'    align='center' bgcolor='#ffffff'><b>事由</b></td>"    
        htmlstr = htmlstr & "</tr>"
        
        For i=1 To dcs.Count
          Set doc = dcs.GetNthDocument(i)
          If Not doc Is Nothing Then    
            If car=doc.car(0) Or car="*" Then
              tnum=tnum+1
              htmlstr = htmlstr+ "<tr>"
              htmlstr = htmlstr+ "<td    height='19'    align='center' bgcolor='#ffffff'>"+doc.Fd_Driver(0)+"</td>"
              htmlstr = htmlstr+ "<td    height='19'    align='center' bgcolor='#ffffff'>"+doc.SelDate(0)+":"+doc.SelMinute(0)+"</td>"
              htmlstr = htmlstr+ "<td    height='19'    align='center' bgcolor='#ffffff'>"+doc.Fd_Dept(0)+"</td>"
              htmlstr = htmlstr+ "<td    height='19'    align='center' bgcolor='#ffffff'>"+doc.dest(0)+"</td>"
              htmlstr = htmlstr+ "<td    height='19'    align='center' bgcolor='#ffffff'>"+doc.car(0)+"</td>"
              htmlstr = htmlstr+ "<td    height='19'    align='center' bgcolor='#ffffff'>"+doc.StartDate(0)+"</td>"
              htmlstr = htmlstr+ "<td    height='19'    align='center' bgcolor='#ffffff'>"+doc.EndDate(0)+"</td>"    
              Set ritem =doc.getfirstitem("Body")
              If Not ritem Is Nothing Then
                htmlstr = htmlstr+ "<td    height='19'    align='center' bgcolor='#ffffff'>"+ritem.text+"</td>"
              Else
                htmlstr = htmlstr+ "<td    height='19'    align='center' bgcolor='#ffffff'></td>"
              End If
              htmlstr = htmlstr+ "</tr>"
            End If
          End If
            
        Next
        
        htmlstr = htmlstr+ "</table>"
      End If
    Next
  Else    
    While Not (Cstr(Format$(stDate.LocalTime,"yyyy-mm-dd"))>Cstr(Format$(edDate.LocalTime,"yyyy-mm-dd")))
        
      Set dcs=view.GetAllDocumentsByKey(Cstr(Format$(stDate.LocalTime,"yyyy-mm-dd")),True)
      If dcs.count>0 Then
        MStr$=Cstr(Format$(stDate.LocalTime,"mm"))
        
        DStr$=Cstr(Format$(stDate.LocalTime,"dd"))
        
        'DateStr$=Cstr(Cint(MStr$))+"月"+Cstr(Cint(DStr$))+"日"
        
        DateStr=Cstr(Format$(stDate.LocalTime,"yyyy年mm月dd日"))
        
        'htmlstr = htmlstr+ "<style>TD{FONT-FAMILY: 宋体; FONT-SIZE: 10pt;}</style><center><h2><b>"+DateStr$+"用车情况</b><h2></center>"
        htmlstr = htmlstr+ "<table border=1>"
        htmlstr = htmlstr+ "<tr>"
        htmlstr = htmlstr+ "<td colspan='8' align='center'><b>"+DateStr$+"用车情况"+"</b></td>"
        htmlstr = htmlstr+ "</tr>"
        htmlstr = htmlstr+ "<tr>"
        htmlstr = htmlstr+ "<td width='8%'    height='19'    align='center' bgcolor='#ffffff'><b>司机</b></td>"
        htmlstr = htmlstr+ "<td width='8%'    height='19'    align='center' bgcolor='#ffffff'><b>时间</b></td>"
        htmlstr = htmlstr+ "<td width='15%'    height='19'    align='center' bgcolor='#ffffff'><b>部门</b></td>"
        htmlstr = htmlstr+ "<td width='15%'    height='19'    align='center' bgcolor='#ffffff'><b>目的地</b></td>"
        htmlstr = htmlstr+ "<td width='10%'    height='19'    align='center' bgcolor='#ffffff'><b>车号</b></td>"
        htmlstr = htmlstr+ "<td width='15%'    height='19'    align='center' bgcolor='#ffffff'><b>出发时间</b></td>"    
        htmlstr = htmlstr+ "<td width='15%'    height='19'    align='center' bgcolor='#ffffff'><b>预计回站时间</b></td>"
        htmlstr = htmlstr+ "<td width='30%'    height='19'    align='center' bgcolor='#ffffff'><b>事由</b></td>"        
        htmlstr = htmlstr+ "</tr>"
        
        For i=1 To dcs.Count
          Set doc = dcs.GetNthDocument(i)
          If Not doc Is Nothing Then    
            If car=doc.car(0) Or car="*" Then
              tnum=tnum+1
              htmlstr = htmlstr+ "<tr>"
              htmlstr = htmlstr+ "<td    height='19'    align='center' bgcolor='#ffffff'>"+doc.Fd_Driver(0)+"</td>"
              htmlstr = htmlstr+ "<td    height='19'    align='center' bgcolor='#ffffff'>"+doc.SelDate(0)+":"+doc.SelMinute(0)+"</td>"
              htmlstr = htmlstr+ "<td    height='19'    align='center' bgcolor='#ffffff'>"+doc.Fd_Dept(0)+"</td>"
              htmlstr = htmlstr+ "<td    height='19'    align='center' bgcolor='#ffffff'>"+doc.dest(0)+"</td>"
              htmlstr = htmlstr+ "<td    height='19'    align='center' bgcolor='#ffffff'>"+doc.car(0)+"</td>"
              htmlstr = htmlstr+ "<td    height='19'    align='center' bgcolor='#ffffff'>"+doc.StartDate(0)+"</td>"
              htmlstr = htmlstr+ "<td    height='19'    align='center' bgcolor='#ffffff'>"+doc.EndDate(0)+"</td>"    
              Set ritem =doc.getfirstitem("Body")
              If Not ritem Is Nothing Then
                htmlstr = htmlstr+ "<td    height='19'    align='center' bgcolor='#ffffff'>"+ritem.text+"</td>"
              Else
                htmlstr = htmlstr+ "<td    height='19'    align='center' bgcolor='#ffffff'></td>"
              End If
              htmlstr = htmlstr+ "</tr>"
            End If
          End If
            
        Next
        htmlstr = htmlstr+ "</table>"
      End If
        
      Call stDate.AdjustDay(1)
        
    Wend
  End If
    
  'Print "</div>"
  If tnum=0 Then
    htmlstr = htmlstr+ "<center><br><br><font size=3>未找到用车记录</font></center>"
  End If
    
  Print htmlstr
errmsg:
  If Cstr(Erl) = "0" Then
    Msgbox "代理程序成功执行完毕!"
  Else
    Msgbox "代理程序出错..出错行数为:" & Cstr(Erl) & "行!错误原因为:" & Error
  End If
End Sub