前端使用vue
前端代码:
添加一个按钮:
<button type="button" class="btn btn-white m-l-md" @click.stop="exportXls()">EXCEL导出</button>
写一段jq:
// 报表导出
exportXls: function () {
if (this.total > 300) {
var _this = this;
parent.layer.confirm('导出的内容超过300条,确定要导出吗?', {
btn: ['是', '否'], //按钮
shade: false //不显示遮罩
}, function () {
parent.layer.msg('正在导出!', { icon: 1 });
window.open(CONFIG["REQUEST_BASE_URL"] + "/xxxxxx?access_token=" + CONFIG['TOKEN'] + _this.export_CELLPHONE + _this.export_USER_NAME +
_this.export_wxid + _this.export_wbid + _this.export_QQ + _this.export_from + _this.export_user_group + _this.export_invitationcode + _this.export_start + _this.export_end);
}, function () {
parent.layer.msg('已取消', { icon: 2 });
});
}
else {
window.open(CONFIG["REQUEST_BASE_URL"] + "/xxxxxxx?access_token=" + CONFIG['TOKEN'] + this.export_CELLPHONE + this.export_USER_NAME +
this.export_wxid + this.export_wbid + this.export_QQ + this.export_from + this.export_user_group + this.export_invitationcode + this.export_start + this.export_end);
}
}
},
后台:
@RequestMapping(value = "/xxxx", method = RequestMethod.GET)
public void exportUserList(TSysUserBankCard tSysUserBankCard,HttpServletResponse response) throws SysException,IOException {
//设置一页300条数据
tSysUserBankCard.setPage(1);
tSysUserBankCard.setRows(300);
//查询列表
List<TSysUserBankCard> tSysUserBankCards = userBankCardListService.selectUserBankCardList(tSysUserBankCard);
//封装需要的表头
Object[] head = {表头内容};
List<List<Object>> dataList = new ArrayList<List<Object>>();
//表头转换为list
List<Object> headList = Arrays.asList(head);
List<Object> rowList = null;
for(TSysUserBankCard s :tSysUserBankCards)
{
rowList = new ArrayList<Object>();
rowList.add(s.getId());
//添加在rowlist
dataList.add(rowList);
}
String filename = java.util.UUID.randomUUID().toString();
//调用cvs工具类
File csvFile = CSVUtils.createCSVFile(headList, dataList, tmppath, filename);
//文件名称
String date = "银行卡信息列表" + DateUtils.milliSecondToDateStr(new Date().getTime(), "yyyy-MM-dd");
response.setHeader("Content-disposition", "attachment; filename=" + new String(date.getBytes(), "iso-8859-1") + ".csv");
response.setHeader("Content-Length", String.valueOf(csvFile.length()));
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
bis = new BufferedInputStream(new FileInputStream(csvFile));
bos = new BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[2048];
while (true) {
int bytesRead;
if (-1 == (bytesRead = bis.read(buff, 0, buff.length))) break;
bos.write(buff, 0, bytesRead);
}
bis.close();
bos.close();
}
cvs工具类:
public static File createCSVFile(List<Object> head, List<List<Object>> dataList,
String outPutPath, String filename) {
File csvFile = null;
BufferedWriter csvWtriter = null;
try {
csvFile = new File(outPutPath + File.separator + filename + ".csv");
File parent = csvFile.getParentFile();
if (parent != null && !parent.exists()) {
parent.mkdirs();
}
csvFile.createNewFile();
// GB2312使正确读取分隔符","
csvWtriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(
csvFile), "GB2312"), 1024);
// 写入文件头部
writeRow(head, csvWtriter);
// 写入文件内容
for (List<Object> row : dataList) {
writeRow(row, csvWtriter);
}
csvWtriter.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
csvWtriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return csvFile;
}
比较敏感的东西被xxx和汉字代替自行修改