开发可复用的从Domino中导出数据到Excel的类

简介:

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

复制代码
Const  EXCEL_APPLICATION         =   " Excel.application "  
 
Private   Const  BASEERROR                                                 =   1200  
' Private Const ERROR_NOSUCHCELL                            = BASEERROR + 0  
'
Private Const ERRORTEXT_NOSUCHCELL                    = "Excel Report - Could not get data from cell."  
 
Const  REG_97             =   " Software\\Microsoft\\Office\\8.0\\Common\\InstallRoot "                      ' Registry Key Office 97  
Const  REG_2000         =   " Software\\Microsoft\\Office\\9.0\\Common\\InstallRoot "                      ' Registry Key Office 2000  
Const  REG_XP             =   " Software\\Microsoft\\Office\\10.0\\Common\\InstallRoot "                      ' Registry Key Office XP  
Const  REG_2003         = " Software\\Microsoft\\Office\\11.0\\Common\\InstallRoot "                      ' Registry Key Office 2003  
 
Const  NAME_97         =   " Office 97 "  
Const  NAME_2000         =   " Office 2000 "  
Const  NAME_XP         =   " Office XP "  
Const  NAME_2003         =   " Office 2003 "  
 
Class ExcelHelper 
     
    
Private xlApp As Variant                    ' Application object 
    Private strFilePath As String     
     
    
