假如您手头已有一xls文档等待封装,假如您机子上已安装有VB6开发系统,那么请跟着往下操作:

一、用VB制作EXE文件头部分

1、打开VB,“文件”-“新建工程”-“标准EXE”;

2、此时会出现名为Form1的默认窗体编辑窗口,Form1将作为软件启动封面窗体,打开该Form1的属性窗口,对如下属性进行设置:BorderStyle=0,StartUpPosition=2,Icon与Picture属性设置成你需要的图标(这也将成为你EXE的图标)和设计好准备使用的图片(即软件封面),窗体的大小设置成您需要的合适值即可。

3、往窗体中添加一个时钟控件timer1,并将其InterVal属性设为1000。

4、双击窗体打开代码编辑窗口,录入以下代码:
Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Private Declare Function GetTempFileName Lib "kernel32" Alias "GetTempFileNameA" (ByVal lpszPath As String, ByVal lpPrefixString As String, ByVal wUnique As Long, ByVal lpTempFileName As String) As Long
Private Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
Private Const MAX_PATH = 260
Private Const EXE_SIZE = 81920 '本EXE实际字节大小
Private Type FileSection
Bytes() As Byte
End Type
Private Type SectionedFile
Files() As FileSection
End Type
Dim StopTime As Integer

Private Sub Form_Activate()
If Command() = "" Then Main1
End Sub

Private Sub Form_Load()
On Error Resume Next
If Command() = "" Then
Form1.Visible = True
SetWindowPos Form1.hwnd, -1, 0, 0, 0, 0, &H2 Or &H1 '将封面置为最顶层窗体
Else
Form1.Visible = False
Form1.Timer1.Enabled = True
End If
End Sub
Sub Main1()
Dim StartXLSByte, I, J As Long
Dim AppPath, XlsTmpPath As String
Dim myfile As SectionedFile
Dim xlApp As Excel.Application '定义EXCEL类
Dim xlBook As Excel.Workbook '定义工件簿类
Dim xlsheet As Excel.Worksheet '定义工作表类
AppPath = App.Path
XlsTmpPath = GetTempFile() '取得XLS临时文件名(带路径)
If VBA.Right(App.Path, 1) = "\" Then AppPath = VBA.Left(App.Path, VBA.Len(App.Path) - 1)
Open AppPath & "\" & App.EXEName & ".exe" For Binary As #1
ReDim myfile.Files(1)
ReDim myfile.Files(1).Bytes(1 To LOF(1) - EXE_SIZE)
Open XlsTmpPath For Binary As #2
Get #1, EXE_SIZE + 1, myfile.Files(1).Bytes '此处数字要根据EXE实际头文件大小更改设定
Put #2, 1, myfile.Files(1).Bytes
Close #1
Close #2
Set xlApp = CreateObject("Excel.Application") '创建EXCEL应用类
Set xlBook = xlApp.Workbooks.Open(FileName:=XlsTmpPath, password:="ldhyob") '打开EXCEL工作簿,已知该工作簿已加打开口令为ldhyob,若您的工作簿没有口令,则将此参数去掉
'以下星号括起部分代码是往xls里写数据(也可不往工作簿里写数据的方法,而生成txt的方法),作用是保存本exe的绝对路径与临时xls绝对路径,以便于EXE重写更新与临时文件删除
'*****************************************
Set xlsheet = xlBook.Worksheets("temp") '设置活动工作表
xlsheet.Cells(1, 1) = AppPath & "\" & App.EXEName & ".exe" '将该EXE完全路径存在工作表单元格内
xlsheet.Cells(2, 1) = XlsTmpPath '将该EXE本次运行产生XLS临时文件路径存在工作表单元格内
'****************************************
xlApp.Visible = True '设置EXCEL可见
Set xlApp = Nothing '释放xlApp对象
StopTime = 0
Me.Timer1.Enabled = True '启动时钟
End Sub

