大家都知道Notes的导入导出工具很不好用,还不支持Excel,最要命的是Office 2007也不支持Lotus 1-2-3格式,怎么办尼?分享一个我自己开发的小工具,VBScript很不好写啊,费了不小功夫。如果你看得懂的话这些代码够你自己做一个了,不会的话可以留言,我发给你。
效果图:1,如何在Notes上添加本工具的按钮;2,工具截图;3,Excel模板。
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <!-- Version 0.1 beta By Kent Xu [email protected] 12/03/04
- Finished importing function
- Version 0.1.1 beta By Kent Xu [email protected] 12/12/06
- 1, Finished exporting function;
- 2, Adjusted the UI;
- 3, Support reader & author for importing;
- -->
- <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
- <hta:application id=batchimport applicationName=Batch Import scroll=no
- singleinstance=yes maximizeButton=no selection=no border=dialog
- icon=notes.exe>
- <title>batchinout</title>
- <script type="text/VBscript">
- Sub InitDocument()
- Call window.resizeTo(400, 340)
- End Sub
- Sub Import()
- Dim nWS
- Dim exlApp
- Dim ExlFile
- Dim ExlSheet
- Dim exlWKBook
- Dim nUIDB
- Dim nDB
- Dim nDoc
- Dim formValue
- Dim i
- Dim j
- Dim k
- Dim fieldNmStr
- Dim fieldTypeStr
- Dim cellNmStr
- Dim cellTypeStr
- Dim field
- Dim fieldNmAry
- Dim fieldTypeAry
- Dim fieldVal
- ExlFile = Trim(document.getElementById("ExcelFile").value)
- ExlSheet = Trim(document.getElementById("ExcelSheet").value)
- If ExlFile = "" Then
- Call Msgbox("Please select the Excel File!", 0, "Warning")
- Elseif ExlSheet = "" Then
- Call Msgbox("Please input the Excel Sheet!" , 0, "Warning")
- Else
- Set nWS = CreateObject("Notes.NotesUIWorkspace")
- Set nUIDB = nWS.CurrentDatabase
- If nUIDB Is Nothing Then
- Call Msgbox("Please open the database that you want to import to!", 0, "Warning")
- Else
- Set nDB = nUIDB.Database
- If Msgbox("Are you sure to import the file " & Chr(10) & ExlFile & Chr(10) & "to " & Chr(10) & nDB.Title & Chr(10) & nDB.Server & "\\" & nDB.FilePath & "?", 4, "Confirm") =6 Then
- Set exlApp = CreateObject("Excel.Application")
- exlApp.application.visible = False
- Set exlWKBook = exlApp.Workbooks.open(ExlFile)
- Set exlSheet = exlApp.Worksheets(ExlSheet)
- formValue = Trim(exlSheet.Cells(1, 2).value)
- i = 1
- fieldNmStr = ""
- fieldTypeStr = ""
- cellNmStr = Trim(exlSheet.Cells(3, i).value)
- cellTypeStr = LCase(Trim(exlSheet.Cells(4, i).value))
- While cellNmStr <> ""
- If fieldNmStr = "" Then
- fieldNmStr = cellNmStr
- Else
- fieldNmStrfieldNmStr = fieldNmStr & "," & cellNmStr
- End If
- If fieldTypeStr = "" Then
- fieldTypeStr = cellTypeStr
- Else
- fieldTypeStrfieldTypeStr = fieldTypeStr & "," & cellTypeStr
- End If
- ii = i + 1
- cellNmStr = Trim(exlSheet.Cells(3, i).value)
- cellTypeStr = LCase(Trim(exlSheet.Cells(4, i).value))
- Wend
- fieldNmAry = Split(fieldNmStr, ",")
- fieldTypeAry = Split(fieldTypeStr, ",")
- j = 5
- cellStr = Trim(exlSheet.Cells(j, 1).value)
- While cellStr <> ""
- Set nDoc = nDB.CreateDocument()
- Call nDoc.ReplaceItemValue("Form", formValue)
- For k = 1 to i - 1 Step 1
- cellStr = Trim(exlSheet.Cells(j, k).value)
- If cellStr = "" Then
- fieldVal = ""
- Else
- Select Case fieldTypeAry(k -1)
- Case "string"
- fieldVal = cellStr
- Case "integer"
- fieldVal = CLng(cellStr)
- Case "float"
- fieldVal = CDbl(cellStr)
- Case "datetime"
- fieldVal = CDate(cellStr)
- Case "reader"
- fieldVal = TrimArray(Split(cellStr, ";"))
- Case "author"
- fieldVal = TrimArray(Split(cellStr, ";"))
- Case Else
- fieldVal = cellStr
- End Select
- End If
- Set field = nDoc.ReplaceItemValue(fieldNmAry(k - 1), fieldVal)
- If fieldTypeAry(k -1) = "reader" Then
- field.IsReaders = True
- End If
- If fieldTypeAry(k -1) = "author" Then
- field.IsAuthors = True
- End If
- Next
- Call nDoc.Save(True, False)
- jj = j + 1
- cellStr = Trim(exlSheet.Cells(j, 1).value)
- Wend
- If j - 5 > 0 Then
- Call Msgbox("Imported " & CStr((j - 5)) & " records successfully!", 0, "Message")
- Else
- Call Msgbox("None record was imported!", 0, "Message")
- End If
- Call exlApp.Quit()
- End If
- End If
- End If
- End Sub
- Function TrimArray(fromArray)
- For i = 0 To UBound(fromArray)
- fromArray(i) = Trim(fromArray(i))
- Next
- TrimArray = fromArray
- End Function
- Sub Export()
- Dim nWS
- Dim nUIView
- Dim nView
- Dim nViewCols
- Dim nDocCOl
- Dim SelAll
- Dim exlApp
- Dim exlsheet
- Dim rows
- Dim cols
- Dim maxcols
- Dim ExlFile
- Dim nDB
- Dim session
- Dim sDB
- Dim sDoc
- Dim var
- Set nWS = CreateObject("Notes.NotesUIWorkspace")
- Set nUIView = nWS.CurrentView
- If nUIView Is Nothing Then
- Call Msgbox("Please open the view that you want to export from!", 0, "Warning")
- Else
- Set nView = nUIView.View
- ExlFile = nWS.SaveFileDialog(False, "Save as...", "Excel 2007|xlsx|Excel 2003|*xls", "C:\", "Notes Export.xlsx")
- If Not IsArray(ExlFile) Then
- Exit Sub
- End If
- rows = 1
- cols = 1
- maxcols = nView.ColumnCount
- Set exlApp = CreateObject("Excel.Application")
- exlApp.StatusBar = "Creating WorkSheet. Please be patient..."
- exlApp.Visible = False
- Call exlApp.Workbooks.Add()
- exlApp.ReferenceStyle = 2
- Set exlsheet = exlApp.Workbooks(1).Worksheets(1)
- exlsheet.Name = "Export From Notes"
- exlApp.StatusBar = "Creating Column Heading. Please be patient..."
- nViewnViewCols = nView.Columns
- For x = 0 To maxcols - 1 Step 1
- exlsheet.Cells(rows, cols).Value = nViewCols(x).Title
- colscols = cols + 1
- Next
- If document.getElementsByName("ExpAll")(0).checked Then
- SelAll = True
- Set nDoc = nView.GetFirstDocument()
- Else
- SelAll = False
- Set nDocCol = nUIView.Documents
- Set nDoc = nDocCol.GetFirstDocument()
- End If
- cols = 1
- rows = 2
- exlApp.StatusBar = "Importing Notes Data - Document "
- While Not nDoc Is Nothing
- For x = 0 To maxcols - 1 Step 1
- If nViewCols(x).isField Then
- Set var = nDoc.GetFirstItem(nViewCols(x).ItemName)
- If Not var Is Nothing Then
- exlsheet.Cells(rows, cols).Value = var.Text
- End If
- ElseIf nViewCols(x).isFormula Then
- If nViewCols(x).Formula = "@IsExpandable" Then
- exlsheet.Cells(rows, cols).Value = ""
- Else
- If IsEmpty(session) Then
- Set session = CreateObject("Lotus.NotesSession")
- Call session.Initialize()
- Set nDB = nDoc.ParentDatabase
- Set sDB = session.GetDatabase(nDB.Server, nDB.Filepath)
- End If
- Set sDoc = sDB.GetDocumentByUNID(nDoc.UniversalID)
- exlsheet.Cells(rows, cols).Value = Join(session.Evaluate("@Text(" & nViewCols(x).Formula & ")", sDoc), ", ")
- End If
- End If
- colscols = cols + 1
- Next
- rowsrows = rows + 1
- cols = 1
- If SelAll Then
- Set nDoc = nView.GetNextDocument(nDoc)
- Else
- Set nDoc = nDocCol.GetNextDocument(nDoc)
- End If
- Wend
- Call exlApp.Rows("1:1").Select()
- exlApp.Selection.Font.Bold = True
- exlApp.Selection.Font.Underline = True
- Call exlApp.Range(exlSheet.Cells(1,1), exlSheet.Cells(rows, maxcols)).Select()
- exlApp.Selection.Font.Name = "Arial"
- exlApp.Selection.Font.Size = 9
- Call exlApp.Selection.Columns.AutoFit()
- exlApp.WorkSheets(1).PageSetup.Orientation = 2
- exlApp.WorkSheets(1).PageSetup.centerheader = "Report - Confidential"
- exlApp.WorkSheets(1).Pagesetup.RightFooter = "Page &P" & Chr(13) & "Date: &D"
- exlApp.WorkSheets(1).Pagesetup.CenterFooter = ""
- exlApp.ReferenceStyle = 1
- Call exlApp.Range("A1").Select()
- exlApp.StatusBar = "Importing Data from Lotus Notes Application was Completed."
- Call exlapp.Activeworkbook.SaveAs(ExlFile)
- Call exlapp.Quit()
- Call Msgbox("Exported " & cstr(rows - 2) & " records", 0, "Message")
- End If
- End Sub
- </script>
- <style type="text/css">
- body {
- font-family: Arial, "Trebuchet MS", Helvetica, sans-serif;
- font-size: 0.9em;
- padding: 0px;
- }
- .comtable {
- width: 100%;
- margin-top: 5px;
- border-collapse: collapse;
- }
- .comtable td {
- font-size: 0.9em;
- border: 1px solid #98bf21;
- padding: 3px 7px 3px 7px;
- }
- .comtable th {
- font-size: 1em;
- text-align: left;
- padding: 5px 7px 5px 7px;
- background-color: #A7C942;
- color: #ffffff;
- }
- </style>
- </head>
- <body onload="vbscript: InitDocument()">
- <form onsubmit="return false;"< /span>>
- <table class="comtable">
- <tr>
- <th align="left" colspan="2">Importing</th>
- </tr>
- <tr>
- <td width="25%">Select Excel</td><td><input type="file" id="ExcelFile" /></td>
- </tr>
- <tr>
- <td>Excel sheet</td><td><input type="text" id="ExcelSheet" value="Sheet1" /></td>
- </tr>
- <tr>
- <td align="right" colspan="2"><button onclick="vbscript: Import()">Import</button></td>
- </tr>
- </table>
- <div style="display:block">
- <table class="comtable">
- <tr>
- <th align="left" colspan="2">Exporting</th>
- </tr>
- <tr>
- <td width="30%">Export Option</td><td><input type="radio" name="ExpAll" value="1"/>All
- <input type="radio" name="ExpAll" value="0" checked/>Selected</td>
- </tr>
- <tr>
- <td align="right" colspan="2"><button onclick="vbscript: Export()">Export</button></td>
- </tr>
- </table>
- </div>
- <table class="comtable">
- <tr>
- <td>Notes tools for importing & exporting via Excel
- <br />Author: Kent Xu, version: 0.1.1 beta</td>
- </tr>
- </table>
- </form>
- </body>
- </html>
本文出自 “Kent_Xu” 博客,转载请与作者联系!