基于SSM的酒店后台管理系统

本文介绍了使用SSM框架(SpringMVC+MyBatis)开发的一个Java项目,涉及文件上传、数据库操作以及Excel数据导出功能,包括Home和Vip实体类的处理以及通过HSSFWorkbook生成Excel表格的代码片段。
摘要由CSDN通过智能技术生成

项目所用技术

SSM+layui框架

项目截图

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

部分源码

 @Autowired
            HomeServiceImpl homeService;

    @RequestMapping("/add")
    public String add(Home home, Model model) throws IOException{

        String sqlPath = null;
        //定义文件保存的本地路径
        String localPath="E:\\Java 项目\\Hotel_Manage\\src\\main\\webapp\\upload";
        //定义 文件名
        String filename=null;
        if(!home.getFile().isEmpty()){
            //生成uuid作为文件名称
            String uuid = UUID.randomUUID().toString().replaceAll("-","");
            //获得文件类型(可以判断如果不是图片,禁止上传)
            String contentType=home.getFile().getContentType();
            //获得文件后缀名
            String suffixName=contentType.substring(contentType.indexOf("/")+1);
            //得到 文件名
            filename=uuid+"."+suffixName;
            System.out.println(filename);
            //文件保存路径
            home.getFile().transferTo(new File(localPath+filename));
        }
        //把图片的相对路径保存至数据库
        sqlPath = "/upload/"+filename;
        System.out.println(sqlPath);
        home.setImg(sqlPath);

        homeService.addHome(home);
        model.addAttribute("home",home);
        return "home_show";
    }
    <insert id="addHome" parameterType="Home">
        insert into home (Num,H_Type,Price,State,Img,Text)
         values (#{Num},#{H_Type},#{Price},#{State},#{Img},#{Text});
    </insert>
   @RequestMapping("/home")
    public void excel_home(HttpServletResponse response )throws IOException {

        response.setCharacterEncoding("UTF-8");
        List<Home> homeList=homeService.queryAllHome();
        //创建excel文件
        HSSFWorkbook wb = new HSSFWorkbook();
        //创建sheet页
        HSSFSheet sheet = wb.createSheet("房间信息");
        //创建标题行
        HSSFRow titleRow = sheet.createRow(0);
        titleRow.createCell(0).setCellValue("编号");
        titleRow.createCell(1).setCellValue("房间号");
        titleRow.createCell(2).setCellValue("房间类型");
        titleRow.createCell(3).setCellValue("价格");
        titleRow.createCell(4).setCellValue("状态");
        titleRow.createCell(5).setCellValue("描述");

        for(Home home:homeList){
            HSSFRow dataRow = sheet.createRow(sheet.getLastRowNum()+1);
            dataRow.createCell(0).setCellValue(home.getId());
            dataRow.createCell(1).setCellValue(home.getNum());
            dataRow.createCell(2).setCellValue(home.getH_Type());
            dataRow.createCell(3).setCellValue(home.getPrice());
            dataRow.createCell(4).setCellValue(home.getState());
            dataRow.createCell(5).setCellValue(home.getText());
        }

        // 设置下载时客户端Excel的名称
        response.setContentType("application/octet-stream;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename="
                + new String("房间信息表".getBytes(),"iso-8859-1") + ".xls");

        OutputStream ouputStream = response.getOutputStream();
        wb.write(ouputStream);
        ouputStream.flush();
        ouputStream.close();

    }

    @RequestMapping("/vip")
    public void excel_vip(HttpServletResponse response )throws IOException {

        response.setCharacterEncoding("UTF-8");
        List<Vip> vipList=vipService.queryAllVip();
        //创建excel文件
        HSSFWorkbook wb = new HSSFWorkbook();
        //创建sheet页
        HSSFSheet sheet = wb.createSheet("会员信息");
        //创建标题行
        HSSFRow titleRow = sheet.createRow(0);
        titleRow.createCell(0).setCellValue("编号");
        titleRow.createCell(1).setCellValue("姓名");
        titleRow.createCell(2).setCellValue("性别");
        titleRow.createCell(3).setCellValue("身份证号");
        titleRow.createCell(4).setCellValue("手机号");
        titleRow.createCell(5).setCellValue("会员类型");
        titleRow.createCell(6).setCellValue("开通时间");
        titleRow.createCell(7).setCellValue("到期时间");

        for(Vip vip:vipList){
            HSSFRow dataRow = sheet.createRow(sheet.getLastRowNum()+1);
            dataRow.createCell(0).setCellValue(vip.getId());
            dataRow.createCell(1).setCellValue(vip.getName());
            dataRow.createCell(2).setCellValue(vip.getSex());
            dataRow.createCell(3).setCellValue(vip.getCard());
            dataRow.createCell(4).setCellValue(vip.getPhone());
            dataRow.createCell(5).setCellValue(vip.getV_Type());
            dataRow.createCell(6).setCellValue(vip.getStartTime());
            dataRow.createCell(7).setCellValue(vip.getEndTime());
        }

        // 设置下载时客户端Excel的名称
        response.setContentType("application/octet-stream;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename="
                + new String("客户会员名单".getBytes(),"iso-8859-1") + ".xls");

        OutputStream ouputStream = response.getOutputStream();
        wb.write(ouputStream);
        ouputStream.flush();
        ouputStream.close();

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值