在jsp中点击下载按钮,从数据库中下载数据库信息

jsp中function:

function DownloadButton(){
	$('form[id=userForm]').attr('action',ctx+'/user/getFormFile.do');	
	$("#userForm").submit();
}

controller:

	/**
	 * 该方法用于结账订单的下载
	 * @param request
	 * @param response
	 * @param queryVo
	 * @throws IOException 
	 */
	@RequestMapping(value="/getFormFile")
	public void getFormFile(HttpServletRequest request,Model model,
			HttpServletResponse response, QueryRecord queryRecord) throws IOException{
		OutputStream outputStream = null;
		try {
			request.setCharacterEncoding("UTF-8");
			response.reset();
			ReportFileReturnVo returnVo =new ReportFileReturnVo();
				returnVo = checkOutService
						.getFormFile(queryRecord);

			response.setContentType("application/vnd.ms-excel; charset=utf-8");
			response.setHeader("Content-Disposition", "attachment; filename="
					+ URLEncoder.encode(returnVo.getFileName(), "UTF-8"));

			outputStream = response.getOutputStream();
			outputStream.write(returnVo.getBytes());
			outputStream.flush();
		} catch (Exception e) {
		} finally {
			outputStream.close();
		}
}

serviceImpl:

	@Override
	public ReportFileReturnVo getFormFile(QueryRecord queryRecord) {
		
			List<CheckOutMessage> detailVoList =checkOutDao.findCheckOutRecord(queryRecord);
			Workbook wb = new HSSFWorkbook();
			
			Sheet sheet1 = (Sheet) wb.createSheet("sheet1");
			sheet1.setColumnWidth(0,3000);
			sheet1.setColumnWidth(1,3000);
			sheet1.setColumnWidth(2,6000);
			sheet1.setColumnWidth(3,6000);
			sheet1.setColumnWidth(4,3000);
			sheet1.setColumnWidth(5,3000);
			Row row = (Row) sheet1.createRow(0); // 第一行为标题
			Cell cell0 = row.createCell(0);
			Cell cell1 = row.createCell(1);
			Cell cell2 = row.createCell(2);
			Cell cell3 = row.createCell(3);
			Cell cell4 = row.createCell(4);
			Cell cell5 = row.createCell(5);
			cell0.setCellValue("入住订单");
			cell1.setCellValue("入住房间");
			cell2.setCellValue("入住时间");
			cell3.setCellValue("结账时间");
			cell4.setCellValue("结账房费");
			cell5.setCellValue("房费统计");

			for (int i = 0;i<detailVoList.size();i++) {
				CheckOutMessage returnVo = detailVoList.get(i);
				 
					Row rowTmp = (Row) sheet1.createRow(i + 1);
				
					Cell c1 = rowTmp.createCell(0);
					c1.setCellValue(returnVo.getOrdernumber());

					Cell c2 = rowTmp.createCell(1);
					c2.setCellValue((returnVo.getRoomnumber()));

					Cell c3 = rowTmp.createCell(2);
					c3.setCellValue(DateUtil.formatDate(returnVo.getCheckintime(),
							DateUtil.DATEFORMAT_FULL));
					
					Cell c4 = rowTmp.createCell(3);
					c4.setCellValue(DateUtil.formatDate(returnVo.getCheckouttime(),
							DateUtil.DATEFORMAT_FULL));
					
					Cell c5 = rowTmp.createCell(4);
					c5.setCellValue(returnVo.getNeedmoney());
					
					Cell c6 = rowTmp.createCell(5);
					c6.setCellValue(returnVo.getAllmoney());
				
			}
			String fileName="default.xls";
			if(queryRecord.getStartTime()==null&&queryRecord.getEndTime()==null){
				fileName="all.xls";
			}else if(queryRecord.getStartTime()==null&&queryRecord.getEndTime()!=null){
				fileName="_"+DateUtil.formatDate(queryRecord.getEndTime(),
						DateUtil.DATEFORMAT_DATE10) + ".xls";
			}else if(queryRecord.getStartTime()!=null&&queryRecord.getEndTime()==null){
				fileName=DateUtil.formatDate(queryRecord.getStartTime(),
						DateUtil.DATEFORMAT_DATE10)
				+ "_.xls";
			}else{	
			fileName = 
					 DateUtil.formatDate(queryRecord.getStartTime(),
							DateUtil.DATEFORMAT_DATE10)
					+ "_"
					+ DateUtil.formatDate(queryRecord.getEndTime(),
							DateUtil.DATEFORMAT_DATE10) + ".xls";
			}
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			try {
				wb.write(baos);
			} catch (IOException e) {
				e.printStackTrace();
			}
			byte[] bytes = baos.toByteArray();
			ReportFileReturnVo returnVo = new ReportFileReturnVo();
			returnVo.setBytes(bytes);
			returnVo.setFileName(fileName);
			return returnVo;
	}

DateUtil:

