1

我有一个视图,每个文档显示 9 行信息。在此视图中,我具有使用以下代码将文档导出到 Excel 的导出到 Excel 功能。前两个文档的数据没有正确导出,例如,如果我的第一个文档有 7 行,那么它应该导出 7 行,但它只导出 2 行。它仅发生在前 2 个文档中,从第 3 个文档开始,与它完美导出到 excel 的任何行号信息无关。我尝试将 row% 的代码从 row% = row%+2 修改为 row% = row%+3 、4 或 5,但它不必要地在 excel 表中创建行,它不是动态行,而且看起来也很奇怪。知道我应该怎么做才能使行动态增加。

Sub Initialize
 'On Error Goto errhandler
 On Error Resume Next
 Dim session As New NotesSession
 Dim db As NotesDatabase
 Dim doccoll As NotesDocumentCollection
 Dim view As NotesView 
 Dim doc As NotesDocument
 Dim otherdoc As NotesDocument

 Set db = session.CurrentDatabase 
 Set view = db.GetView("CRMOpenIssue") 
 Set doccoll=db.UnprocessedDocuments

 Set oExcel = CreateObject ( "Excel.Application" )
 Set oWorkbook = oExcel.Workbooks.Add
 Set oWorkSheet= oWorkbook.Sheets ( 1 )


 oWorkSheet.Cells(1,1).value="Quote# "
 oWorkSheet.Cells(1,2).value="Quote Line#" 
 oWorkSheet.Cells(1,3).value="Customer - fab"
 oWorkSheet.Cells(1,4).value="OppNum"
 oWorkSheet.Cells(1,5).value="OppLine#" 
 oWorkSheet.Cells(1,6).value="Open Issue#"
 oWorkSheet.Cells(1,7).value="Open Issue"
 oWorkSheet.Cells(1,8).value="Category"
 oWorkSheet.Cells(1,9).value="Due date"
 oWorkSheet.Cells(1,10).value="Owner to resolve issue"
 oWorkSheet.Cells(1,11).value="Owner/PME Verify when closed"
 oExcel.Worksheets(1).Range("A1:K1").Font.Bold = True

 oExcel.columns("A:A").ColumnWidth=15.00
 oExcel.columns("B:B").ColumnWidth=8.00
 oExcel.columns("C:C").ColumnWidth=15.00
 oExcel.columns("D:D").ColumnWidth=10.00
 oExcel.columns("E:E").ColumnWidth=8.00
 oExcel.columns("F:F").ColumnWidth=8.00 
 oExcel.columns("G:G").ColumnWidth=30.00
 oExcel.columns("H:H").ColumnWidth=30.00
 oExcel.columns("I:I").ColumnWidth=15.00
 oExcel.columns("J:J").ColumnWidth=15.00
 oExcel.columns("K:K").ColumnWidth=30.00

 row% = 1
 offset% = 0
 lastOffset% = 0 

 If doccoll.count >1 Then 'if more than one doc selected then confirm 
  resp = Messagebox("Do you want to export only the " & _
  "selected " & doccoll.count & " documents?", 36, "Selected only?" )
 Else
  Messagebox "Exporting all rows. (To export only selected " & _
  "rows tick those required in the left margin first.)"
 End If  '6= yes 

 oExcel.visible=True

 If resp=6 Then 'selected documents
  Set doc = doccoll.GetFirstDocument   
  While Not doc Is Nothing
   If resp=6 Then  
    row% = row%+2
    col% = 0 'Reset the Columns
    Set otherdoc = view.getnextdocument(doc)
    If otherdoc Is Nothing Then
     Set otherdoc = view.getprevdocument(doc)
     If otherdoc Is Nothing Then
      Print " >1 doc should be selected"
      End
     Else
      Set otherdoc = view.getnextdocument(otherdoc)
     End If
    Else 'got next doc
     Set otherdoc = view.getprevdocument(otherdoc)
    End If        
   End If
   Forall colval In otherdoc.ColumnValues
    col% = col% + 1
    If Isarray(colval) Then
     columnVal=Fulltrim(colval)
     For y = 0 To Ubound(columnVal)
      offset% = row% + y +lastOffset%   
      oWorkSheet.Cells(offset%,col%).value = columnVal(y) 
     Next
    Else
     oWorkSheet.Cells(row%, col%).value = colval  
    End If  

   End Forall
   Set doc = doccoll.GetNextDocument(doc)       
  Wend
 Else 'all documents
  Set otherdoc =view.GetFirstDocument  
  While Not otherdoc Is Nothing
   row% = row% + 2
   col% = 0 'Reset the Columns
  'Loop through all the column entries
  'Forall colval In entry.ColumnValues
   Forall colval In otherdoc.ColumnValues
    col% = col% + 1
    If Isarray(colval) Then
     columnVal=Fulltrim(colval)
     For y = 0 To Ubound(columnVal)
      offset% = row% + y +lastOffset%   
      oWorkSheet.Cells(offset%,col%).value = columnVal(y) 
     Next
    Else
     oWorkSheet.Cells(row%, col%).value = colval  
    End If         
   End Forall
   row%=offset%
   Set otherdoc=view.GetNextDocument(otherdoc)
  Wend
 End If
