java可以调用wsf文件,通过vba代码来将多个excel合成为一个,代码如下。
vba代码可以参考以下路径:
vba合成excel代码
// wsf文件路径
String wsf_path = "D:\1\combineExcel.wsf"
// 首先将所有需要合成的excel放到combineFilePath路径上
String combineFilePath = "D:\test\combine\";
// excelPatn是合成excel的文件路径
String excelPath = "D:\test2\combineFile.xlsx";
String[] paras = new String[] { combineFilePath, excelPath };
Process process = combineUtil.runWsf(wsf_path, paras);
process.waitFor();
combineUtil如下:
public class CombineUtil{
/**
* 将wsf下载到temp目录
* @param class1 当前class所在路径
* @param fileName wsf文件名
* @param path 需要生成的wsf所在路径
* @return
* @throws Exception
*/
public static String getFileFromClass(Class<?> class1, String fileName,
String path) throws Exception {
String filePath = path + "\\" + fileName;
File file = new File(filePath);
if (file.exists()) {
file.delete();
}
file.createNewFile();
InputStream inputStream = class1.getResourceAsStream(fileName);
OutputStream os = new BufferedOutputStream(new FileOutputStream(
filePath));
int readLen = 0;
byte[] buf = new byte[1];
while ((readLen = inputStream.read(buf, 0, 1)) != -1) {
os.write(buf, 0, readLen);
}
inputStream.close();
os.flush();
os.close();
return filePath;
}
/**
* 运行wsf
* @param wsfPath wsf路径
* @param paras 运行wsf给的参数
* @return
*/
public static Process runWsf(String wsfPath, String[] paras) {
Process proc = null;
try {
Vector<String> cmdVector = new Vector<String>();
cmdVector.add("wscript");
cmdVector.add(wsfPath);
for (String para : paras) {
cmdVector.add(para);
}
String[] cmd = cmdVector.toArray(new String[cmdVector
.size()]);
Runtime runtime = Runtime.getRuntime();
proc = runtime.exec(cmd);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return proc;
}
}