博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
开发可复用的从Domino中导出数据到Excel的类
阅读量:7050 次
发布时间:2019-06-28

本文共 7708 字,大约阅读时间需要 25 分钟。

在domino开发中我们不可避免的要和报表打交道,一般就是生成各种Excel报表,本人主要为了自己在开发中方便,简单实现了一个基本类,现在功能有限,当然这个类我慢慢的根据以后遇到的需求逐渐完善。 

None.gif
Const
 EXCEL_APPLICATION        
=
 
"
Excel.application
"
 
None.gif 
None.gif
Private
 
Const
 BASEERROR                                                
=
 
1200
 
None.gif
'
Private Const ERROR_NOSUCHCELL                            = BASEERROR + 0
 
None.gif'
Private Const ERRORTEXT_NOSUCHCELL                    = "Excel Report - Could not get data from cell."
 
None.gif
 
None.gif
Const
 REG_97            
=
 
"
Software\\Microsoft\\Office\\8.0\\Common\\InstallRoot
"
                    
'
Registry Key Office 97
 
None.gif
Const
 REG_2000        
=
 
"
Software\\Microsoft\\Office\\9.0\\Common\\InstallRoot
"
                    
'
Registry Key Office 2000
 
None.gif
Const
 REG_XP            
=
 
"
Software\\Microsoft\\Office\\10.0\\Common\\InstallRoot
"
                    
'
Registry Key Office XP
 
None.gif
Const
 REG_2003        
=
"
Software\\Microsoft\\Office\\11.0\\Common\\InstallRoot
"
                    
'
Registry Key Office 2003
 
None.gif
 
None.gif
Const
 NAME_97        
=
 
"
Office 97
"
 
None.gif
Const
 NAME_2000        
=
 
"
Office 2000
"
 
None.gif
Const
 NAME_XP        
=
 
"
Office XP
"
 
None.gif
Const
 NAME_2003        
=
 
"
Office 2003
"
 
None.gif 
ExpandedBlockStart.gif
Class ExcelHelper 
InBlock.gif     
InBlock.gif    
Private xlApp As Variant                    ' Application object 
InBlock.gif
    Private strFilePath As String     
