java代码导出excel
Control.java
@SuppressWarnings("deprecation")
@RequestMapping(value = "ddosAttackReport_Download.do")
public void Download(HttpServletRequest request,HttpServletResponse response,PagedRequest pagedRequest ,String ids ,Long corpid ,Long task ){
//先将数据查询出来
Map params = new HashMap<>();
params.put("task", task);
if (Validators.isNotnull(getCustomContext(request))) {
if(Validators.isNull(task)){
if(Validators.isNotnull(getCustomContext(request).getCorp())){
corpid = getCustomContext(request).getCorp().getCorpid();
}
}
params.put("corpid", corpid);
} else {
params.put("corpid", corpid);
}
if(Validators.isNotnull(task) && task == 1L){
Map previousMondayToSunday = DateUtils.getPreviousMondayToSunday();
Date beginTime = previousMondayToSunday.get("monday");
Date endTime = previousMondayToSunday.get("sunday");
params.put("begintime", beginTime);
params.put("endtime", endTime);
} else {
task = null;
Date begintime = DateUtils.str2Date(request.getParameter("begintime"),DateUtils.datetimeFormat);
params.put("begintime", begintime);
Date endtime = DateUtils.str2Date(request.getParameter("endtime"),DateUtils.datetimeFormat);
params.put("endtime", endtime);
params.put("attacktype", request.getParameter("attacktype"));
params.put("dstip", request.getParameter("dstip"));
params.put("receivebits", request.getParameter("receivebits"));
params.put("symbol", request.getParameter("symbol"));
}
List daeids = null;
if(Validators.isNotnull(ids)){
daeids = new ArrayList();
String[] cids = ids.split(",");
for (String id : cids) {
if(id != null){
long l = Long.parseLong(id);
daeids.add(l);
}
}
}
params.put("daeids", daeids);
PagedResult
Service.java
public PagedResult> getDdosAttackReportDownload( PagedRequest pagedRequest, Map params);
ServiceImpl.java
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public PagedResult> getDdosAttackReportDownload(
PagedRequest pagedRequest, Map params) {
List list = ddosAttackReportMapper.getDdosAttackReportList(params);
return BeanUtil.toPagedResult(list);
}
dao.java
public List getDdosAttackReportDownload(Mapparams);
dao.xml
页面按钮.jsp
//导出
function Batch() {
var dstip = $("#dstip").val();
var begintime = $("#begintime").val();
var endtime = $("#endtime").val();
var attacktype = $('#attacktype').val();
var symbol = $('#symbol').val();
var receivebits = $('#receivebits').val();
var urlj = '&dstip='+dstip
+'&begintime='+begintime
+'&endtime='+endtime
+'&attacktype='+attacktype
+'&symbol='+symbol
+'&receivebits='+receivebits;
var url = path+'/front/flowclearing/ddosAttackReport/ddosAttackReport_Download.do?corpid=${corpid}&task=${task}'+urlj;
window.location.href = url;
}
设置excel样式.txt(需要的时候加)
// 第一步,创建一个webbook,对应一个Excel文件
HSSFWorkbook wb = new HSSFWorkbook();
// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet("搞笑");
// 第四步,创建单元格,并设置值表头 设置表头居中
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); //设置垂直对齐的样式为居中对齐;
//创建单元格(excel的单元格,参数为列索引,可以是0~255之间的任何一个
HSSFCell cell=row1.createCell(0);
//设置单元格内容
cell.setCellValue("详细攻击情况统计");
cell.setCellStyle(style);
//设置Excel中的边框(表头的边框)
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM);
style.setBottomBorderColor(HSSFColor.BLACK.index);
style.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM);
style.setLeftBorderColor(HSSFColor.BLACK.index);
style.setBorderRight(HSSFCellStyle.BORDER_MEDIUM);
style.setRightBorderColor(HSSFColor.BLACK.index);
style.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);
style.setTopBorderColor(HSSFColor.BLACK.index);
//设置字体
HSSFFont font = wb.createFont();
font.setFontHeightInPoints((short) 16); // 字体高度
font.setFontName("黑体"); // 字体
style.setFont(font);