通过vb代码将多个excel合并成一个

代码如下,可以新建一个wsf文件,在java中调用。

<job id="copysheet">
<reference guid="{00020905-0000-0000-C000-000000000046}" comment="MSWord 2000 tag library, under HKEY_CLASSES_ROOT\TypeLib\"/>
<script language="VBScript">

Option Explicit
Dim hexDict,i,j,sheetIndex
hexDict = "0123456789ABCDEF"

If WScript.Arguments.Count < 2 Then
		MsgBox "Usage: copySheet..."
		WScript.Quit
End If

Dim ExlApp 
Dim ExlBook 
Dim ExlBook2 
Dim ExlSheet 


Dim excel_path,excelFile
excel_path=WScript.Arguments(0)
excelFile=WScript.Arguments(1)

Set ExlApp = CreateObject("Excel.Application") 
If Err.Number <> 0 Then 
	Set ExlApp = GetObject("Excel.Application") 
End If 
Dim fs, f, f1, fc
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(excel_path) '在括号内输入你指定的目录
Set fc = f.Files


Set ExlBook2 = ExlApp.Workbooks.Add 
ExlBook2.SaveAs excelFile 
j = 1

For Each f1 In fc
	'打开被合并工作簿
	sheetIndex = 1
	Set ExlBook= ExlApp.Workbooks.Open(f1) 
	Dim ws
	For Each ws In ExlBook.Sheets
		Set ExlSheet = ExlBook.Worksheets(sheetIndex) 
		ExlApp.DisplayAlerts = False 
		ExlSheet.Copy  ExlBook2.Sheets(j)
		j = j + 1
		sheetIndex = sheetIndex + 1
	Next
	ExlBook.Close
	'f1.delete
Next

For i = ExlBook2.Sheets.Count To j Step -1
	ExlBook2.Sheets(i).Delete 
Next 
	
ExlBook2.Save 
ExlApp.Quit 
Set ExlSheet = Nothing 
Set ExlBook2 = Nothing 
Set ExlBook = Nothing 
Set ExlApp = Nothing 
</script>
</job>

你可能感兴趣的:(java,excel,vba)