Notes导入导出通用工具

   大家都知道Notes的导入导出工具很不好用,还不支持Excel,最要命的是Office 2007也不支持Lotus 1-2-3格式,怎么办尼?分享一个我自己开发的小工具,VBScript很不好写啊,费了不小功夫。如果你看得懂的话这些代码够你自己做一个了,不会的话可以留言,我发给你。

 效果图:1,如何在Notes上添加本工具的按钮;2,工具截图;3,Excel模板。

 

 

  
  
  
  
  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  3. <html xmlns="http://www.w3.org/1999/xhtml"> 
  4. <head> 
  5.  
  6. <!-- Version 0.1 beta By Kent Xu [email protected] 12/03/04 
  7. Finished importing function 
  8. Version 0.1.1 beta By Kent Xu [email protected] 12/12/06 
  9. 1, Finished exporting function; 
  10. 2, Adjusted the UI; 
  11. 3, Support reader & author for importing; 
  12. --> 
  13.  
  14. <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> 
  15. <hta:application id=batchimport applicationName=Batch Import scroll=no 
  16. singleinstance=yes maximizeButton=no selection=no border=dialog 
  17. icon=notes.exe> 
  18. <title>batchinout</title> 
  19. <script type="text/VBscript"> 
  20. Sub InitDocument() 
  21.         Call window.resizeTo(400, 340) 
  22. End Sub 
  23. Sub Import() 
  24.     Dim nWS 
  25.     Dim exlApp 
  26.     Dim ExlFile 
  27.     Dim ExlSheet 
  28.     Dim exlWKBook 
  29.     Dim nUIDB 
  30.     Dim nDB 
  31.     Dim nDoc 
  32.     Dim formValue 
  33.     Dim i 
  34.     Dim j 
  35.     Dim k 
  36.     Dim fieldNmStr 
  37.     Dim fieldTypeStr 
  38.     Dim cellNmStr 
  39.     Dim cellTypeStr 
  40.     Dim field 
  41.     Dim fieldNmAry 
  42.     Dim fieldTypeAry 
  43.     Dim fieldVal 
  44.      
  45.     ExlFile = Trim(document.getElementById("ExcelFile").value) 
  46.     ExlSheet = Trim(document.getElementById("ExcelSheet").value) 
  47.      
  48.     If ExlFile = "" Then 
  49.         Call Msgbox("Please select the Excel File!", 0, "Warning") 
  50.     Elseif ExlSheet = "" Then 
  51.         Call Msgbox("Please input the Excel Sheet!" , 0, "Warning") 
  52.     Else 
  53.         Set nWS = CreateObject("Notes.NotesUIWorkspace") 
  54.         Set nUIDB = nWS.CurrentDatabase 
  55.         If nUIDB Is Nothing Then 
  56.             Call Msgbox("Please open the database that you want to import to!", 0, "Warning") 
  57.         Else 
  58.             Set nDB = nUIDB.Database 
  59.             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 
  60.             Set exlApp = CreateObject("Excel.Application") 
  61.             exlApp.application.visible = False 
  62.             Set exlWKBook = exlApp.Workbooks.open(ExlFile) 
  63.             Set exlSheet = exlApp.Worksheets(ExlSheet) 
  64.                              
  65.             formValue = Trim(exlSheet.Cells(1, 2).value) 
  66.             i = 1 
  67.             fieldNmStr = "" 
  68.             fieldTypeStr = "" 
  69.             cellNmStr = Trim(exlSheet.Cells(3, i).value) 
  70.             cellTypeStr = LCase(Trim(exlSheet.Cells(4, i).value)) 
  71.  
  72.             While cellNmStr <> "" 
  73.                 If fieldNmStr = "" Then 
  74.                     fieldNmStr = cellNmStr 
  75.                 Else 
  76.                     fieldNmStrfieldNmStr = fieldNmStr & "," & cellNmStr 
  77.                 End If                                                 
  78.                 If fieldTypeStr = "" Then 
  79.                     fieldTypeStr = cellTypeStr 
  80.                 Else 
  81.                     fieldTypeStrfieldTypeStr = fieldTypeStr & "," & cellTypeStr 
  82.                 End If 
  83.                 ii = i + 1 
  84.                 cellNmStr = Trim(exlSheet.Cells(3, i).value) 
  85.                 cellTypeStr = LCase(Trim(exlSheet.Cells(4, i).value)) 
  86.             Wend 
  87.              
  88.             fieldNmAry = Split(fieldNmStr, ",") 
  89.             fieldTypeAry = Split(fieldTypeStr, ",") 
  90.                              
  91.             j = 5 
  92.             cellStr = Trim(exlSheet.Cells(j, 1).value) 
  93.             While cellStr <> "" 
  94.                 Set nDoc = nDB.CreateDocument() 
  95.                 Call nDoc.ReplaceItemValue("Form", formValue) 
  96.                 For k = 1 to i - 1 Step 1 
  97.                     cellStr = Trim(exlSheet.Cells(j, k).value) 
  98.                     If cellStr = "" Then 
  99.                         fieldVal = "" 
  100.                     Else 
  101.                         Select Case fieldTypeAry(k -1) 
  102.                         Case "string" 
  103.                             fieldVal = cellStr 
  104.                         Case "integer" 
  105.                             fieldVal = CLng(cellStr) 
  106.                         Case "float" 
  107.                             fieldVal = CDbl(cellStr) 
  108.                         Case "datetime" 
  109.                             fieldVal = CDate(cellStr) 
  110.                         Case "reader" 
  111.                             fieldVal = TrimArray(Split(cellStr, ";")) 
  112.                         Case "author" 
  113.                             fieldVal = TrimArray(Split(cellStr, ";")) 
  114.                         Case Else 
  115.                             fieldVal = cellStr 
  116.                         End Select 
  117.                     End If 
  118.                     Set field = nDoc.ReplaceItemValue(fieldNmAry(k - 1), fieldVal) 
  119.                     If fieldTypeAry(k -1) = "reader" Then 
  120.                         field.IsReaders = True 
  121.                     End If 
  122.                     If fieldTypeAry(k -1) = "author" Then 
  123.                         field.IsAuthors = True 
  124.                     End If 
  125.                 Next 
  126.                 Call nDoc.Save(True, False) 
  127.                 jj = j + 1 
  128.                 cellStr = Trim(exlSheet.Cells(j, 1).value) 
  129.             Wend 
  130.             If j - 5 > 0 Then 
  131.                 Call Msgbox("Imported " & CStr((j - 5)) & " records successfully!", 0, "Message") 
  132.             Else 
  133.                 Call Msgbox("None record was imported!", 0, "Message") 
  134.             End If 
  135.                 Call exlApp.Quit() 
  136.             End If 
  137.         End If 
  138.     End If         
  139. End Sub 
  140.  
  141. Function TrimArray(fromArray) 
  142.     For i = 0 To UBound(fromArray) 
  143.         fromArray(i) = Trim(fromArray(i)) 
  144.     Next 
  145.     TrimArray = fromArray 
  146. End Function 
  147.  
  148. Sub Export() 
  149.     Dim nWS 
  150.     Dim nUIView 
  151.     Dim nView 
  152.     Dim nViewCols 
  153.     Dim nDocCOl 
  154.     Dim SelAll 
  155.     Dim exlApp 
  156.     Dim exlsheet 
  157.     Dim rows 
  158.     Dim cols 
  159.     Dim maxcols 
  160.     Dim ExlFile 
  161.     Dim nDB 
  162.     Dim session 
  163.     Dim sDB 
  164.     Dim sDoc 
  165.     Dim var 
  166.      
  167.     Set nWS = CreateObject("Notes.NotesUIWorkspace") 
  168.     Set nUIView = nWS.CurrentView 
  169.     If nUIView Is Nothing Then 
  170.         Call Msgbox("Please open the view that you want to export from!", 0, "Warning") 
  171.     Else 
  172.         Set nView = nUIView.View 
  173.         ExlFile = nWS.SaveFileDialog(False, "Save as...", "Excel 2007|xlsx|Excel 2003|*xls", "C:\", "Notes Export.xlsx") 
  174.         If Not IsArray(ExlFile) Then 
  175.             Exit Sub 
  176.         End If 
  177.          
  178.         rows = 1 
  179.         cols = 1 
  180.         maxcols = nView.ColumnCount 
  181.          
  182.         Set exlApp = CreateObject("Excel.Application") 
  183.         exlApp.StatusBar = "Creating WorkSheet. Please be patient..." 
  184.         exlApp.Visible = False 
  185.         Call exlApp.Workbooks.Add() 
  186.         exlApp.ReferenceStyle = 2 
  187.         Set exlsheet = exlApp.Workbooks(1).Worksheets(1) 
  188.         exlsheet.Name = "Export From Notes" 
  189.         exlApp.StatusBar = "Creating Column Heading. Please be patient..." 
  190.         nViewnViewCols = nView.Columns 
  191.         For x = 0 To maxcols - 1 Step 1 
  192.             exlsheet.Cells(rows, cols).Value = nViewCols(x).Title 
  193.             colscols = cols + 1 
  194.         Next 
  195.          
  196.         If document.getElementsByName("ExpAll")(0).checked Then 
  197.             SelAll = True 
  198.             Set nDoc = nView.GetFirstDocument() 
  199.         Else 
  200.             SelAll = False 
  201.             Set nDocCol = nUIView.Documents 
  202.             Set nDoc = nDocCol.GetFirstDocument() 
  203.         End If 
  204.          
  205.         cols = 1 
  206.         rows = 2 
  207.         exlApp.StatusBar = "Importing Notes Data - Document "  
  208.         While Not nDoc Is Nothing 
  209.             For x = 0 To maxcols - 1 Step 1 
  210.                 If nViewCols(x).isField Then 
  211.                     Set var = nDoc.GetFirstItem(nViewCols(x).ItemName) 
  212.                     If Not var Is Nothing Then 
  213.                         exlsheet.Cells(rows, cols).Value = var.Text 
  214.                     End If 
  215.                 ElseIf nViewCols(x).isFormula Then 
  216.                     If nViewCols(x).Formula = "@IsExpandable" Then 
  217.                         exlsheet.Cells(rows, cols).Value = "" 
  218.                     Else 
  219.                         If IsEmpty(session) Then 
  220.                             Set session = CreateObject("Lotus.NotesSession") 
  221.                             Call session.Initialize() 
  222.                             Set nDB = nDoc.ParentDatabase 
  223.                             Set sDB = session.GetDatabase(nDB.Server, nDB.Filepath) 
  224.                         End If 
  225.                         Set sDoc = sDB.GetDocumentByUNID(nDoc.UniversalID) 
  226.                         exlsheet.Cells(rows, cols).Value = Join(session.Evaluate("@Text(" & nViewCols(x).Formula & ")", sDoc), ", ") 
  227.                     End If 
  228.                 End If 
  229.                 colscols = cols + 1 
  230.             Next 
  231.             rowsrows = rows + 1 
  232.             cols = 1 
  233.             If SelAll Then 
  234.                 Set nDoc = nView.GetNextDocument(nDoc) 
  235.             Else 
  236.                 Set nDoc = nDocCol.GetNextDocument(nDoc) 
  237.             End If 
  238.         Wend 
  239.          
  240.         Call exlApp.Rows("1:1").Select() 
  241.         exlApp.Selection.Font.Bold = True 
  242.         exlApp.Selection.Font.Underline = True 
  243.         Call exlApp.Range(exlSheet.Cells(1,1), exlSheet.Cells(rows, maxcols)).Select() 
  244.         exlApp.Selection.Font.Name = "Arial" 
  245.         exlApp.Selection.Font.Size = 9 
  246.         Call exlApp.Selection.Columns.AutoFit() 
  247.         exlApp.WorkSheets(1).PageSetup.Orientation = 2 
  248.         exlApp.WorkSheets(1).PageSetup.centerheader = "Report - Confidential" 
  249.         exlApp.WorkSheets(1).Pagesetup.RightFooter = "Page &P" & Chr(13) & "Date: &D" 
  250.         exlApp.WorkSheets(1).Pagesetup.CenterFooter = "" 
  251.         exlApp.ReferenceStyle = 1 
  252.         Call exlApp.Range("A1").Select() 
  253.         exlApp.StatusBar = "Importing Data from Lotus Notes Application was Completed." 
  254.         Call exlapp.Activeworkbook.SaveAs(ExlFile) 
  255.         Call exlapp.Quit() 
  256.         Call Msgbox("Exported " & cstr(rows - 2) & " records", 0, "Message") 
  257.     End If 
  258. End Sub 
  259. </script> 
  260. <style type="text/css"> 
  261. body { 
  262.     font-family: Arial, "Trebuchet MS", Helvetica, sans-serif; 
  263.     font-size: 0.9em; 
  264.     padding: 0px; 
  265.  
  266. .comtable { 
  267.     width: 100%; 
  268.     margin-top: 5px; 
  269.     border-collapse: collapse; 
  270.  
  271. .comtable td { 
  272.     font-size: 0.9em; 
  273.     border: 1px solid #98bf21; 
  274.     padding: 3px 7px 3px 7px; 
  275.  
  276. .comtable th { 
  277.     font-size: 1em; 
  278.     text-align: left; 
  279.     padding: 5px 7px 5px 7px; 
  280.     background-color: #A7C942; 
  281.     color: #ffffff; 
  282. </style> 
  283. </head> 
  284.  
  285. <body onload="vbscript: InitDocument()"> 
  286.  
  287. <form onsubmit="return false;"< /span>> 
  288.     <table class="comtable"> 
  289.         <tr> 
  290.             <th align="left" colspan="2">Importing</th> 
  291.         </tr> 
  292.         <tr> 
  293.             <td width="25%">Select Excel</td><td><input type="file" id="ExcelFile" /></td> 
  294.         </tr> 
  295.         <tr> 
  296.             <td>Excel sheet</td><td><input type="text" id="ExcelSheet" value="Sheet1" /></td> 
  297.         </tr> 
  298.         <tr> 
  299.             <td align="right" colspan="2"><button onclick="vbscript: Import()">Import</button></td> 
  300.         </tr> 
  301.     </table> 
  302.     <div style="display:block"> 
  303.     <table class="comtable"> 
  304.         <tr> 
  305.             <th align="left" colspan="2">Exporting</th> 
  306.         </tr> 
  307.         <tr> 
  308.             <td width="30%">Export Option</td><td><input type="radio" name="ExpAll" value="1"/>All 
  309.             <input type="radio" name="ExpAll" value="0" checked/>Selected</td> 
  310.         </tr> 
  311.         <tr> 
  312.             <td align="right" colspan="2"><button onclick="vbscript: Export()">Export</button></td> 
  313.         </tr> 
  314.     </table> 
  315.     </div> 
  316.     <table class="comtable"> 
  317.         <tr> 
  318.             <td>Notes tools for importing & exporting via Excel 
  319.             <br />Author: Kent Xu, version: 0.1.1 beta</td> 
  320.         </tr> 
  321.     </table> 
  322. </form> 
  323. </body> 
  324. </html> 

 

本文出自 “Kent_Xu” 博客,转载请与作者联系!

你可能感兴趣的:(导出,导入,notes)