Skip to content

如何生成Excel文件范例

张弓 edited this page Feb 19, 2021 · 1 revision

如何生成Excel文件范例:

第一步,在需要显示导出按钮的画面添加:

ExportFile export = new ExportFile(app.getService(), app.getExportKey());
bottom.addMenu("导出(Excel)", String.format("FrmSample.exportExcel?service=%s&exportKey=%s",
		export.getService(), export.getKey()));

第二步,编写对应的FrmSample文件:

public IPage exportExcel() throws WriteException, IOException, AccreditException {
	ExportExcel export = new ExportExcel(getResponse());
	export.setHandle(this);
	export.setTemplateId("FrmSample");
	String service = getRequest().getParameter("service");
	String exportKey = getRequest().getParameter("exportKey");
	LocalService app = new LocalService(this);
	app.setService(service);
	try (MemoryBuffer buff = new MemoryBuffer(BufferType.getExportKey, this.getUserCode(), exportKey)) {
		app.getDataIn().close();
		app.getDataIn().setJSON(buff.getString("data"));
	}
	if (app.exec()) {
		export.getTemplate().setDataSet(app.getDataOut());
		export.export();
	} else
		export.export(app.getMessage());
	return null;
}

第三步,编写配置文件 export-excel.xml:

<bean id="FrmSample" class="cn.cerc.jexport.excel.Template"
	scope="prototype">
	<property name="fileName" value="Excel导出范例" />
	<property name="columns">
		<list>
			<bean class="cn.cerc.jexport.excel.StringColumn">
				<property name="code" value="Code_" />
				<property name="name" value="代码" />
			</bean>
			<bean class="cn.cerc.jexport.excel.StringColumn">
				<property name="code" value="Name_" />
				<property name="name" value="名称" />
			</bean>
		</list>
	</property>
</bean>