Sub new(xlFilename As String, isVisible As Boolean
        
On Error Goto GeneralError         
        
Set xlApp = CreateObject(EXCEL_APPLICATION)        ' open the application 
        xlApp.Workbooks.Add xlFilename                            ' create an Excel workbook 
        xlApp.Visible = isVisible                                            ' make it visible (or not) 
        strFilePath = xlFilename                                            ' store the filename        
        Goto ExitSub 
         
GeneralError: 
        
If Not (xlApp Is NothingThen xlApp.quit                    ' quit, if there is an error 
        Resume ExitSub         
ExitSub: 
    
End Sub
     
     
    
Public Function save 
        xlApp.ActiveWorkbook.SaveAs( strFilePath ) 
    
End Function
     
     
    
Public Function saveAs(newFilename) 
        xlApp.ActiveWorkbook.SaveAs( newFileName ) 
    
End Function
     
     
    
Public Function setCell( Sheet As Variant , row As Integer , column As Variant , value As Variant ) 
        xlApp.Workbooks(
1).Worksheets( Sheet ).Cells( row , column ).Value = value 
    
End Function
 
     
    
Public Function getCell( Sheet As Variant , row As Integer , column As Variant ) As String 
        
On Error Goto GeneralError 
        getCell 
= xlApp.Workbooks(1).Worksheets( Sheet ).Cells( row , column ).Value 
        
Goto ExitSub         
GeneralError: 
        getCell 
= "" 
        
Resume ExitSub         
ExitSub:         
    
End Function
     
     
    
Public Function quit 
        
If Not (xlApp Is NothingThen 
            xlApp.Quit 
            
Set xlApp = Nothing     
        
End If 
    
End Function
     
     
    
Public Function setVisibility(isVisible As Boolean
        
If (isVisible And Not xlApp.Visible)     Then     xlApp.Visible = True 
        
If (Not isVisible And xlApp.Visible)    Then        xlApp.Visible = False 
    
End Function
 
     
    
Public Function setSheetName(Sheet As Variant,sheetName As String
        xlApp.Workbooks(
1).Worksheets( Sheet ).Select 
        xlApp.Workbooks(
1).Worksheets( Sheet ).Name=sheetName 
    
End Function
 
     
    
Public Function setCellColor(Sheet As Variant, row As Integer, column As Variant, innercolor As Variant) 
        
On Error Goto GeneralError         
        
If Cstr(innercolor) <> "" Then 
            xlApp.Workbooks(
1).Worksheets( Sheet ).Cells( row , column ).Interior.ColorIndex = innercolor     
        
End If         
        
Goto ExitSub         
GeneralError: 
        
Resume ExitSub         
ExitSub: 
    
End Function
     
     
    
Public Function setCellFont(Sheet As Variant, row As Integer, column As Variant, style As Variant, size As Variant, color As Variant) 
        
On Error Goto GeneralError         
        
If Cstr(style) <> "" Then  
            xlApp.Workbooks(
1).Worksheets( Sheet ).Cells( row , column ).Font.FontStyle         = style 
        
End If 
         
        
If Cstr(size) <> "" Then 
            xlApp.Workbooks(
1).Worksheets( Sheet ).Cells( row , column ).Font.Size            = size 
        
End If 
         
        
If Cstr(color) <> "" Then 
            xlApp.Workbooks(
1).Worksheets( Sheet ).Cells( row , column ).Font.ColorIndex     = color 
        
End If         
         
        
Goto ExitSub 
         
GeneralError: 
        
Resume ExitSub         
ExitSub: 
    
End Function
     
     
    
Public Function setRowFont(Sheet As Variant, row As Integer,  style As Variant, size As Variant, color As Variant) 
        
On Error Goto GeneralError         
        
Dim rowpara As String 
        rowpara
=Cstr(row)+":"+Cstr(row) 
         
        
If Cstr(style) <> "" Then  
            xlApp.Workbooks(
1).Worksheets( Sheet ).Rows(rowpara).Select 
            xlApp.Selection.Font.FontStyle     
= style 
        
End If 
         
        
If Cstr(size) <> "" Then 
            xlApp.Workbooks(
1).Worksheets( Sheet ).Rows(rowpara).Select 
            xlApp.Selection.Font.Size    
= size 
        
End If 
         
        
If Cstr(color) <> "" Then 
            xlApp.Workbooks(
1).Worksheets( Sheet ).Rows(rowpara).Select 
            xlApp.Selection.Font.ColorIndex 
= color 
        
End If 
         
        
Goto ExitSub         
GeneralError: 
        
Resume ExitSub         
ExitSub: 
    
End Function
     
     
    
Public Function getVersion() As String         
        
On Error Goto GeneralError         
        
Dim formula As String 
        
Dim SWVersion As String 
        
Dim Versions List As String 
        
Dim v As Variant         
         
        Versions(NAME_97)        
= REG_97 
        Versions(NAME_2000)    
= REG_2000 
        Versions(NAME_XP)        
= REG_XP 
        Versions(NAME_2003)    
= REG_2003     
         
        Forall vers 
In Versions 
            formula$ 
= | (@RegQueryValue("HKEY_LOCAL_MACHINE""| & vers & |";"Path")) | 
            v 
= Evaluate( formula$ ) 
            
If v(0<> "" Then 
                getVersion 
= Listtag(vers) 
                
Goto ExitSub 
            
End If 
        
End Forall 
         
        getVersion 
= ""         
        
Goto ExitSub 
         
GeneralError:         
        getVersion 
= "" 
        
Resume ExitSub         
ExitSub: 
    
End Function
     
     
    
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
        
Dim viewnav As NotesViewNavigator 
        
Dim entry As NotesViewEntry 
        
Dim viewcolumns As Variant 
        
Dim column As Integer 
        
Dim row As Integer         
        
Dim i As Integer 
        
Dim array(0 To 9As String 
        array(
0)="A"  
        array(
1)="B"   
        array(
2)="C"  
        array(
3)="D"  
        array(
4)="E"  
        array(
5)="F"  
        array(
6)="G"  
        array(
7)="H"  
        array(
8)="I"  
        array(
9)="J"          
         
        
Set viewnav     = view.CreateViewNav() 
        
Set entry        = viewnav.GetFirstDocument() 
        viewcolumns    
= view.Columns 
        row                 
= OffsetRow + 1 
        column             
= OffsetCol + 1         
         
        
If isWithHeader Then 
            Forall vc 
In viewcolumns 
                
Call Me.setCell(Sheet, row, column, vc.title)     
                column 
= column + 1 
            
End Forall 
        
End If             
         
        
While Not (entry Is Nothing
            row             
= row + 1 
            column         
= OffsetCol + 1 
            Forall cv 
In entry.ColumnValues 
                
If doColumnExport(viewcolumns(column - OffsetCol - 1), includeHidden, IncludeIcons, includeColors) Then 
                    
Call Me.setCell(Sheet, row, column, Cstr(cv))     
                
End If 
                column 
= column + 1 
            
End Forall             
            
Set entry = viewnav.GetNextDocument(entry) 
        Wend         
         
        
For i=0 To  (column-1)  
            
Call Me.autoFit(Sheet,array(i))             
        
Next     
         
    
End Function
 
     
    
Private Function doColumnExport (viewcol As NotesViewColumn, includeHidden As Boolean, IncludeIcons As Boolean, includeColors As BooleanAs Boolean 
        
Dim isHiddenOK     As Boolean 
        
Dim isIconOK         As Boolean 
        
Dim isColorOK         As Boolean 
         
        isHiddenOK 
= (viewcol.isHidden And IncludeHidden) Or Not viewcol.isHidden 
        isIconOK    
= (viewcol.isIcon And IncludeIcons) Or Not (viewcol.isIcon) 
        isColorOK    
= True 
        doColumnExport 
= isHiddenOK And isIconOK And isColorOK 
    
End Function
 
     
    
Public Function autoFit(Sheet As Variant,col As String
        xlApp.Workbooks(
1).Worksheets(Sheet).Columns(col+":"+col).EntireColumn.AutoFit 
    
End Function
 
     
     
End Class 

测试程序调用的代理代码如下: 

复制代码
Sub Initialize     
    
Dim view As NotesView 
    
Dim excelfilepath As String 
    
Dim Sheet As Variant 
    
Dim OffsetX As Integer 
    
Dim OffsetY As Integer 
    
Dim isWithHeader As Boolean 
    
Dim includeIcons As Boolean 
    
Dim includeColors As Boolean 
    
Dim includeHidden As Boolean 
    
Dim session As New NotesSession 
    
Dim currdb As NotesDatabase     
     
    
Const Font_Style            = "Bold" 
    
Const Font_Size                =12 
    
Const Font_Color                =5         
     
    
Set currdb=session.CurrentDatabase 
    Sheet                     
= 1 
    OffsetX                    
= 1 
    OffsetY                    
= 1 
    isWithHeader            
= True 
    includeIcons            
= True 
    includeColors        
= True 
    includeHidden        
= True 
    excelfilepath            
= ""            ' create an empty excel file 
     
    
'Set view         = ws.CurrentView.View 
    Set view=currdb.GetView("chunainfo"
    
Set report= New ExcelHelper(""True
     
    
'Call report.setCellFont(1, 2, 2, Font_Style, Font_Size, Font_Color) 
     
    
Call report.setRowFont(12, Font_Style, Font_Size, Font_Color) 
    
Call report.exportNotesView(view, Sheet, OffsetX, OffsetY, isWithheader, includeIcons, includeColors, includeHidden) 
    
Call report.exportNotesView(view, 2, OffsetX, OffsetY, isWithheader, includeIcons, includeColors, includeHidden) 
    
Call report.setVisibility(True
    
Call report.setSheetName(Sheet,"请假单"
    
Call report.setSheetName(2,"出差报核单"
    
Msgbox "成功导出报表" 
     
End Sub 
复制代码


该类还有很多不完善的地方,一点一点慢慢来吧。
复制代码


本文转自生鱼片博客园博客,原文链接:http://www.cnblogs.com/carysun/archive/2008/08/08/DominoExcel.html,如需转载请自行联系原作者
目录
相关文章
|
4月前
|
Python
如何根据Excel某列数据为依据分成一个新的工作表
在处理Excel数据时,我们常需要根据列值将数据分到不同的工作表或文件中。本文通过Python和VBA两种方法实现该操作:使用Python的`pandas`库按年级拆分为多个文件,再通过VBA宏按班级生成新的工作表,帮助高效整理复杂数据。
|
4月前
|
数据采集 数据可视化 数据挖掘
用 Excel+Power Query 做电商数据分析:从 “每天加班整理数据” 到 “一键生成报表” 的配置教程
在电商运营中,数据是增长的关键驱动力。然而,传统的手工数据处理方式效率低下,耗费大量时间且易出错。本文介绍如何利用 Excel 中的 Power Query 工具,自动化完成电商数据的采集、清洗与分析,大幅提升数据处理效率。通过某美妆电商的实战案例,详细拆解从多平台数据整合到可视化报表生成的全流程,帮助电商从业者摆脱繁琐操作,聚焦业务增长,实现数据驱动的高效运营。
|
6月前
|
存储 安全 大数据
网安工程师必看!AiPy解决fscan扫描数据整理难题—多种信息快速分拣+Excel结构化存储方案
作为一名安全测试工程师,分析fscan扫描结果曾是繁琐的手动活:从海量日志中提取开放端口、漏洞信息和主机数据,耗时又易错。但现在,借助AiPy开发的GUI解析工具,只需喝杯奶茶的时间,即可将[PORT]、[SERVICE]、[VULN]、[HOST]等关键信息智能分类,并生成三份清晰的Excel报表。告别手动整理,大幅提升效率!在安全行业,工具党正碾压手动党。掌握AiPy,把时间留给真正的攻防实战!官网链接:https://www.aipyaipy.com,解锁更多用法!
|
4月前
|
Python
Excel中如何批量重命名工作表与将每个工作表导出到单独Excel文件
本文介绍了如何在Excel中使用VBA批量重命名工作表、根据单元格内容修改颜色,以及将工作表导出为独立文件的方法。同时提供了Python实现导出工作表的代码示例,适用于自动化处理Excel文档。
|
11月前
|
数据采集 数据可视化 数据挖掘
利用Python自动化处理Excel数据:从基础到进阶####
本文旨在为读者提供一个全面的指南,通过Python编程语言实现Excel数据的自动化处理。无论你是初学者还是有经验的开发者,本文都将帮助你掌握Pandas和openpyxl这两个强大的库,从而提升数据处理的效率和准确性。我们将从环境设置开始,逐步深入到数据读取、清洗、分析和可视化等各个环节,最终实现一个实际的自动化项目案例。 ####
2004 10
|
4月前
|
Python
将Excel特定某列数据删除
将Excel特定某列数据删除
|
5月前
|
Java 测试技术 数据库
spring号码归属地批量查询,批量查询号码归属地,在线工具,可按省份城市运营商号段分类分开分别导出excel表格
简介:文章探讨Spring Boot项目启动优化策略,通过自定义监听器、异步初始化及分库分表加载优化等手段,将项目启动时间从280秒缩短至159秒,提升约50%,显著提高开发效率。
|
9月前
|
分布式计算 Hadoop 大数据
从Excel到Hadoop:数据规模的进化之路
从Excel到Hadoop:数据规模的进化之路
199 10
|
11月前
|
存储 Java easyexcel
招行面试:100万级别数据的Excel,如何秒级导入到数据库?
本文由40岁老架构师尼恩撰写,分享了应对招商银行Java后端面试绝命12题的经验。文章详细介绍了如何通过系统化准备,在面试中展示强大的技术实力。针对百万级数据的Excel导入难题,尼恩推荐使用阿里巴巴开源的EasyExcel框架,并结合高性能分片读取、Disruptor队列缓冲和高并发批量写入的架构方案,实现高效的数据处理。此外,文章还提供了完整的代码示例和配置说明,帮助读者快速掌握相关技能。建议读者参考《尼恩Java面试宝典PDF》进行系统化刷题,提升面试竞争力。关注公众号【技术自由圈】可获取更多技术资源和指导。
|
12月前
|
数据格式 UED
记录一次NPOI库导出Excel遇到的小问题解决方案
【11月更文挑战第16天】本文记录了使用 NPOI 库导出 Excel 过程中遇到的三个主要问题及其解决方案:单元格数据格式错误、日期格式不正确以及合并单元格边框缺失。通过自定义单元格样式、设置数据格式和手动添加边框,有效解决了这些问题,提升了导出文件的质量和用户体验。
927 3