package zxc.util.ieport.poi;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import org.springframework.util.CachingMapDecorator;
import zxc.exceptions.BaseRuntimeException;
import zxc.model.support.FileBaseBean;
import zxc.model.support.IeudBean;
import zxc.util.ieport.IExport;
/**
* 这是一个组件工作
*
* @author KHT
*
*/
public class POIExcelFactory {
private static POIExcelFactory excelFactory = new POIExcelFactory();
public static final String DEFAULT_EXPORT_KEY = "DEFAULT_EXPORT_KEY";
@SuppressWarnings("unchecked")
private Map<String, IExport> exportContainer = new CachingMapDecorator() {
@Override
protected Object create(Object key) {
return new DefaultExcelExport();
}
};
protected POIExcelFactory() {
super();
}
public static POIExcelFactory getFactory() {
return excelFactory;
}
public IExport getDefaultExport() {
return this.getExport(DEFAULT_EXPORT_KEY);
}
public IExport getExport(String exportKey) {
return this.exportContainer.get(exportKey);
}
public void processExcelExport(FileBaseBean model) {
//需要导出
IeudBean ieudBean = model.getIeudBean();
if (ieudBean.getExport() != null && ieudBean.getExport()) {
POIWorkContext workContext = POIWorkContext.getCurrentContext();
workContext.initWorkContext(model);
this.getDefaultExport().exportData(workContext.getCurrentListData());
//如果采用一次性导出,或者在多次导出的情况下,处于完成状态时,则做最后处理
if (ieudBean.getOneTimeExport() || ieudBean.getExportCompleted()) {
ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream(4069);
try {
workContext.getCurrentWorkbook().write(arrayOutputStream);
} catch (IOException e) {
throw new BaseRuntimeException("导出文件失败");
}
ieudBean.setDownLoadStream(new ByteArrayInputStream(arrayOutputStream.toByteArray()));
ieudBean.setContentType("application/vnd.ms-excel");
ieudBean.setContentDisposition("risen-zxc-" + new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss").format(new Date()) + ".xls");
workContext.clearCurrentContext();//清理资源
}
}
}
}