Sub Initialize
   On Error Goto errmsg
   ''''''''''''''''''''''''''''''''''''''''''''''''
   '程序名:ExportToExcel
   '设计者:wnight88
   '功    能:导出Excel代理
   ''''''''''''''''''''''''''''''''''''''''''''''''
  Print |Content-Type:application/vnd.ms-excel;charset=GBK|
  Print |"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 & "

|+DateStr+|用车情况

"

        htmlstr = htmlstr & ""
        htmlstr = htmlstr+ ""
        htmlstr = htmlstr+ ""
        htmlstr = htmlstr+ ""
        htmlstr = htmlstr & ""
        htmlstr = htmlstr & ""
        htmlstr = htmlstr & ""
        htmlstr = htmlstr & ""
        htmlstr = htmlstr & ""
        htmlstr = htmlstr & ""
        htmlstr = htmlstr & ""    
        htmlstr = htmlstr & ""
        htmlstr = htmlstr & ""    
        htmlstr = htmlstr & ""
        
         For i=1 To dcs.Count
           Set doc = dcs.GetNthDocument(i)
           IfNot doc IsNothingThen    
             If car=doc.car(0) Or car= "*"Then
              tnum=tnum+1
              htmlstr = htmlstr+ ""
              htmlstr = htmlstr+ ""+doc.Fd_Driver(0)+""
              htmlstr = htmlstr+ ""+doc.SelDate(0)+":"+doc.SelMinute(0)+""
              htmlstr = htmlstr+ ""+doc.Fd_Dept(0)+""
              htmlstr = htmlstr+ ""+doc.dest(0)+""
              htmlstr = htmlstr+ ""+doc.car(0)+""
              htmlstr = htmlstr+ ""+doc.StartDate(0)+""
              htmlstr = htmlstr+ ""+doc.EndDate(0)+""    
               Set ritem =doc.getfirstitem( "Body")
               IfNot ritem IsNothingThen
                htmlstr = htmlstr+ ""+ritem.text+""
               Else
                htmlstr = htmlstr+ ""
               EndIf
              htmlstr = htmlstr+ ""
             EndIf
           EndIf
            
         Next
        
        htmlstr = htmlstr+ "
"+DateStr$+"用车情况"+"
司机 时间 部门 目的地 车号 出发时间 预计回站时间 事由
"
       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+ "

"+DateStr$+"用车情况

"

        htmlstr = htmlstr+ ""
        htmlstr = htmlstr+ ""
        htmlstr = htmlstr+ ""
        htmlstr = htmlstr+ ""
        htmlstr = htmlstr+ ""
        htmlstr = htmlstr+ ""
        htmlstr = htmlstr+ ""
        htmlstr = htmlstr+ ""
        htmlstr = htmlstr+ ""
        htmlstr = htmlstr+ ""
        htmlstr = htmlstr+ ""    
        htmlstr = htmlstr+ ""
        htmlstr = htmlstr+ ""        
        htmlstr = htmlstr+ ""
        
         For i=1 To dcs.Count
           Set doc = dcs.GetNthDocument(i)
           IfNot doc IsNothingThen    
             If car=doc.car(0) Or car= "*"Then
              tnum=tnum+1
              htmlstr = htmlstr+ ""
              htmlstr = htmlstr+ ""+doc.Fd_Driver(0)+""
              htmlstr = htmlstr+ ""+doc.SelDate(0)+":"+doc.SelMinute(0)+""
              htmlstr = htmlstr+ ""+doc.Fd_Dept(0)+""
              htmlstr = htmlstr+ ""+doc.dest(0)+""
              htmlstr = htmlstr+ ""+doc.car(0)+""
              htmlstr = htmlstr+ ""+doc.StartDate(0)+""
              htmlstr = htmlstr+ ""+doc.EndDate(0)+""    
               Set ritem =doc.getfirstitem( "Body")
               IfNot ritem IsNothingThen
                htmlstr = htmlstr+ ""+ritem.text+""
               Else
                htmlstr = htmlstr+ ""
               EndIf
              htmlstr = htmlstr+ ""
             EndIf
           EndIf
            
         Next
        htmlstr = htmlstr+ "
"+DateStr$+"用车情况"+"
司机 时间 部门 目的地 车号 出发时间 预计回站时间 事由
"
       End If
        
       Call stDate.AdjustDay(1)
        
     Wend
   End If
    
   'Print "
"
   If tnum=0 Then
    htmlstr = htmlstr+ "


未找到用车记录
"

   End If
    
  Print htmlstr
errmsg:
   If Cstr(Erl) = "0" Then
    Msgbox "代理程序成功执行完毕!"
   Else
    Msgbox "代理程序出错..出错行数为:" & Cstr(Erl) & "行!错误原因为:" & Error
   End If
End Sub