PDA

View Full Version : Notes与外部应用程序如Excel、Word如何进行数据交换?


xcheny007
20-02-06, 08:12 PM
Notes与外部应用程序如Excel、Word如何进行数据交换? 等待中……

空空空
20-02-06, 10:24 PM
给你网络上的资料:
1.notes--->excel:++++++++++++++++++++++

Sub Click(Source As Button)
Dim session As New NotesSession
Dim db As NotesDatabase
Dim view As Notesview
Dim doc As Notesdocument.nbsp
Dim excelapplication As Variant
Dim excelworkbook As Variant
Dim excelsheet As Variant
Dim i As Integer
Dim uvcols As Integer
Dim selection As Variant
'path=session.GetEnvironmentString ("D:",True)
'gzpath=path+"\"+"test.xls"
Set excelapplication=CreateObject("Excel.Application")
excelapplication.statusbar="正在创建工作表,请稍等....."
excelapplication.Visible=True
'==================
'excelapplication.excel.open(gzpath)
excelapplication.Workbooks.Add
excelapplication.referencestyle=2
Set excelsheet=excelapplication.Workbooks(1).worksheets(1)
excelsheet.name="notes export"
Dim rows As Integer
Dim cols As Integer
Dim maxcols As Integer
Dim fieldname As String
Dim fitem As NotesItem
rows=1
cols=1
Set db=session.CurrentDatabase
Set view=db.GetView ("注册表视图")
uvcols=Ubound(view.Columns)
For x=0 To Ubound(view.Columns)
excelapplication.statusbar="正在创建单元格,请稍等....."
If view.Columns(x).IsHidden=False Then
If view.Columns(x).title<>"" Then
'excelsheet.Cells(1,1).value="姓名"
'excelsheet.Cells(1,2).value="年龄"
excelsheet.Cells(rows,cols).value=view.Columns(x).Title
cols=cols+1
End If
End If
Next
maxcols=cols-1
Set doc=view.GetFirstdocument.nbsp
rows=2
cols=1
While Not(doc Is Nothing)
For x=0 To Ubound(view.Columns)
excelapplication.statusbar="正在从Notes中引入数据,请稍等....."
If view.Columns(x).IsHidden=False Then
If view.Columns(x).title<>"" Then
fieldname=view.Columns(x).itemname
Set fitem=doc.GetFirstItem(fieldname)
excelsheet.Cells(rows,cols).value=fitem.Text
cols=cols+1
End If
End If
Next
rows=rows+1
cols=1
Set doc=view.GetNextdocument.nbsp(doc)
Wend
With excelapplication.worksheets(1)
.pagesetup.orientation=2
.pagesetup.centerheader="report_confidential"
.pagesetup.rightfooter="page &P"&Chr$(13) &"Date:&D"
.pagesetup.CenterFooter=""
End With
excelapplication.referencestyle=1
excelapplication.range("A1").Select
excelapplication.statusbar="数据导入完成。"
excelsheet.PageSetup.PrintGridlines=True
'excelworkbook.printout
'excelworkbook.SaveAs("d:\test.xls")
'excelworkbook.Save
excelapplication.Quit
Set excelapplication=Nothing
End Sub


2.excel-->notes++++++++++++++++++++++

Sub Click(Source As Button)
Dim workspace As New NotesUIWorkspace
Dim session As New NotesSession
Dim db As NotesDatabase
Dim view As NotesView
Dim item As NotesItem
Dim files As Variant
Dim schar As String
Dim excelApplication As Variant
Dim excelWorkbook As Variant
Dim excelsheet As Variant
Dim i As Integer
Set db = session.CurrentDatabase
Set view = db.GetView( "ExcelRegister" )
files=workspace.OpenFileDialog (False,"选择引入数据文件","Excel file|*.xls","c:")
If files(0)="" Then
Exit Sub
Else
Set excelApplication=CreateObject("Excel.Application")
Set excelWorkbook =excelApplication.Workbooks.Open(files)
Set excelsheet=excelWorkbook.Worksheets(1)
i=2
stemp=excelSheet.Cells(i,1).value
Do Until Cstr(stemp)=""
Set cpdoc=New Notesdocument.db)
cpdoc.form="Excel_notes注册表"
cpdoc.lx=excelsheet.Cells(i,1).value
stemp=excelSheet.Cells(i,1).value
stemp2=excelSheet.Cells(i,2).value
cpdoc.NameExcel=stemp
cpdoc.AgeExcel=stemp2
i=i+1
Call cpdoc.save(True,False)
Call workspace.ViewRefresh
Loop
excelWorkbook.close(False)
excelApplication.Quit
Set excelApplication=Nothing
End If
End Sub