Private Function GetTempFile() As String '用来产生系统临时文件名
Dim lngRet As Long
Dim strBuffer As String, strTempPath As String
strBuffer = String$(MAX_PATH, 0)
lngRet = GetTempPath(Len(strBuffer), strBuffer)
If lngRet = 0 Then Exit Function
strTempPath = Left$(strBuffer, lngRet)
strBuffer = String$(MAX_PATH, 0)
lngRet = GetTempFileName(strTempPath, "tmp", 0&, strBuffer)
If lngRet = 0 Then Exit Function
lngRet = InStr(1, strBuffer, Chr(0))
If lngRet > 0 Then
GetTempFile = Left$(strBuffer, lngRet - 1)
Else
GetTempFile = strBuffer
End If
End Function

Private Sub Timer1_Timer()
On Error Resume Next
If Command() <> "" Then
If VBA.Dir(Command()) <> "" Then
Kill Command() '删除本次运行遗留的临时XLS文件
Else
End
End If
Else
StopTime = StopTime + 1 '计时累加
If StopTime = 1 Then Unload Me: End '2秒后自动关闭退出
End If
End Sub
5、可保存工程,如取名为“工程1”;

6、进行编译,“文件”-“生成工程1.exe”,此时也可将生成的EXE另外取名,如取名叫abc.exe。

7、查看第6步生成的EXE文件字节大小,并将具体数字记下来,并将VB模块中的“Private Const EXE_SIZE = 81920”保证一致(这里的81920是笔者例子的结果数字,每个人在实际时会有不同)。此例此步很重要,必须要做,该数字在xls文档VBA中还要使用到。

至此,文件头部分已做完,abc.exe文件也已生成于磁盘中。
特别提示:编译前,确保VB里"工程"---"引用"里"Microsoft Excel 9.0 Object Library"前面的勾已打上.
二、xls文档部分操作

1、给工作簿增加一个工作表temp,将其置为隐藏。

2、增加xls文档宏代码以实现文档关闭时EXE数据刷新。

打开xls文档,打开VBE窗口,在ThisWorkBook代码区头部加入以下代码:
Private Const EXE_SIZE = 81920 '此处数字为前面第7步得到的EXE文件字节数
Private Type FileSection
Bytes() As Byte
End Type

在Workbook_BeforeClose事件中加入如下代码(对原有的代码可保留):
Dim myfile As FileSection '定义变量
Dim comc, exec, xlsc As String '定义变量
Application.Visible = False '隐藏EXCEL主窗口
exec = WorkSheets(“temp”).cells(1,1).value
xlsc = WorkSheets(“temp”).cells(2,1).value
comc = exec & " " & xlsc
Open exec For Binary As #1 '打开EXE文件
ReDim myfile.Bytes(1 To EXE_SIZE)
Get #1, 1, myfile.Bytes '取得固有文件头
Close #1
If VBA.Dir(exec) <> "" Then Kill exec
Open exec For Binary As #1 '生成新的EXE文件
Put #1, 1, myfile.Bytes '先写入文件头
Open xlsc For Binary As #2 '打开xls临时文件
ReDim myfile.Bytes(1 To FileLen(xlsc))
Get #2, 1, myfile.Bytes
Put #1, EXE_SIZE + 1, myfile.Bytes '将xls部分追加进EXE
Close #1
Close #2
Application.Quit
Shell comc, vbMinimizedNoFocus ‘删除临时xls文件

3、保存文档,退出,关闭EXCEL。(提示:XLS文档不要在open事件中显示起始窗体,在关闭事件中,最好增加询问用户是否保存的语句)

三、将EXE与XLS结合生成新的EXE

将第一步得到的abc.exe与第二步得到的test.xls(文件名随你愿意取)放到同一目录下,切换到MS-DOS模式,或者在该目录下建立一个批处理文件即bat文件,这样就不用切换到MS-DOS模式下输入DOS命令了。建立bat文件的步骤方法是:鼠标右键“新建”—“文本文档”,输入以下内容:
@echo off
copy /b abc.exe + test.xls main.exe
保存,更改文件名(包括扩展名)为“合并.bat”,然后双击它,不一会就会发现在当前目录下会多出一个EXE文件main.exe,这就是封装成品了,你可以将其重命名为所需要的名称。

好了,大功告成!