Eclipse RCP入门(七)BIRT2.2.2导出EXCEL
BIRT导出为HTML和PDF,倒是比较简单,BIRT上有支持。以前的代码中简单的更换一个
HTMLRenderOption和PDFRenderOption就可以了。
但是在导出EXCEL的时候,遇到不少问题。参考了一些文章,大部分是基于BIRT2.2.1的。
其实在2.2.2中已经集成了EXCEL的导出了。可以做得更简单一些。
首先我去下载了BIRT2.2.2的源代码来做为参考,下载的CVS地址如下:
Host dev.eclipse.org
Repository path /cvsroot/birt
User anonymous
Connection type pserver
Use Default Port Selected
我下载过来的版本是Versions里面的source里面的2.2.2的最近的Release版本
由于没有CSVRenderOption类,所以我自己新建了一个CSVRenderOption.java:
package com.sillycat.birt;
import org.eclipse.birt.report.engine.api.IRenderOption;
import org.eclipse.birt.report.engine.api.RenderOption;
public class CSVRenderOption extends RenderOption {
public static final String OUTPUT_FORMAT_CSV = "xls";
public CSVRenderOption() {
super();
}
public CSVRenderOption(IRenderOption options) {
super(options);
}
}
在报表引擎那里这样调用的:
IRenderOption pdfOptions;
pdfOptions = new PDFRenderOption();
pdfOptions.setOutputFormat(PDFRenderOption.OUTPUT_FORMAT_PDF);
pdfOptions.setOutputFileName("D:/birt/test.pdf");
task.setRenderOption(pdfOptions);
task.run();
// HTML
IRenderOption htmlOptions;
htmlOptions = new HTMLRenderOption();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
htmlOptions.setOutputStream(bos);
htmlOptions.setOutputFormat(HTMLRenderOption.OUTPUT_FORMAT_HTML);
task.setRenderOption(htmlOptions);
task.run();
browser.setText(bos.toString());
// CSV
IRenderOption csvOptions;
csvOptions = new CSVRenderOption();
csvOptions.setOutputFileName("D:/birt/test.xls");
csvOptions.setOutputFormat(CSVRenderOption.OUTPUT_FORMAT_CSV);
task.setRenderOption(csvOptions);
task.run();
// destroy the task
task.close();
engine.destroy();
这样会分别导出为Excel文件,pdf文件,同时用HTML的形式显示在browser上。
同时在plugin.xml和sillycatGen.product上面都要添加上这个包:
org.eclipse.birt.report.engine.emitter.prototype.excel