Java调用VBA XLS转换XLS

 

Rem http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.xlfileformat.aspx

Set objArgs = WScript.Arguments
FullName = objArgs(0)
FileName = Left(FullName, InstrRev(FullName, ".")-1)
Set objExcel = CreateObject("Excel.application")


objExcel.application.visible=false
objExcel.application.displayalerts=false
set objExcelBook = objExcel.Workbooks.Open(FullName)
objExcelBook.SaveAs FileName & "(SysAutoConv)",51

objExcel.Application.Quit
objExcel.Quit   




Set objExcel = Nothing
set objExcelBook = Nothing

 

 

 

 

import java.io.IOException;

/**
 *
 * @author lidongbo
 * @date 2012-10-15
 */
public class VBAUtils {

	public static final void xls2xlsx(String filePath) throws IOException{
		//TODO:测试
		Runtime.getRuntime().exec("cscript \"" + "C:\\aaa\\xls2xlsx.vbs\" \"" + filePath + "\"");
	}
	
	public static void main(String[] args) throws IOException {
		
		xls2xlsx("C:\\aaa\\aaa.xls");
	}
}

 

 

 

如果出现以下异常

Microsoft Office Excel 不能访问文件“XXXxls”。 可能的原因有:? 文件名称或路径不存在。? 文件正被其他程序使用。 ? 您正要保存的工作簿与当前打开的工作簿同名

 

解决办法如下

?Windows 2008 Server x64 
  Please make this folder.

  C:\Windows\SysWOW64\config\systemprofile\Desktop

?Windows 2008 Server x86

  Please make this folder.

  C:\Windows\System32\config\systemprofile\Desktop

  ...instead of dcomcnfg.exe.

This operation took away office automation problems in my system.

A Desktop folder seems to be necessary in the systemprofile folder to open file by Excel.

It disappears from Windows2008, Windows2003 had the folder, 
and I think it cause this error.

 

在C:\Windows\System32\config\systemprofile和C:\Windows\SysWOW64\config\systemprofile目录下创建名为Desktop目录即可解决问题。

 

你可能感兴趣的:(java)