1.一开始用ajax请求,但无法弹出下载框。
function exportMrvBunkerApproval(form)
{
var selectIDs = getSelectIDs(form);
if(selectIDs.length==0)
{
alert("Please select the record!");
return;
}
$.ajax({
url : Systempath+"/servlet/mrvTankerServlet?_actionType=exportMrvBunkerApproval",
data: {
selectIDs: selectIDs+""
},
dataType : "JSON",
type : "post",
success : function(data) {
alert("Operation Success!");
RefreshDocument();
},
error : function() {
alert("Operation Fail!");
}
});
}
2.直接URL打开请求。因为URL传值的长度有限制,所以此方法只适用于传递少量参数值的情况,对于要传大量参数值不适用。
function exportMrvBunkerApproval(form)
{
var selectIDs = getSelectIDs(form);
if(selectIDs.length==0)
{
alert("Please select the record!");
return;
}
var url = Systempath+"/servlet/mrvTankerServlet?_actionType=exportMrvBunkerApproval&selectIDs="+selectIDs;
window.open(url);
}
3.构建一个form,用post 方式提交参数。此方法可适用传大量参数值。
function exportMrvBunkerApproval(form)
{
var selectIDs = getSelectIDs(form);
if(selectIDs.length==0)
{
alert("Please select the record!");
return;
}
var url = Systempath+"/servlet/mrvTankerServlet?_actionType=exportMrvBunkerApproval";
$('<form method="post" action="' + url + '"><input type="text" name="selectIDs" value="'+selectIDs+'"/></form>').appendTo('body').submit().remove();
}
后台代码:
/**
*
* <p>
* 单船分组查询导出数据到excel
* </p>
* @param request
* @param response
* @throws Exception
*/
public void exportMrvBunkerApproval(final HttpServletRequest request,final HttpServletResponse response) throws Exception
{
response.reset();
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
InputStream path = this.getClass().getResourceAsStream("templet.xls");
POIFSFileSystem pos = new POIFSFileSystem(path);
HSSFWorkbook wb = new HSSFWorkbook(pos); //拿到模版 开始数据和样式封装
HSSFSheet sheet = wb.getSheetAt(0);
HSSFCellStyle style1 = getHSSFCellStyle(wb,1);//普通,居左
HSSFCellStyle style2 = getHSSFCellStyle(wb,2);//普通、2位小数 水平居右
HSSFCellStyle style3 = getHSSFCellStyle(wb,3);//大标题
HSSFCellStyle style4 = getHSSFCellStyle(wb,4);//表头
HSSFCellStyle style5 = getHSSFCellStyle(wb,5);//背景色,加粗,水平居中
HSSFCellStyle style6 = getHSSFCellStyle(wb,6);//背景色,加粗,水平居右
HSSFCellStyle style7 = getHSSFCellStyle(wb,7);//总计行,加粗,水平居左
HSSFCellStyle style8 = getHSSFCellStyle(wb,8);//普通,居中
HSSFCellStyle style9 = getHSSFCellStyle(wb,9);//普通,居右
//获取数据
String selectIDs = request.getParameter("selectIDs");
Map params = new HashMap();
params.put("selectIDs", selectIDs);
MrvTankerHelper helper = new MrvTankerHelper();
params.put("selectIDs", selectIDs);
List<MrvBunkerApproval> list = helper.getMrvBunkerApprovalList(params);
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
String pattern="###,##0.000";
DecimalFormat df = new DecimalFormat(pattern);
int row = 0;
HSSFRow rowt = sheet.createRow(row);
HSSFCell cell = rowt.createCell((short)0);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellStyle(style3);
cell.setCellValue("加油审核数据");
row = 1;
String[] header = new String[]{"BDN_Number","Bunker_Delivery_Date","IMO","Fuel_Type","Sulphur Content%","Mass"};
for(int i=0,l = header.length;i<l;i++)
{
rowt = sheet.createRow(row);
cell = rowt.createCell((short)i);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellStyle(style4);
cell.setCellValue(header[i]);
}
sheet.addMergedRegion(new Region(0,(short)0,0,(short)(header.length-1)));
row = 2;
for(int i=0;i<list.size();i++){
int col = 0;
MrvBunkerApproval bean = list.get(i);
rowt = sheet.createRow(row);
evaluAtion(rowt,style1,bean.getBDN(),col++);
evaluAtion(rowt,style1,bean.getPURCHASEDATE()==null?"":sdf.format(bean.getPURCHASEDATE()),col++);
evaluAtion(rowt,style1,bean.getIMO(),col++);
String oilType = "";
if("FO_H".equals(bean.getOILTYPE()))
{
oilType = "HFO";
}else if("FO_L".equals(bean.getOILTYPE()))
{
oilType = "LFO";
}else if("DO_H".equals(bean.getOILTYPE()))
{
oilType = "HDO";
}else if("DO_L".equals(bean.getOILTYPE()))
{
oilType = "LDO";
}
evaluAtion(rowt,style8,oilType,col++);
evaluAtion(rowt,style9,df.format(bean.getSULPHUR()),col++);
evaluAtion(rowt,style2,Double.valueOf(bean.getQTY()),col++);
row++;
}
String fileName = URLEncoder.encode("加油审核数据", "UTF-8");
response.setHeader("Content-Disposition", "attachment; filename=" + fileName + ".xls");//设定输出文件头
response.setContentType("application/vnd.ms-excel;charset=utf-8");// // 指定文件的保存类型。
response.setCharacterEncoding("utf-8");
ServletOutputStream sos = null;
try {
sos = response.getOutputStream();
wb.write(sos);
sos.flush();
}catch (Exception e) {
e.printStackTrace();
}finally {
try {
sos.close();
} catch (IOException e) {
}
}
}