空空空
20-02-06, 10:25 PM
Notes与Office的接口
---- 本文介绍了Notes 与 Office的三种接口程序的编制,它们可实现将Notes数据引入Office程序中,实现在Notes中较难实现的标签打印、表格打印以及修订显示等功能。

利用Word“邮件合并”功能
----Word的邮件合并功能可以将数据源的数据与主文档的数据合并,主要用于生成标签和信封等具有固定格式的文档。我们可以利用这个功能,将 Notes数据库中的数据引出,生成固定格式的文档,如:图书的小标签、信封打印等。

----步骤如下:

建立邮件合并所需的数据源文档模板(Source.dot)和主文档模板(Main.dot)。

在Notes数据库中,建立视图、表单,将Source.dot、Main.dot作为附件附加到表单中, 供后面的程序调用此模板生成文档。

通过s cript编程实现用Notes文档内容生成Word文档。下面是源程序的主要部分,该程序在Notes中点击“打印”按钮,或在操作菜单中选择“打印”时执行:

----①启动WORD。 Set wordapp=GetObject(“",“Word.Application.8" If Isempty(wordapp) Then Set wordapp = CreateObject(“Word.Application.8" If Isempty(wordapp) Then Messagebox “请先安装WORD 97。",0+64,“打印" Exit Sub End If End If

----②产生数据源文件。


Call wordapp.document..add
Set WordSourceDoc= wordapp.activedocument.nbsp
Set selection=wordapp.selection
Call WordSourceDoc.Tables.Add
(wordapp.selection.Range,2, 25)
Call selection.TypeText (“图书名称"
Call Selection.MoveRight(12)
Call selection.TypeText (“图书编号"
Call Selection.MoveRight(12)
Call selection.TypeText (“购入日期"
Call Selection.MoveRight(12)
……………
//生成数据源表头

Set CurDoc = uidoc.document.nbsp
Call selection.TypeText (CurDoc.Title(0))
Call Selection.MoveRight(12)
Call selection.TypeText (CurDoc.Code(0))
Call Selection.MoveRight(12)
Call selection.TypeText (CurDoc.Date(0))
Call Selection.MoveRight(12)
……………
//将 Notes文档中的数据填入数据源表中
FileName$=“c:图书数据源.doc"
Call WordSourceDoc.SaveAs (“c: 图书数据源.Doc"
//保存数据源文件

----③产生邮件合并主文档。


Set view=CurDb.GetView(“图书打印配置"
Set tspzdoc=view.Getdocument.yKey(CurDoc.P_type(0),True)
If tslxpzdoc Is Nothing Then
Messagebox “找不到配置文档,请重新生成
"+key+“编辑配置文档。",0+64,“检测配置文档"
Exit Sub
End If
Set ts_label=tspzdoc.GetAttachment(“图书标签.dot"
If ts_label Is Nothing Then
Messagebox “配置文档中找不到WORD"
Exit Sub
End If
FileName$=“c: 图书标签.dot"
Call ts_label.ExtractFile(FileName$)
WordApp.document..Open (“C: 图书标签.dot"

----④执行邮件合并。


With WordApp.Activedocument. MailMerge
.Destination =0
.Execute
End With
Call wordapp.document.(“图书标签.dot".Close (0,0,False)
Call wordapp.document.(“图书数据源.doc".Close (0,0,False)

利用Word表格实现接口
----表格在日常办公中经常用到,用Notes表单或视图打印表格,不仅格式简单,而且不够美观,很难满足需要。此时可将Notes数据库文档引出,利用Word生成表格。

----步骤如下:

建立表格文档模板,如:科技档案案卷目录.dot。

在Notes数据库中,建立视图、表单,表单中包括每页表格的行数、打印的起始行和列, 还包括一个RTF域,将文档模板引入这个RTF域,生成文档。

s cript编程实现用Notes文档内容生成Word文档。下面是源程序的主要部分,该程序在 Notes中点击“打印”按钮,或在操作菜单中选择“打印”时执行。

----①获得打印设置以及打印模板。


Set doc = docs.Getfirstdocument.nbsp
If doc Is Nothing Then
Msgbox “没有选中打印文档",
Mb_Ok+Mb_IconStop,“停止"
Exit Sub
End If
Set Worddoc = ExtractWord(“科技档案案卷目录."
If Worddoc Is Nothing Then Exit Sub
//取得打印模板文档
Num_BeginH=Worddoc .Num_BeginH(0)
Num_BeginL=Worddoc.Num_BeginL(0)
Num_HS = Worddoc.Num_HS(0)
//取得打印设置,依次为开始行、开始列、每页打印行数

----②启动Word,根据Notes文档提供的表格模板生成Word 文档。


Set Tempdoc = CreateObject(“Word.document.8"
Set Wordapp = Tempdoc.Application
Set WordBasic = Wordapp.WordBasic
WordBasic.FileNew Worddoc.Txt_PathName(0)

----③将Notes数据库的内容引入生成的Word文档中,生 成固定格式的表格。


Items = Worddoc.Txt_Items
Pageno = 1
Row = 1
While Not(doc Is Nothing)
If Row = 1 Then
If Pageno = 1 Then
Set Table = Wordapp.Activedocument.Tables(1)
WordBasic.EditSelectAll
Wordbasic.Editcopy
Table. Cell(Num_BeginH,Num_BeginL).Select
Else
wordapp.Selection.MoveRight 1,2
Wordbasic.InsertPagebreak
WordBasic.EditPaste
Set WordSelection = Wordapp.Selection
WordSelection.Delete 1,1
Set Table = Wordapp.Activedocument.Tables(Pageno)
Table.Cell(Num_BeginH, Num_BeginL).select
End If
Else
End If
For i = 0 To Ubound(Items)
InsertStr =doc.GetItemvalue(Items(i))
Wordbasic.insert Cstr(InsertStr(0))
End If
If i = Ubound(Items) And Row = Num_HS Then
Else
Wordbasic.NextCell
End If
Next
Row = Row + 1
If Row > Num_HS Then
Row = 1
Pageno = Pageno +1
End If
Wend
WordBasic.Appshow
Wordapp.WindowState = 1
End Sub

利用Word“修订”功能 实现修改痕迹保留功能
---- 在办公自动化系统必需的公文子系统中,发文流程必然涉及到正文的修改。根据档案工作的要求,修改的原稿必需保留,这就要求发文正文的修改痕迹必需保留。要在Notes中做到这一点是非常困难的。在Word中包含一个修订功能, 如果启用了修订功能,Microsoft Word 将使用修订标记来标记修订。文档审阅完毕之后, 可以根据不同的文本颜色区分出不同审阅者所做的修改。

----步骤如下:

----1. 建立一个Word文档,文档中包含以下宏。

----①查看痕迹。


Sub 查看痕迹()
With Activedocument.nbsp
.TrackRevisions = False
//标记对指定文档的修改
.PrintRevisions = True
//在打印文档的同时打印修订标记
.ShowRevisions = True
//在屏幕上显示对指定文档的修订
End With
End Sub

----②不查看痕迹。


Sub 不查看痕迹()
With Activedocument.nbsp
.TrackRevisions = False
.PrintRevisions = False
.ShowRevisions = False
End With
End Sub

----③保存退出。


Sub 保存退出()
Activedocument.Save
Activedocument.Close
End Sub

----2.将该文档作为对象在表单中创建,注意将其显示为图表 形式。

----3.将Notes中的用户名转入Word中,以便生成标注。该程序可在数据库的PostOpen事件中触发,主要程序如下。


Dim Wordapp As Variant
Set Wordapp = CreateObject(“Word.document.8"
If Wordapp.Application.UserName
< > Session.CommonUserName Then
Wordapp.Application.UserName =
Session.CommonUserName
//设置Word用户名
End If
End Function

月影无限
21-02-06, 12:13 AM
这个问题论坛中也有类似的案例,你也可以参考一下。

dsieyx
23-10-08, 10:31 AM
请问下版上有大大用第一个notes to excel的代码成功转换的吗?我完全复制粘贴,然后改了视图名称等,但仍然报错

到了excelapplication.excel.open(gzpath)这一步就报错,我想是因为那个文件根本没生成。。。。该怎么办咧。。。

g007008
23-10-08, 03:55 PM
请问下版上有大大用第一个notes to excel的代码成功转换的吗?我完全复制粘贴,然后改了视图名称等,但仍然报错

到了excelapplication.excel.open(gzpath)这一步就报错,我想是因为那个文件根本没生成。。。。该怎么办咧。。。

excelapplication.excel.open(gzpath) 不是被注释掉了吗?怎么会运行
notes to excel这种代码 常用的很,没问题

caoxianlong
24-10-08, 10:20 PM
又长见识了,word的标签打印和修订功能,第一次看到耶。真是太感谢你们了

dsieyx
27-10-08, 02:59 PM
呃。。。to 007008,所有的注释我都去掉前面那个符号了,也把相关视图什么的也改了。。。

luqqsg2006
30-10-08, 04:00 PM
Sub Initialize
On Error Goto errorHandle
Dim s As NotesSession
Dim db As NotesDatabase
Dim vw As NotesView
Dim docc As NotesDocumentCollection
Dim doc As NotesDocument
Dim dc As NotesDocument
Dim gbry As NotesItem
Dim bsy As notesitem
Dim bm As Variant
Call printHead()
Set s=New NotesSession()
Set db =s.CurrentDatabase
Set vw=db.GetView("vwjbtj")
Set docc=vw.GetAllDocumentsByKey("top1")
Msgbox("start")
Set doc=docc.GetFirstDocument
Msgbox(docc.Count)
While Not (doc Is Nothing)
Set gbry=doc.GetFirstItem("fld1")
Set bsy =doc.GetFirstItem("fld2")
bm=Cstr(doc.fld3(0))
If Instr(bm,"/")>0 Then
bm=Left(bm,Instr(bm,"/")-1)
End If

Print|<tr><td>|+bm+|</td><td>|+Cstr(gbry.Text)+|</td><td>|+Cstr(bsy.Text)+|</td><td></td></tr>|

Set doc =docc.GetNextDocument( doc )
Wend

'输出最后的HTML
Print |<![if supportMisalignedColumns]>
<tr height=0 style='display:none'>
<td width=72 style='width:54pt'></td>
<td width=72 style='width:54pt'></td>
<td width=72 style='width:54pt'></td>
<td width=72 style='width:54pt'></td>
</tr>
<![endif]>
</table>
</div>
<!----------------------------->
<!--“从 EXCEL 发布网页”向导结束-->
<!----------------------------->
</body>|
Exit Sub
errorHandle:
Msgbox "agtExportToExcel :" & Err & " in " & Erl & ":" & Error
End Sub


printHead :
Print |Content-Type:application/vnd.ms-excel|
Print |Content-Disposition:Attachment; filename="jbsqtj.xls"|
Print |<head>
<meta http-equiv=Content-Type content="text/html; charset=gb2312">
<meta name=ProgId content=Excel.Sheet>
<meta name=Generator content="Microsoft Excel 11">
<style id="Book1_7614_Styles">
<!--table
{mso-displayed-decimal-separator:"\.";
mso-displayed-thousand-separator:"\,";}
.font57614
{color:windowtext;
font-size:9.0pt;
font-weight:400;
font-style:normal;
text-decoration:none;
font-family:宋体;
mso-generic-font-family:auto;
mso-font-charset:134;}
.xl237614
{padding-top:1px;
padding-right:1px;
padding-left:1px;
mso-ignore:padding;
color:windowtext;
font-size:10.0pt;
font-weight:700;
font-style:normal;
text-decoration:none;
font-family:宋体;
mso-generic-font-family:auto;
mso-font-charset:134;
mso-number-format:General;
text-align:center;
vertical-align:bottom;
border-top:1.0pt solid windowtext;
border-right:.15pt solid windowtext;
border-bottom:.15pt solid windowtext;
border-left:none;
mso-background-source:auto;
mso-pattern:auto;
white-space:nowrap;}
.xl257614
{padding-top:1px;
padding-right:1px;
padding-left:1px;
mso-ignore:padding;
color:windowtext;
font-size:9.0pt;
font-weight:400;
font-style:normal;
text-decoration:none;
font-family:宋体;
mso-generic-font-family:auto;
mso-font-charset:134;
mso-number-format:General;
text-align:center;
vertical-align:bottom;
border-top:none;
border-right:.5pt solid windowtext;
border-bottom:.5pt solid windowtext;
border-left:.5pt solid windowtext;
mso-background-source:auto;
mso-pattern:auto;
white-space:nowrap;}
.xl267614
{padding-top:1px;
padding-right:1px;
padding-left:1px;
mso-ignore:padding;
color:windowtext;
font-size:14.0pt;
font-weight:700;
font-style:normal;
text-decoration:none;
font-family:宋体;
mso-generic-font-family:auto;
mso-font-charset:134;
mso-number-format:General;
text-align:center;
vertical-align:bottom;
mso-background-source:auto;
mso-pattern:auto;
white-space:nowrap;}
ruby
{ruby-align:left;}
rt
{color:windowtext;
font-size:9.0pt;
font-weight:400;
font-style:normal;
text-decoration:none;
font-family:宋体;
mso-generic-font-family:auto;
mso-font-charset:134;
mso-char-type:none;}
-->
</style>
</head>
<body>
<!--[if !excel]>  <![endif]-->
<!--下列信息由 Microsoft Office Excel 的“发布为网页”向导生成。-->
<!--如果同一条目从 Excel 中重新发布,则所有位于 DIV 标记之间的信息均将被替换。-->
<!----------------------------->
<!--“从 EXCEL 发布网页”向导开始-->
<div id="Book1_7614" align=center x:publishsource="Excel">
<table x:str border=1 cellpadding=0 cellspacing=0 width=1600 style='border-collapse:
collapse;table-layout:fixed;width:648pt'>
<col width=200>
<col width=400>
<col width=400>
<col width=100>
<tr height=25 style='height:18.75pt'>
<td colspan=4 height=45 class=xl267614 style='height:18.75pt'>公司各部门周日值班统计表(|+Cstr(Today)+|)
</td></tr>
<tr height=19 style='height:14.25pt'>
<td class=xl237614 >部门</td>
<td class=xl237614 >干部值班名单</td>
<td class=xl237614 >办事员</td>
<td class=xl237614 >总计</td>
</tr>|

dsieyx
25-12-08, 10:02 AM
好吧。。。我知道这两段代码已经在网上广为流传了。。。所以要出错的话也只能是我自己什么地方写错了。。。

但'path=session.GetEnvironmentString ("D:",True)我真的取不到path啊。。。。这个执行后,path是空来着。。。然后稍微到底下一步
excelapplication.statusbar="正在创建工作表,请稍等....."
excelapplication.Visible=True
这个excel文档就自动打开了。。。然后就进行不下去了。。。。有人有在自己机器上测试完全ok的代码。。。粘贴出来咩。。。我真的是把该改的都改了。。。稍后我把我的贴上来。。。

空空空
25-12-08, 10:11 AM
path=session.GetEnvironmentString ("D:",True) 知道这句是什么意思么?

GetEnvironmentString 是取环境变量的值,这个值你可以在notes.ini里找到的,而你现在根本就没有D这个环境变量,所以返回当然空啊,一般我们常用这个来获得Notes的安装目录
path=session.GetEnvironmentString ("Directory:",True)
获得的值就是Notes.ini里Directory=D:\Lotus\Notes\Data Directory值"D:\Lotus\Notes\Data"

后面不知道你要表达什么问题

dsieyx
25-12-08, 10:18 AM
晕。。。我以为是D盘的意思。。。后面的意思是。。。我根本连excel表格都建立不了。。。


Sub Click(Source As Button)
Dim session As New NotesSession
Dim db As NotesDatabase
Dim view As Notesview
Dim doc As Notesdocument
Dim excelapplication As Variant
Dim excelworkbook As Variant
Dim excelsheet As Variant
Dim i As Integer
Dim uvcols As Integer
Dim selection As Variant

path = session.GetEnvironmentString ("D:",True)
gzpath = path + "\" + "test.xls" <----我直接改成 gzpath = "d:\test.xls" 行不?
Set excelapplication=CreateObject("Excel.Application")
excelapplication.statusbar = "正在创建工作表,请稍等....."
excelapplication.Visible = True
'==================
excelapplication.excel.open(gzpath)
excelapplication.Workbooks.Add
excelapplication.referencestyle = 2
Set excelsheet = excelapplication.Workbooks(1).worksheets(1)
excelsheet.name = "notes export"

Dim rows As Integer
Dim cols As Integer
Dim maxcols As Integer
Dim fieldname As String
Dim fitem As NotesItem
rows = 1
cols = 1
Set db=session.CurrentDatabase
Set view = db.GetView ("视图名称")
uvcols = Ubound(view.Columns)

For x = 0 To Ubound(view.Columns)
excelapplication.statusbar = "正在创建单元格,请稍等....."
If view.Columns(x).IsHidden = False Then
If view.Columns(x).title <> "" Then
excelsheet.Cells(1,1).value="第一列"
excelsheet.Cells(1,2).value="第二列"
excelsheet.Cells(rows,cols).value=view.Columns(x).Title
cols=cols+1
End If
End If
Next

maxcols = cols - 1
Set doc = view.GetFirstdocument
rows = 2
cols = 1

While Not(doc Is Nothing)
For x = 0 To Ubound(view.Columns)
excelapplication.statusbar = "正在从Notes中引入数据,请稍等....."
If view.Columns(x).IsHidden = False Then
If view.Columns(x).title <> "" Then
fieldname = view.Columns(x).itemname
Set fitem = doc.GetFirstItem(fieldname)
excelsheet.Cells(rows,cols).value = fitem.Text
cols = cols + 1
End If
End If
Next

rows = rows + 1
cols = 1
Set doc = view.GetNextdocument(doc)
Wend

With excelapplication.worksheets(1)
.pagesetup.orientation = 2
.pagesetup.centerheader = "report_confidential"
.pagesetup.rightfooter = "page &P"&Chr$(13) &"Date:&D"
.pagesetup.CenterFooter = ""
End With

excelapplication.referencestyle = 1
excelapplication.range("A1").Select
excelapplication.statusbar = "数据导入完成。"
excelsheet.PageSetup.PrintGridlines=True
excelworkbook.printout
excelworkbook.SaveAs("d:\test.xls")
excelworkbook.Save
excelapplication.Quit
Set excelapplication=Nothing
End Sub


还是有错误。。。你的意思是我直接写成path = session.GetEnvironmentString(Directory:",true),后,那pth就等于我的notesini里的那个值是吗?我现在机器的默认安装目录是C:\Program Files\lotus\notes\data,也就是说这个xls文档的默认存储路径是在这下面?但。。。我运行代码的时候为啥path还是为空?是默认就是这样子咩。。。。

那如果我单纯就想在d盘下建立个文档,那是不是我可以干脆不要这句path,直接改成 gzpath = "d:\test.xls" 行不?

10:44更新:嗯,到了excelapplication.excel.open(gzpath)就弹出来"instance member excel doesn't exist".....难道还非得按原程序里写?但这个时候连那个excel文档都打开了,而且确实在状态栏里有正在被创建

空空空
25-12-08, 10:51 AM
Directory加上引号
可以啊,你就随便 path = "d:\test.xls",保存到D下的test.xls就可以
测试的时候可以这样,如果这代码在服务器上运行的话,我们都是会把这种文件放到Notes目录下创建个临时文件夹里,这时就用到这个Directory环境变量了

后面excel的代码我看你很烦琐,那是因为你都是考百来靠百去的,根本就没花心思去研究,很多句都可以去掉,代码可以写的很简洁,你自己先每句逐步研究下代表什么意思,可以从很简单的一个功能开始,比方说只实现创建一个Excel文件,这个实现了之后,如何写入数据,一步一步来,你会掌握的好一些,否则也是云里雾里的,就不好了

dsieyx
25-12-08, 11:42 AM
是啊。。。我也觉得很繁琐。。。都不知道哪些是完全可以省去的。。。下午俺在研究的说,咔咔。。。