InBlock.gif     
ExpandedSubBlockStart.gif    
Sub new(xlFilename As String, isVisible As Boolean
InBlock.gif        
On Error Goto GeneralError         
InBlock.gif        
Set xlApp = CreateObject(EXCEL_APPLICATION)        ' open the application 
InBlock.gif
        xlApp.Workbooks.Add xlFilename                            ' create an Excel workbook 
InBlock.gif
        xlApp.Visible = isVisible                                            ' make it visible (or not) 
InBlock.gif
        strFilePath = xlFilename                                            ' store the filename        
InBlock.gif
        Goto ExitSub 
InBlock.gif         
InBlock.gifGeneralError: 
InBlock.gif        
If Not (xlApp Is NothingThen xlApp.quit                    ' quit, if there is an error 
InBlock.gif
        Resume ExitSub         
InBlock.gifExitSub: 
ExpandedSubBlockEnd.gif    
End Sub
     
InBlock.gif     
ExpandedSubBlockStart.gif    
Public Function save 
InBlock.gif        xlApp.ActiveWorkbook.SaveAs( strFilePath ) 
ExpandedSubBlockEnd.gif    
End Function
     
InBlock.gif     
ExpandedSubBlockStart.gif    
Public Function saveAs(newFilename) 
InBlock.gif        xlApp.ActiveWorkbook.SaveAs( newFileName ) 
ExpandedSubBlockEnd.gif    
End Function
     
InBlock.gif     
ExpandedSubBlockStart.gif    
Public Function setCell( Sheet As Variant , row As Integer , column As Variant , value As Variant ) 
InBlock.gif        xlApp.Workbooks(
1).Worksheets( Sheet ).Cells( row , column ).Value = value 
ExpandedSubBlockEnd.gif    
End Function
 
InBlock.gif     
ExpandedSubBlockStart.gif    
Public Function getCell( Sheet As Variant , row As Integer , column As Variant ) As String 
InBlock.gif        
On Error Goto GeneralError 
InBlock.gif        getCell 
= xlApp.Workbooks(1).Worksheets( Sheet ).Cells( row , column ).Value 
InBlock.gif        
Goto ExitSub         
InBlock.gifGeneralError: 
InBlock.gif        getCell 
= "" 
InBlock.gif        
Resume ExitSub         
InBlock.gifExitSub:         
ExpandedSubBlockEnd.gif    
End Function
     
InBlock.gif     
ExpandedSubBlockStart.gif    
Public Function quit 
InBlock.gif        
If Not (xlApp Is NothingThen 
InBlock.gif            xlApp.Quit 
InBlock.gif            
Set xlApp = Nothing     
InBlock.gif        
End If 
ExpandedSubBlockEnd.gif    
End Function
     
InBlock.gif     
ExpandedSubBlockStart.gif    
Public Function setVisibility(isVisible As Boolean
InBlock.gif        
If (isVisible And Not xlApp.Visible)     Then     xlApp.Visible = True 
InBlock.gif        
If (Not isVisible And xlApp.Visible)    Then        xlApp.Visible = False 
ExpandedSubBlockEnd.gif    
End Function
 
InBlock.gif     
ExpandedSubBlockStart.gif    
Public Function setSheetName(Sheet As Variant,sheetName As String
InBlock.gif        xlApp.Workbooks(
1).Worksheets( Sheet ).Select 
InBlock.gif        xlApp.Workbooks(
1).Worksheets( Sheet ).Name=sheetName 
ExpandedSubBlockEnd.gif    
End Function
 
InBlock.gif     
ExpandedSubBlockStart.gif    
Public Function setCellColor(Sheet As Variant, row As Integer, column As Variant, innercolor As Variant) 
InBlock.gif        
On Error Goto GeneralError         
InBlock.gif        
If Cstr(innercolor) <> "" Then 
InBlock.gif            xlApp.Workbooks(
1).Worksheets( Sheet ).Cells( row , column ).Interior.ColorIndex = innercolor     
InBlock.gif        
End If         
InBlock.gif        
Goto ExitSub         
InBlock.gifGeneralError: 
InBlock.gif        
Resume ExitSub         
InBlock.gifExitSub: 
ExpandedSubBlockEnd.gif    
End Function
     
InBlock.gif     
ExpandedSubBlockStart.gif    
Public Function setCellFont(Sheet As Variant, row As Integer, column As Variant, style As Variant, size As Variant, color As Variant) 
InBlock.gif        
On Error Goto GeneralError         
InBlock.gif        
If Cstr(style) <> "" Then  
InBlock.gif            xlApp.Workbooks(
1).Worksheets( Sheet ).Cells( row , column ).Font.FontStyle         = style 
InBlock.gif        
End If 
InBlock.gif         
InBlock.gif        
If Cstr(size) <> "" Then 
InBlock.gif            xlApp.Workbooks(
1).Worksheets( Sheet ).Cells( row , column ).Font.Size            = size 
InBlock.gif        
End If 
InBlock.gif         
InBlock.gif        
If Cstr(color) <> "" Then 
InBlock.gif            xlApp.Workbooks(
1).Worksheets( Sheet ).Cells( row , column ).Font.ColorIndex     = color 
InBlock.gif        
End If         
InBlock.gif         
InBlock.gif        
Goto ExitSub 
InBlock.gif         
InBlock.gifGeneralError: 
InBlock.gif        
Resume ExitSub         
InBlock.gifExitSub: 
ExpandedSubBlockEnd.gif    
End Function
     
InBlock.gif     
ExpandedSubBlockStart.gif    
Public Function setRowFont(Sheet As Variant, row As Integer,  style As Variant, size As Variant, color As Variant) 
InBlock.gif        
On Error Goto GeneralError         
InBlock.gif        
Dim rowpara As String 
InBlock.gif        rowpara
=Cstr(row)+":"+Cstr(row) 
InBlock.gif         
InBlock.gif        
If Cstr(style) <> "" Then  
InBlock.gif            xlApp.Workbooks(
1).Worksheets( Sheet ).Rows(rowpara).Select 
InBlock.gif            xlApp.Selection.Font.FontStyle     
= style 
InBlock.gif        
End If 
InBlock.gif         
InBlock.gif        
If Cstr(size) <> "" Then 
InBlock.gif            xlApp.Workbooks(
1).Worksheets( Sheet ).Rows(rowpara).Select 
InBlock.gif            xlApp.Selection.Font.Size    
= size 
InBlock.gif        
End If 
InBlock.gif         
InBlock.gif        
If Cstr(color) <> "" Then 
InBlock.gif            xlApp.Workbooks(
1).Worksheets( Sheet ).Rows(rowpara).Select 
InBlock.gif            xlApp.Selection.Font.ColorIndex 
= color 
InBlock.gif        
End If 
InBlock.gif         
InBlock.gif        
Goto ExitSub         
InBlock.gifGeneralError: 
InBlock.gif        
Resume ExitSub         
InBlock.gifExitSub: 
ExpandedSubBlockEnd.gif    
End Function
     
InBlock.gif     
ExpandedSubBlockStart.gif    
Public Function getVersion() As String         
InBlock.gif        
On Error Goto GeneralError         
InBlock.gif        
Dim formula As String 
InBlock.gif        
Dim SWVersion As String 
InBlock.gif        
Dim Versions List As String 
InBlock.gif        
Dim v As Variant         
InBlock.gif         
InBlock.gif        Versions(NAME_97)        
= REG_97 
InBlock.gif        Versions(NAME_2000)    
= REG_2000 
InBlock.gif        Versions(NAME_XP)        
= REG_XP 
InBlock.gif        Versions(NAME_2003)    
= REG_2003     
InBlock.gif         
InBlock.gif        Forall vers 
In Versions 
InBlock.gif            formula$ 
= | (@RegQueryValue("HKEY_LOCAL_MACHINE""| & vers & |";"Path")) | 
InBlock.gif            v 
= Evaluate( formula$ ) 
InBlock.gif            
If v(0<> "" Then 
InBlock.gif                getVersion 
= Listtag(vers) 
InBlock.gif                
Goto ExitSub 
InBlock.gif            
End If 
InBlock.gif        
End Forall 
InBlock.gif         
InBlock.gif        getVersion 
= ""         
InBlock.gif        
Goto ExitSub 
InBlock.gif         
InBlock.gifGeneralError:         
InBlock.gif        getVersion 
= "" 
InBlock.gif        
Resume ExitSub         
InBlock.gifExitSub: 
ExpandedSubBlockEnd.gif    
End Function
     
InBlock.gif     
ExpandedSubBlockStart.gif    
Public Function exportNotesView(view As NotesView, Sheet As Variant, OffsetRow As Integer, OffsetCol As Integer, isWithheader As Boolean, includeIcons As Boolean, includeColors As Boolean, includeHidden As Boolean
InBlock.gif        
Dim viewnav As NotesViewNavigator 
InBlock.gif        
Dim entry As NotesViewEntry 
InBlock.gif        
Dim viewcolumns As Variant 
InBlock.gif        
Dim column As Integer 
InBlock.gif        
Dim row As Integer         
InBlock.gif        
Dim i As Integer 
InBlock.gif        
Dim array(0 To 9As String 
InBlock.gif        array(
0)="A"  
InBlock.gif        array(
1)="B"   
InBlock.gif        array(
2)="C"  
InBlock.gif        array(
3)="D"  
InBlock.gif        array(
4)="E"  
InBlock.gif        array(
5)="F"  
InBlock.gif        array(
6)="G"  
InBlock.gif        array(
7)="H"  
InBlock.gif        array(
8)="I"  
InBlock.gif        array(
9)="J"          
InBlock.gif         
InBlock.gif        
Set viewnav     = view.CreateViewNav() 
InBlock.gif        
Set entry        = viewnav.GetFirstDocument() 
InBlock.gif        viewcolumns    
= view.Columns 
InBlock.gif        row                 
= OffsetRow + 1 
InBlock.gif        column             
= OffsetCol + 1         
InBlock.gif         
InBlock.gif        
If isWithHeader Then 
InBlock.gif            Forall vc 
In viewcolumns 
InBlock.gif                
Call Me.setCell(Sheet, row, column, vc.title)     
InBlock.gif                column 
= column + 1 
InBlock.gif            
End Forall 
InBlock.gif        
End If             
InBlock.gif         
InBlock.gif        
While Not (entry Is Nothing
InBlock.gif            row             
= row + 1 
InBlock.gif            column         
= OffsetCol + 1 
InBlock.gif            Forall cv 
In entry.ColumnValues 
InBlock.gif                
If doColumnExport(viewcolumns(column - OffsetCol - 1), includeHidden, IncludeIcons, includeColors) Then 
InBlock.gif                    
Call Me.setCell(Sheet, row, column, Cstr(cv))     
InBlock.gif                
End If 
InBlock.gif                column 
= column + 1 
InBlock.gif            
End Forall             
InBlock.gif            
Set entry = viewnav.GetNextDocument(entry) 
InBlock.gif        Wend         
InBlock.gif         
InBlock.gif        
For i=0 To  (column-1)  
InBlock.gif            
Call Me.autoFit(Sheet,array(i))             
InBlock.gif        
Next     
InBlock.gif         
ExpandedSubBlockEnd.gif    
End Function
 
InBlock.gif     
ExpandedSubBlockStart.gif    
Private Function doColumnExport (viewcol As NotesViewColumn, includeHidden As Boolean, IncludeIcons As Boolean, includeColors As BooleanAs Boolean 
InBlock.gif        
Dim isHiddenOK     As Boolean 
InBlock.gif        
Dim isIconOK         As Boolean 
InBlock.gif        
Dim isColorOK         As Boolean 
InBlock.gif         
InBlock.gif        isHiddenOK 
= (viewcol.isHidden And IncludeHidden) Or Not viewcol.isHidden 
InBlock.gif        isIconOK    
= (viewcol.isIcon And IncludeIcons) Or Not (viewcol.isIcon) 
InBlock.gif        isColorOK    
= True 
InBlock.gif        doColumnExport 
= isHiddenOK And isIconOK And isColorOK 
ExpandedSubBlockEnd.gif    
End Function
 
InBlock.gif     
ExpandedSubBlockStart.gif    
Public Function autoFit(Sheet As Variant,col As String
InBlock.gif        xlApp.Workbooks(
1).Worksheets(Sheet).Columns(col+":"+col).EntireColumn.AutoFit 
ExpandedSubBlockEnd.gif    
End Function
 
InBlock.gif     
InBlock.gif     
ExpandedBlockEnd.gif
End Class 
测试程序调用的代理代码如下: 
ExpandedBlockStart.gif
Sub Initialize     
InBlock.gif    
Dim view As NotesView 
InBlock.gif    
Dim excelfilepath As String 
InBlock.gif    
Dim Sheet As Variant 
InBlock.gif    
Dim OffsetX As Integer 
InBlock.gif    
Dim OffsetY As Integer 
InBlock.gif    
Dim isWithHeader As Boolean 
InBlock.gif    
Dim includeIcons As Boolean 
InBlock.gif    
Dim includeColors As Boolean 
InBlock.gif    
Dim includeHidden As Boolean 
InBlock.gif    
Dim session As New NotesSession 
InBlock.gif    
Dim currdb As NotesDatabase     
InBlock.gif     
InBlock.gif    
Const Font_Style            = "Bold" 
InBlock.gif    
Const Font_Size                =12 
InBlock.gif    
Const Font_Color                =5         
InBlock.gif     
InBlock.gif    
Set currdb=session.CurrentDatabase 
InBlock.gif    Sheet                     
= 1 
InBlock.gif    OffsetX                    
= 1 
InBlock.gif    OffsetY                    
= 1 
InBlock.gif    isWithHeader            
= True 
InBlock.gif    includeIcons            
= True 
InBlock.gif    includeColors        
= True 
InBlock.gif    includeHidden        
= True 
InBlock.gif    excelfilepath            
= ""            ' create an empty excel file 
InBlock.gif
     
InBlock.gif    
'Set view         = ws.CurrentView.View 
InBlock.gif
    Set view=currdb.GetView("chunainfo"
InBlock.gif    
Set report= New ExcelHelper(""True
InBlock.gif     
InBlock.gif    
'Call report.setCellFont(1, 2, 2, Font_Style, Font_Size, Font_Color) 
InBlock.gif
     
InBlock.gif    
Call report.setRowFont(12, Font_Style, Font_Size, Font_Color) 
InBlock.gif    
Call report.exportNotesView(view, Sheet, OffsetX, OffsetY, isWithheader, includeIcons, includeColors, includeHidden) 
InBlock.gif    
Call report.exportNotesView(view, 2, OffsetX, OffsetY, isWithheader, includeIcons, includeColors, includeHidden) 
InBlock.gif    
Call report.setVisibility(True
InBlock.gif    
Call report.setSheetName(Sheet,"请假单"
InBlock.gif    
Call report.setSheetName(2,"出差报核单"
InBlock.gif    
Msgbox "成功导出报表" 
InBlock.gif     
ExpandedBlockEnd.gif
End Sub 
该类还有很多不完善的地方,一点一点慢慢来吧。
本文转自生鱼片博客园博客,原文链接:http://www.cnblogs.com/carysun/archive/2008/08/08/DominoExcel.html,如需转载请自行联系原作者
你可能感兴趣的文章
Ubuntu12.04下JDK、Eclipse的安装,在linux终端中用命令启动eclipse
查看>>
zookeeper集群配置
查看>>
img2icns制作mac下应用的图标
查看>>
web.xml之context-param,listener,filter, servlet备忘录
查看>>
Maven Dependencies - miss
查看>>
Mongo Collections
查看>>
Android MVVM开发——DataBinding基础
查看>>
php中file_get_content 和curl以及fopen
查看>>
基于 Pusher 驱动的 Laravel 事件广播(上)
查看>>
fuel部署openstack 打开fuel的UI界面出现白屏的情况
查看>>
PhpStrom安装Xdebug调试工具
查看>>
Spark Streaming源码解读之数据清理 内幕
查看>>
项目打包流程
查看>>
vue-cli项目动态引用图片链接问题
查看>>
规格模式取代sql查询代码
查看>>
iOS 如何去掉Tabbar上的黑线
查看>>
eclipse远程调试(windows)
查看>>
FDStatusBarNotifierView
查看>>
OpenStack云平台的网络模式及其工作机制
查看>>
PHP-FIG 定义的 PSR-3 日志接口规范
查看>>