java调用vba代码合成Excel

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;
	}
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值