Java ssh poi 根据条件查询数据导出excel到浏览器 下载框不显示 浏览器乱码 之前是用submit()

    1.我需要做的功能是根据条件查询然后的数据创建成excel。然后导出excel并且下载到浏览器

      excel类似的模型:

      

    最终的效果:点击保存弹出下载框

        


2.然后我感觉我做好了。进行了测试。而且后台没有报错,到了前台乱码,并且下载的保存框没有显示出来。

         效果:

 3.进入分析阶段  

  然后就进入了找问题阶段了。我要知道我到底是在哪里错误了。

  首先我要确定是我这个excel生成是否正确?验证:把excel保存到本地磁盘上面。excel正确

  那到底是什么造成的乱码呢?

  一开始以为是response.setContentType里面写错了。那这个到底要怎么测试呢? 一直在改。一直都是一样的效果。

  因为我以前也做过这种功能。我用的是springmvc做的。一样的代码。那到底是哪里出现问题呢?

  然后我觉得应该是我前台点击导出的时候那段代码有问题?

    这个是我点击导出时候的js,因为我是要根据form表单查询查询数据的

  function exportExcel(){
    var url = "team/flightAction!exportExcel.action" ;
    $("#flightForm").attr("action", url);
    $("#flightForm").submit();
  }
  那我要怎么确定是这里错了。我有直接在浏览器请求导出excel的action。弹出下载框

  那么现在问题确定。我知道是这里有问题。那我需要怎么修改呢?我怎么吧我需要按照条件查询的数据传递过去呢?

  修改代码:

  

 function exportExcel(){
   var url = "team/flightAction!exportExcel.action?" +$("#flightForm").formSerialize() ;
   url = decodeURIComponent(url,true);
   url = encodeURI(encodeURI(url));
   window.location.href=url;
 }

如果不了解formSerialize()方法的话。可以百度一下。我以前也没有接触过这个方法

4.代码

  action中代码:

 public void exportExcel(){
    page=vLineService.queryVLine(this.getPage(), this.getParameter());//这个是根据条件查询出list数据
    ServletOutputStream ouputStream= null;//创建一个输出流对象
    try {
        HSSFWorkbook wb=ExcelUtil.filghtTicket(page.getItems()) ;response = ServletActionContext.getResponse();
	response.setContentType("application/binary;charset=utf-8");  
	String headerStr =new String("订单".getBytes("utf-8"), "ISO8859-1");//headerString为中文时转码
	response.setHeader("Content-disposition","attachment; filename="+headerStr+".xls");//filename是下载的xls的名
	ouputStream = response.getOutputStream();  
	wb.write(ouputStream);
	ouputStream.flush();  
	ouputStream.close();
     } catch (IOException e) {
	e.printStackTrace();
     }
 }
 ExcelUtil.java
