2010-10-25 11 views
1

J'ai une vue qui affiche 9 lignes d'information pour chaque document. Dans cette vue, j'ai la fonctionnalité Exporter vers Excel en utilisant le code ci-dessous pour exporter le document vers Excel. Les données ne s'exportent pas correctement pour les deux premiers documents, par exemple si j'ai 7 lignes pour le premier document, alors il devrait exporter 7 lignes mais exporter 2 lignes seulement. Il se passe pour les 2 premiers documents seulement, à partir du 3ème document indépendamment de toute ligne d'information qu'il exporte pour exceller parfaitement. J'ai essayé de modifier le code de la ligne% de la ligne% = row% + 2 à la ligne% = row% + 3, 4 ou 5, mais il n'est pas nécessaire de créer des lignes dans la feuille Excel et cela semble étrange. Toute idée de ce que je devrais faire pour que les lignes augmentent dynamiquement.Exporter vers Excel, Lotus note domino

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 

Répondre

1

Je vois que vous utilisez l'automatisation Excel. L'automatisation Excel est parfois lourde.

Je voudrais essayer NPOI pour les fichiers Excel XLS. Jetez un coup d'oeil. Vraiment simple de travailler avec:

Create Excel (.XLS and .XLSX) file from C#

0

Euh, ce code doit certainement être plus facile à lire, je parie qu'il ya une façon plus simple de faire ce que vous voulez.
OK, pouvez-vous expliquer à quoi sert la vue "CRMOpenIssue"? Je suggère que vous oubliez le nombre de lignes que chaque document est représenté dans votre vue et utilisez les champs de document comme source de données, au lieu des données affichées directement dans les colonnes de la vue.

+1

J'applaudis ce que vous essayez de faire, mais ce serait mieux comme un commentaire, pas une réponse. –

1

Il y a quelque chose de très mauvais avec le code que vous avez téléchargé. Vous devez avoir supprimé ou ajouté une boucle If car la première boucle If se ferme avant de fermer la boucle While qu'il contient. Cela étant dit, cela devrait fonctionner, même si je ne l'ai pas testé.

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 
Questions connexes