package cxc.hotel.other;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
	public static final String DATEFORMAT_FULL = "yyyy-MM-dd HH:mm:ss";

	public static final String DATEFORMAT_DATE6 = "yyyyMM";

	public static final String DATEFORMAT_DATE7 = "yyyy-MM";

	public static final String DATEFORMAT_DATE8 = "yyyyMMdd";

	public static final String DATEFORMAT_DATE10 = "yyyy-MM-dd";

	public static final String DATEFORMAT_DATETIME14 = "yyyyMMddHHmmss";

	public static final String DATEFORMAT_DATETIME16 = "yyyy-MM-dd HH:mm";

	public static final String DATEFORMAT_TIME2 = "HH";

	public static final String DATEFORMAT_TIME5 = "mm:ss";

	public static final String DATEFORMAT_TIME8 = "HH:mm:ss";

	/**
	 * 使用默认格式格式化日期
	 * 
	 * @param date
	 * @return
	 */
	public static String formatDate(Date date) {
		SimpleDateFormat fm = new SimpleDateFormat(DateUtil.DATEFORMAT_FULL);

		return fm.format(date);
	}

	/**
	 * 使用指定格式格式化日期
	 * 
	 * @param date
	 * @param formatStr
	 * @return
	 */
	public static String formatDate(Date date, String formatStr) {
		SimpleDateFormat fm = new SimpleDateFormat(formatStr);

		return fm.format(date);
	}

	/**
	 * 根据date和date字符串格式的长度动态选择格式化类型,只处理三种格式化类型
	 * 
	 * @param date
	 * @param dateStrLength
	 * @return
	 */
	public static String formatDateByType(Date date, int dateStrLength) {
		SimpleDateFormat fm = new SimpleDateFormat(DateUtil.DATEFORMAT_FULL);

		if (dateStrLength <= DateUtil.DATEFORMAT_TIME8.length()) {
			fm = new SimpleDateFormat(DateUtil.DATEFORMAT_TIME8);
		} else if (dateStrLength <= DateUtil.DATEFORMAT_DATE10.length()) {
			fm = new SimpleDateFormat(DateUtil.DATEFORMAT_DATE10);
		}

		return fm.format(date);
	}

	/**
	 * 使用默认格式转换日期
	 * 
	 * @param date
	 * @return
	 */
	public static Date toDate(String date) {
		SimpleDateFormat fm = new SimpleDateFormat(DateUtil.DATEFORMAT_FULL);
		fm.setLenient(false);

		try {
			return fm.parse(date);
		} catch (ParseException e) {
			throw new RuntimeException("日期转换出错", e);
		}
	}

	/**
	 * 使用指定格式转换日期
	 * 
	 * @param date
	 * @param formatStr
	 * @return
	 */
	public static Date toDate(String date, String formatStr) {
		SimpleDateFormat fm = new SimpleDateFormat(formatStr);
		fm.setLenient(false);

		try {
			return fm.parse(date);
		} catch (ParseException e) {
			throw new RuntimeException("日期转换出错", e);
		}
	}

	/**
	 * 根据date字符串的长度动态选择格式化类型,只处理三种格式化类型
	 * 
	 * @param date
	 */
	public static Date toDateByType(String date) {
		if (date.length() <= DateUtil.DATEFORMAT_TIME8.length()) {
			return DateUtil.toDate(date, DateUtil.DATEFORMAT_TIME8);
		} else if (date.length() <= DateUtil.DATEFORMAT_DATE10.length()) {
			return DateUtil.toDate(date, DateUtil.DATEFORMAT_DATE10);
		} else {
			return DateUtil.toDate(date);
		}
	}

	/**
	 * 
	 * @param date
	 * @return
	 */
	public static Calendar toCalendar(Date date) {
		if (date == null)
			return null;

		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);

		return calendar;
	}

	/**
	 * 两个时间相差多少分钟(date1-date2)
	 * 
	 * @param date1
	 * @param date2
	 * @return
	 *//*
	public static Integer subtractDate(Date date1, Date date2) {
		if (null == date1)
			return null;
		if (null == date2)
			return null;

		DateTime dt1 = new DateTime(date1);
		DateTime dt2 = new DateTime(date2);

		return Minutes.minutesBetween(dt1, dt2).getMinutes();
	}

	*//**
	 * 两个时间相差的天数(date1-date2)
	 * 
	 * @param date1
	 * @param date2
	 * @return
	 *//*
	public static Integer daysBetween(Date date1, Date date2) {
		if (null == date1)
			return null;
		if (null == date2)
			return null;

		DateTime dt1 = new DateTime(date1);
		DateTime dt2 = new DateTime(date2);

		return Days.daysBetween(dt1, dt2).getDays();
	}
*/
	/**
	 * 添加指定的小时数
	 * 
	 * @param srcDate
	 * @param hours
	 *            需要添加的小时数,可以为负数
	 * @return 重新生成的日期对象
	 */
	public static Date addHours(Date srcDate, int hours) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(srcDate);
		calendar.add(Calendar.HOUR, hours);

		return calendar.getTime();
	}

	/**
	 * 添加指定的天数
	 * 
	 * @param srcDate
	 * @param days
	 *            需要添加的天数,可以为负数
	 * @return 重新生成的日期对象
	 */
	public static Date addDays(Date srcDate, int days) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(srcDate);
		calendar.add(Calendar.DAY_OF_YEAR, days);

		return calendar.getTime();
	}

	/**
	 * 添加指定的年份数
	 * 
	 * @param srcDate
	 * @param days
	 *            需要添加的年份数,可以为负数
	 * @return 重新生成的日期对象
	 */
	public static Date addYears(Date srcDate, int years) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(srcDate);
		calendar.add(Calendar.YEAR, years);

		return calendar.getTime();
	}

	/**
	 * 通过结束时间返回开始时间 根据相差的天数
	 * 
	 * @param en
	 * @param subDay
	 * @return
	 */
	@Deprecated
	public static Date getOtherDay(Date en, Long subDay) {
		return new Date(en.getTime() - subDay * 24 * 60 * 60 * 1000);
	}
}

效果:


猜你喜欢

转载自blog.csdn.net/qq_39986274/article/details/80022675