这么做的导出可以按照事先准备的excel格式与央视进行导出,看着会舒心一点。并无其他用处。
jsp页面(jeecg框架)
<t:dgToolBar title="政审导出" icon="icon-download" url="" funname="exportFile('${fid}','excel')" ></t:dgToolBar>
js方法
function exportFile(dqjcid,type){
var plansize = $('#exhibitorList').datagrid('getRows').length;
if(plansize == 0){
tip('无数据!');
return;
}
var jcid = $("select[name='forthetimeid'] option:selected").val();
var jcids = "";
if(jcid){
jcids = "&jcid="+jcid;
}else{
jcids = "&jcid="+dqjcid;
}
var params = "";
var url = "";
if(type == 'excel'){
url = "activityplanController.do?exportXlsZS";
}
var ids = [];
var rows = $("#exhibitorList").datagrid('getSelections');
if (rows.length > 0) {
for ( var i = 0; i < rows.length; i++) {
ids.push(rows[i].id);
}
ids = ids.join(',');
params = "&ids="+ids;
window.open(url+encodeURI(params),false);
}else{
tip('请至少选择一条数据!');
}
}
java 后端代码
@RequestMapping(params = "exportXlsZS")
public void exportXlsZS(HttpServletRequest request,
HttpServletResponse response) throws IOException, InvalidFormatException {
String ids = request.getParameter("ids");
if(StringUtils.isNotEmpty(ids)) {
if(ids.contains(",")){
ids = ids.replace(",", "','");
}
}
Session session = sessionFactory.openSession();
String sql = "select A.PK_INFORMATION as id, rownum as num,A.SURNAME_CN as surname,A.GIVENNAME_CN as givenname,"
+ "A.SURNAME_EN as surnameen, A.GIVENNAME_EN as givennameen,A.POSITION as posi,"
+ "A.SEX as sex,A.IDTYPE as idtype,A.IDNUMBER as idnumber,A.WORKPHONE as phone ,"
+ "B.NAME_CN AS company from GH_BFM.bfm_fm_participant A,GH_BFM.BFM_FM_EXHIBITOR B "
+ "WHERE A.REFID=B.PK_INFORMATION and A.REFID IN ('"+ids+"')";
SQLQuery query = session.createSQLQuery(sql.toString()).addEntity(ParinfoVo.class);
List<ParinfoVo> lists = query.list();
List<DictEntity> sexs = systemService.findByProperty(DictEntity.class, "dictname", "sex"); //性别字典
List<DictEntity> idcadtypes = systemService.findByProperty(DictEntity.class, "dictname", "iDTypeDict");//证件类型字典
for (ParinfoVo person : lists) {
for (DictEntity dictEntity : sexs) {
if(person.getSex().equals(dictEntity.getDictkey())) {
person.setSex(dictEntity.getDictvaluecn());
}
}
for (DictEntity dictEntity : idcadtypes) {
if(person.getIdtype().equals(dictEntity.getDictkey())) {
person.setIdtype(dictEntity.getDictvaluecn());
}
}
}
String templetePath = "/export/template/import_userFilmMarket.xls"; //excel模板地址
Map<String, Object> beans = new HashMap<String, Object>();
beans.put("result", lists);
XLSTransformer transfer = new XLSTransformer();
URL url = getClass().getResource(templetePath);
File file = new File(url.getPath());
FileInputStream fileInputStream = new FileInputStream(file);
org.apache.poi.ss.usermodel.Workbook workbook = transfer.transformXLS(fileInputStream, beans);
PropertiesUtil util = new PropertiesUtil("sysConfig.properties");
String strExcelPath = util.readProperty("exportExcelPath");//获取文件导出地址
String dateStr =DateUtils.getDate("yyyyMMddHHmmss") ;
File excelPath = new File(strExcelPath);
if(!excelPath.exists()) {
excelPath.mkdir();
}
FileOutputStream fos = new FileOutputStream("import_userFilmMarket.xls");
workbook.write(fos);
String codedFileName = "systemdata";
response.setHeader("Content-Disposition",
"attachment;filename=" + "import_userFilmMarket" + ".xls");
// 响应类型,编码
response.setContentType("application/octet-stream;charset=UTF-8");
// 形成输出流
OutputStream osOut = response.getOutputStream();
// 将指定的字节写入此输出流
workbook.write(osOut);
// 刷新此输出流并强制将所有缓冲的输出字节被写出
osOut.flush();
// 关闭流
fileInputStream.close();
response.getOutputStream().close();
fos.close();
osOut.close();
File excelFiles = new File(strExcelPath+"/"+dateStr+".xls");
if(excelFiles.exists()) {
excelFiles.delete();
}
}