编写java整合kettle9.3实现读取表中的数据,生成excel文件的代码
时间: 2023-03-11 13:16:17 浏览: 212
官方kettle最新9.3下载百度云
5星 · 资源好评率100%
可以参考如下代码:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import org.pentaho.di.core.KettleEnvironment;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.trans.Trans;
import org.pentaho.di.trans.TransMeta;
public class KettleExcel {
public static void main(String[] args) {
try {
KettleEnvironment.init(); //初始化Kettle环境
//创建TransMeta实例
TransMeta transMeta = new TransMeta("kettle/table2excel.ktr");
//创建Trans实例
Trans trans = new Trans(transMeta);
//执行转换
trans.execute(null);
//等待转换执行结束
trans.waitUntilFinished();
//把结果输出到文件
if (trans.getErrors() > 0) {
System.out.println("转换出错!");
} else {
String excelName = "test.xls";
File file = new File(excelName);
OutputStream os = new FileOutputStream(file);
InputStream is = trans.getTrans().getTransOutputExcelResultFiles().get(0).getContent();
byte[] b = new byte[1024];
int len;
while ((len = is.read(b)) != -1) {
os.write(b, 0, len);
}
is.close();
os.close();
System.out.println("转换完成!");
}
} catch (KettleException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
阅读全文