public class ExcelUtil {
	//表格头数组
	static String[] excelHeader={"序列号","航段","航空公司","航班号","日期段","天数","折扣","底价含T","销售价","航空公司总机位","总机位","现收","客户","联系方式","预留","预留实收","客户","余位","今日新增","总实出","利润","操作人","操作时间","备注","时限","大PNR","小PNR","订单号","级数"};
	/**
	 * 导出查询数据的excel方法
	 * @author ljr
	 * @param listVO 需要导出的数据
	 *        listVO.orders=List<TmOrderVO> orders 订单列表
	 * @return HSSFWorkbook
	 */
	public static HSSFWorkbook filghtTicket(List <VTmLineVO> listVO){
		SimpleDateFormat sf=new SimpleDateFormat("yyyy-mm-dd");
		//创建一个webbook,对应一个Excel文件  
		HSSFWorkbook wb=new HSSFWorkbook();
		//在webbook中添加一个sheet
		HSSFSheet sheet=wb.createSheet("天天机位数据");
		//在sheet中添加表头第0行
		HSSFRow row=sheet.createRow(0);
		HSSFCellStyle headerStyle=wb.createCellStyle();
		headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//设置自动居中
	        //设置单元格的背景颜色为淡蓝色  
		headerStyle.setFillForegroundColor(HSSFColor.PALE_BLUE.index);  
		headerStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);  
                // 设置单元格居中对齐  
		headerStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);  
                // 设置单元格垂直居中对齐  
		headerStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);   
                // 设置单元格字体样式  
		HSSFFont font = wb.createFont();  
                // 设置字体加粗  
               font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);  
               font.setFontName("宋体");  
               font.setFontHeight((short) 200);  
               headerStyle.setFont(font);  
               // 设置单元格边框为细线条  
               headerStyle.setBorderLeft(XSSFCellStyle.BORDER_THIN);  
               headerStyle.setBorderBottom(XSSFCellStyle.BORDER_THIN);  
               headerStyle.setBorderRight(XSSFCellStyle.BORDER_THIN);  
               headerStyle.setBorderTop(XSSFCellStyle.BORDER_THIN);  
        
		HSSFCellStyle itemStyle=wb.createCellStyle();
		
		HSSFCellStyle itemStyleRow=wb.createCellStyle();
		itemStyleRow.setWrapText(true);   
		
		HSSFCellStyle orderStyle=wb.createCellStyle();
		orderStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//设置自动居中
	
		//添加表格头里面的每一列的值
		for(int i=0;i<excelHeader.length;i++){
			//创建标题列的值
			HSSFCell cell=row.createCell(i);
			cell.setCellValue(excelHeader[i]);
			cell.setCellStyle(headerStyle);
			sheet.autoSizeColumn(i);
		}
		
		
		//添加表格里面的值
		if(listVO!=null&& listVO.size()>0){

			Integer rowNum=0;
			for(int i=0;i<listVO.size();i++){
				rowNum=rowNum+1;
				VTmLineVO vo=listVO.get(i);
				row=sheet.createRow(rowNum);
				//编号
				HSSFCell cid=row.createCell(0);
				cid.setCellValue(vo.getId());
				cid.setCellStyle(itemStyle);
				//航段
				String flights="";
				HSSFCell cflights=row.createCell(1);
				if(vo.getFlights()!=null && vo.getFlights().size()>0){
					int in=0;
					for(FlightVO flight:vo.getFlights()){
						in=in+1;
						String n=vo.getFlights().size()==in?"":"   ";
						flights+=flight.getStartName()+"-"+flight.getEndName()+n;
					}
				}
				cflights.setCellValue(new HSSFRichTextString(flights));
				cflights.setCellStyle(itemStyleRow);
				//航空公司
				HSSFCell cairlineCompany=row.createCell(2);
				cairlineCompany.setCellValue(vo.getAirlineCompany());
				cairlineCompany.setCellStyle(itemStyle);
				//航班号
				HSSFCell cflightNumber=row.createCell(3);
				cflightNumber.setCellValue(vo.getFlightNumber());
				cflightNumber.setCellStyle(itemStyle);
				//日期段
				HSSFCell cairDate=row.createCell(4);
				cairDate.setCellValue(sf.format(vo.getAirDate()));
				cairDate.setCellStyle(itemStyle);
				//天数
				HSSFCell cdays=row.createCell(5);
				cdays.setCellValue(vo.getDays());
				cdays.setCellStyle(itemStyle);
				//折扣
				HSSFCell cdiscount=row.createCell(6);
				cdiscount.setCellValue(vo.getDiscount());
				cdiscount.setCellStyle(itemStyle);
				//底价含T
				HSSFCell cfloorPrice=row.createCell(7);
				cfloorPrice.setCellValue(vo.getFloorPrice());
				cfloorPrice.setCellStyle(itemStyle);
				//销售价
				HSSFCell cprice=row.createCell(8);
				cprice.setCellValue(vo.getPrice());
				cprice.setCellStyle(itemStyle);
				//航空总机位
				HSSFCell ctotalAirlines=row.createCell(9);
				ctotalAirlines.setCellValue(vo.getTotalAirlines());
				ctotalAirlines.setCellStyle(itemStyle);
				//总机位
				HSSFCell ctotalQuantity=row.createCell(10);
				ctotalQuantity
				.setCellValue(vo.getTotalQuantity());
				ctotalQuantity.setCellStyle(itemStyle);
				//现收
				HSSFCell cnowPaid=row.createCell(11);
				cnowPaid.setCellValue(vo.getNowPaid());
				cnowPaid.setCellStyle(itemStyle);
				//客户
				HSSFCell kehu1=row.createCell(12);
				kehu1.setCellValue("");
				kehu1.setCellStyle(itemStyle);
				//联系方式
				HSSFCell clxfs=row.createCell(13);
				clxfs.setCellValue("");
				clxfs.setCellStyle(itemStyle);
				//预留
				HSSFCell creserveNumber=row.createCell(14);
				creserveNumber.setCellValue(vo.getReserveNumber());
				creserveNumber.setCellStyle(itemStyle);
				//预留实收
				HSSFCell ctotalReceipts=row.createCell(15);
				ctotalReceipts.setCellValue(vo.getTotalReceipts());
				ctotalReceipts.setCellStyle(itemStyle);
				//客户
				HSSFCell kehu2=row.createCell(16);
				kehu2.setCellValue("");
				kehu2.setCellStyle(itemStyle);
				//余位
				HSSFCell ctotalRemainder=row.createCell(17);
				ctotalRemainder.setCellValue(vo.getTotalRemainder());
				ctotalRemainder.setCellStyle(itemStyle);
				//今日新增
				HSSFCell cdayadd=row.createCell(18);
				cdayadd.setCellValue("");
				cdayadd.setCellStyle(itemStyle);
				//总实出
				HSSFCell ctotalActualReceipt=row.createCell(19);
				ctotalActualReceipt.setCellValue(vo.getTotalActualReceipt());
				ctotalActualReceipt.setCellStyle(itemStyle);
				//利润
				HSSFCell ctotalProfits=row.createCell(20);
				ctotalProfits.setCellValue(vo.getTotalProfits());
				ctotalProfits.setCellStyle(itemStyle);
				//操作人
				HSSFCell coperator=row.createCell(21);
				coperator.setCellValue(vo.getOperator());
				coperator.setCellStyle(itemStyle);
				//操作时间
				HSSFCell cupdates=row.createCell(22);
				cupdates.setCellValue(sf.format(vo.getUpdates()));
				cupdates.setCellStyle(itemStyle);
				//备注
				HSSFCell cbz=row.createCell(23);
				cbz.setCellValue("");
				cbz.setCellStyle(itemStyle);
				//时限
				HSSFCell cshixian=row.createCell(24);
				cshixian.setCellValue("");
				cshixian.setCellStyle(itemStyle);
				//大PNR
				HSSFCell cdpnr=row.createCell(25);
				cdpnr.setCellValue("");
				cdpnr.setCellStyle(itemStyle);
				//小PNR
				HSSFCell cxpnr=row.createCell(26);
				cxpnr.setCellValue("");
				cxpnr.setCellStyle(itemStyle);
				//订单号
				HSSFCell corderNo=row.createCell(27);
				corderNo.setCellValue("");
				corderNo.setCellStyle(itemStyle);
				//级数
				HSSFCell cjs=row.createCell(28);
				cjs.setCellValue("");
				cjs.setCellStyle(itemStyle);
				
			}
		}
		return wb;
	}

完成。编辑好。下班

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值