'errhandler:
 Call oExcel.quit()    
 Set oWorkSheet= Nothing
 Set oWorkbook = Nothing
 Set oExcel = Nothing
 Print "Done"
End Sub 
4

3 回答 3

1

您上传的代码有问题。您必须删除或添加了一个 If 循环,因为第一个 If 循环在您关闭它包含的 While 循环之前关闭。话虽如此,这应该可以工作,尽管我还没有测试过。

Option Public
Option Declare

Sub Initialize
    Dim session As New NotesSession
    Dim db As NotesDatabase
    Dim doccoll As NotesDocumentCollection
    Dim view As NotesView 
    Dim doc As NotesDocument
    Dim resp As Integer, row As Integer, offset As Integer, nextrow As Integer, col As Integer
    Dim oExcel As Variant
    Dim oWorkbook As Variant
    Dim oWorkSheet As Variant

    On Error GoTo olecleanup

    Set db = session.CurrentDatabase 
    Set view = db.GetView("CRMOpenIssue") 
    Set doccoll=db.UnprocessedDocuments

    Set oExcel = CreateObject ( "Excel.Application" )
    Set oWorkbook = oExcel.Workbooks.Add
    Set oWorkSheet = oWorkbook.Sheets ( 1 )

    oWorkSheet.Cells(1,1).value="Quote# "
    oWorkSheet.Cells(1,2).value="Quote Line#" 
    oWorkSheet.Cells(1,3).value="Customer - fab"
    oWorkSheet.Cells(1,4).value="OppNum"
    oWorkSheet.Cells(1,5).value="OppLine#" 
    oWorkSheet.Cells(1,6).value="Open Issue#"
    oWorkSheet.Cells(1,7).value="Open Issue"
    oWorkSheet.Cells(1,8).value="Category"
    oWorkSheet.Cells(1,9).value="Due date"
    oWorkSheet.Cells(1,10).value="Owner to resolve issue"
    oWorkSheet.Cells(1,11).value="Owner/PME Verify when closed"
    oExcel.Worksheets(1).Range("A1:K1").Font.Bold = True

    oExcel.columns("A:A").ColumnWidth=15.00
    oExcel.columns("B:B").ColumnWidth=8.00
    oExcel.columns("C:C").ColumnWidth=15.00
    oExcel.columns("D:D").ColumnWidth=10.00
    oExcel.columns("E:E").ColumnWidth=8.00
    oExcel.columns("F:F").ColumnWidth=8.00 
    oExcel.columns("G:G").ColumnWidth=30.00
    oExcel.columns("H:H").ColumnWidth=30.00
    oExcel.columns("I:I").ColumnWidth=15.00
    oExcel.columns("J:J").ColumnWidth=15.00
    oExcel.columns("K:K").ColumnWidth=30.00

    offset% = 0
    nextrow% = 3

    If doccoll.count >1 Then 'if more than one doc selected then confirm 
        resp = MessageBox("Do you want to export only the " & _
        "selected " & doccoll.count & " documents?", 36, "Selected only?" )
    Else
        MessageBox "Exporting all rows. (To export only selected " & _
        "rows tick those required in the left margin first.)"
    End If  '6= yes 

    oExcel.visible=True

    If resp=6 Then 'selected documents
        Set doc = doccoll.GetFirstDocument
        If doccoll.count = 1 Then
            Print " >1 doc should be selected"
        End If
    Else
        Set doc =view.GetFirstDocument
    End if

    While Not doc Is Nothing
        row% = nextrow%
        col% = 0 'Reset the Columns
        nextrow% = row% + 1

        ForAll colval In doc.ColumnValues
            col% = col% + 1
            If IsArray(colval) Then
                offset% = row%
                ForAll cv In colval
                    If CStr(cv) <> "" Then
                        oWorkSheet.Cells(offset%,col%).value = cv
                        offset% = offset% + 1
                    End If
                End ForAll
                If nextrow% < offset% Then nextrow% = offset% 
            Else
                oWorkSheet.Cells(row%, col%).value = colval  
            End If
        End ForAll

        If resp=6 Then 'selected documents
            Set doc = doccoll.Getnextdocument(doc)
        Else
            Set doc =view.Getnextdocument(doc)
        End If
    Wend

    oExcel.activeworkbook.close
    oExcel.quit
    Set oExcel = Nothing

Finish :
    Print "Done"
    Exit Sub

olecleanup :
'   Call LogError() 'Enable to use OpenLog
    If Not(IsEmpty(oExcel)) Then
        oExcel.activeworkbook.close
        oExcel.quit
        Set oExcel = Nothing
    End If
    Resume Finish
End Sub
于 2011-07-27T14:07:29.100 回答
1

我看到您正在使用 Excel 自动化。Excel自动化有时很麻烦。

我会为 Excel XLS 文件尝试 NPOI。看看它。使用起来非常简单:

从 C# 创建 Excel(.XLS 和 .XLSX)文件

于 2010-10-25T14:47:37.547 回答
0

嗯,这段代码肯定需要更具可读性,我敢打赌有一种更简单的方法可以做你想做的事。
好的,你能解释一下你使用“CRMOpenIssue”视图做什么吗?

我建议您忘记每个文档在视图中表示的行数,并使用文档字段作为数据源,而不是直接显示在视图列中的数据。

于 2011-03-05T18:12:12.827 回答