java poi生成excel(个人例子js-jsp-java)

js代码:

function exportRepQudl(){
if(confirm("导出输出页面内容?")){
var id = $("input[name='id']").val();
var lx = $("input[name='lx']").val();
var rq = $("input[name='rq']").val();
var url = __ctx+'/zjdlbb/zjdlbb/zjdlbb/exportExcelQudl.ht?id='+id+"&lx="+lx+"&rq="+rq;
var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
if (userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1) {//判断是否IE浏览器
window.location.href(url);
}else{
window.open(url,"导出报表");
}
}
}

 

jsp代码:

<a class="myLinkA" id="exportRepQudl" οnclick="exportRepQudl()"><span></span>导出</a>

java代码:

@RequestMapping({ "exportExcelQudl" })
@Action(description = "导出excel")
public void exportExcelQudl(HttpServletRequest request,
HttpServletResponse response) throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

// 获取请求参数的各种处理
String lx = request.getParameter("lx");
String rq = request.getParameter("rq");

String rqTime = "";
if (StringUtil.isEmpty(rq)) {
rq = sdf.format(new Date());
rqTime = sdf.format(new Date()) + " 22:00:00";
} else {
rqTime = rq + " 22:00:00";
}

// 取得各种需要导出的数据
Map<String, String> mapXzz = new HashMap<String, String>();
mapXzz.put("rq", rq);
mapXzz.put("rqTime", rqTime);
List<Zjdlbbxzb> listXzz = zjdlbbxzbService.getQqdl(mapXzz);

// 第一步、设置文件名,类型+日期+后缀.xls
String fileName = lx + rq + ".xls";;
// 第二步,创建excel
HSSFWorkbook wb = new HSSFWorkbook();
// 第三步、创建excel sheet
HSSFSheet sheet = wb.createSheet();
// 第四步、设置数据



//添加行的指标
int flagIndex = 0;
// 第一行操作
HSSFRow row0 = sheet.createRow(flagIndex++);
row0.setHeightInPoints((short)20);
//添加合并区域(合并第三到第五列)
sheet.addMergedRegion(new CellRangeAddress(0,0,2,4));
HSSFCell titleCell = row0.createCell(2);
titleCell.setCellValue(""+rq);

//标题样式
HSSFCellStyle cellStyleTitle = wb.createCellStyle();
cellStyleTitle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中
HSSFFont font = wb.createFont();
font.setFontHeightInPoints((short) 14);
cellStyleTitle.setFont(font);
titleCell.setCellStyle(cellStyleTitle);


//第二行操作
List<String> listTitle = zjdlbbService.getQutlTitle();
HSSFRow row1 = sheet.createRow(flagIndex++);
//样式
HSSFCellStyle cellStyle = ZjdlbbUtil.getHSSFCellStyle(wb);
for (int i = 0; i < listTitle.size(); i++) {
HSSFCell title = row1.createCell(i);
title.setCellValue(listTitle.get(i));
title.setCellStyle(cellStyle);
//设置每一列的宽度
sheet.setColumnWidth(i,5000);
}

// 其他行操作
for (int i = 0; i < listXzz.size(); i++) {
HSSFRow row = sheet.createRow(flagIndex++);
HSSFCell qy = row.createCell(0);
HSSFCell ygdl = row.createCell(1);
HSSFCell wgdl = row.createCell(2);
HSSFCell zgfh = row.createCell(3);
HSSFCell pjfh = row.createCell(4);
HSSFCell fhl = row.createCell(5);
HSSFCell glys = row.createCell(6);

Zjdlbbxzb entity = listXzz.get(i);
qy.setCellValue(entity.getLx()==null?"":entity.getLx());
ygdl.setCellValue(entity.getYgzdl()==null?0d:entity.getYgzdl());
wgdl.setCellValue(entity.getWgzdl()==null?0d:entity.getWgzdl());
zgfh.setCellValue(entity.getZgfh()==null?0d:entity.getZgfh());
pjfh.setCellValue(entity.getPjfh()==null?0d:entity.getPjfh());
fhl.setCellValue(entity.getFhl()==null?0d:entity.getFhl());
glys.setCellValue(entity.getGlys()==null?0d:entity.getGlys());

}

response.setContentType("application/octet-stream;charset=UTF-8");
response.setHeader("Content-Type", "application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;filename="
+ new String(fileName.getBytes("GB2312"), "8859_1"));
response.addHeader("Pargam", "no-cache");
response.addHeader("Cache-Control", "no-cache");
OutputStream out = response.getOutputStream();
wb.write(out);
out.flush();
out.close();
}

转载于:https://www.cnblogs.com/rdchen/p/9723484.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值