• 023医疗项目-模块二:药品目录的导入导出-从数据库中查出数据用XSSF导出excel并存放在虚拟目录最后下载(包括调试)


    我们要实现的效果:


     
     
    进入到这个页面后,输入要查询的条件,查询出药品表的数据,然后按下导出按钮 ,就会在服务器的一个目录下生成一个药品表的excel表格。
     点击”导出”之后:
    点击下载就能下载Excel表格,实现导出功能。
     
     
     
    讲解如下:
     
    还是分为三层:
    1:Dao层
     
     
    我们先看sql语句。
    在PL/SQL写sql:
    select 
     id,
     bm,
     mc,
     jx,
     gg,
     zhxs,
     scqymc,
     spmc,jyzt,
     zbjg,
     (select info from dictinfo where dictinfo.typecode='003'and dictinfo.dictcode=ypxx.jyzt )jyztmc//药品的使用状态
      from ypxx
    结果:

    我们要查的药品的表格里面包含了药品的使用状态(使用,停用),所以不单单是对药品表的查询,也包含了别的表,所以。我们的po类是一个自定义的类:YpxxMaapaerCustom.java:这个类继承于Ypxx.java这个单表的po类。

    package yycg.business.pojo.vo;
    import yycg.business.pojo.po.Ypxx;
    /**
     * 
    * @ClassName: YpxxCustom
    * @Description: 从数据库中查出来的数据放在这里。也就是自定义的pojo类
    * @author A18ccms a18ccms_gmail_com
    * @date 2016年11月23日 上午12:00:19
    *
     */
    public class YpxxCustom extends Ypxx{
        
        private String jyztmc;
    
        public String getJyztmc() {
            return jyztmc;
        }
    
        public void setJyztmc(String jyztmc) {
            this.jyztmc = jyztmc;
        }
        
    
    }
     接下来我们写
     YpxxMapperCustom.xml:
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
    <mapper namespace="yycg.business.dao.mapper.YpxxMapperCustom" >
    <!-- 药品目录查询条件 -->
     <sql id="query_ypxx_where">
     <if test="ypxxCustom!=null">
               <!-- 药品的流水号 -->
                <if test="ypxxCustom.bm!=null and ypxxCustom.bm!=''">
              and ypxx.bm = #{ypxxCustom.bm}
               </if>
               <!-- 药品的通用名 -->
            <if test="ypxxCustom.mc!=null and ypxxCustom.mc!=''">
                    and ypxx.mc = #{ypxxCustom.mc}
                </if>
               <!-- 药品的剂型-->
                <if test="ypxxCustom.jx!=null and ypxxCustom.jx!=''">
               
               and ypxx.jx like '%${ypxxCustom.jx}%'
               
               </if>
            <!-- 药品的类别-->
                <if test="ypxxCustom.lb!=null and ypxxCustom.lb!=''">
               
                  and ypxx.lb = #{ypxxCustom.lb}
               
               </if>  
                <!-- 药品的启用停止-->
                <if test="ypxxCustom.jyzt!=null and ypxxCustom.jyzt!=''">
               
                  and ypxx.jyzt = #{ypxxCustom.jyzt}
               
               </if>  
               
    </if>
     </sql>
    <!-- 药品目录查询 
    这个yycg.business.pojo.vo.YpxxQueryVo是包装类,从Action传数据时用的。

    --> <select id="findYpxxList" parameterType="yycg.business.pojo.vo.YpxxQueryVo" resultType="yycg.business.pojo.vo.YpxxCustom"> select id, bm, mc, jx, gg, zhxs, scqymc, spmc,jyzt, zbjg, (select info from dictinfo where dictinfo.typecode='003'and dictinfo.dictcode=ypxx.jyzt )jyztmc from ypxx <where> <include refid="query_ypxx_where"></include> </where> </select> </mapper>

    接下来写Mapper接口:

    package yycg.business.dao.mapper;
    
    import java.util.List;
    
    import org.apache.ibatis.annotations.Param;
    
    import yycg.business.pojo.po.Ypxx;
    import yycg.business.pojo.po.YpxxExample;
    import yycg.business.pojo.vo.YpxxCustom;
    import yycg.business.pojo.vo.YpxxQueryVo;
    /*
     * Dao层
     */
    public interface YpxxMapperCustom {
    /*
     * 查找药品的数据
     */
    public List<YpxxCustom> findYpxxList(YpxxQueryVo ypxxQueryVo); }

    接下来写Service层:

    我们直接看实现类:

    import java.util.List;
    
    import org.springframework.beans.factory.annotation.Autowired;
    
    import yycg.business.dao.mapper.YpxxMapperCustom;
    import yycg.business.pojo.vo.YpxxCustom;
    import yycg.business.pojo.vo.YpxxQueryVo;
    import yycg.business.service.YpxxService;
    
    public class YpxxServiceImpl implements YpxxService {
    @Autowired
        YpxxMapperCustom ypxxMapperCustom;
        @Override
        public List<YpxxCustom> findYpxxList(YpxxQueryVo ypxxQueryVo) {
            return     ypxxMapperCustom.findYpxxList(ypxxQueryVo);
            
        }
    
    }

    我们看Action层:

    package yycg.business.action;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    import yycg.base.pojo.po.Dictinfo;
    import yycg.base.process.context.Config;
    import yycg.base.process.result.ResultUtil;
    import yycg.base.process.result.SubmitResultInfo;
    import yycg.base.service.SystemConfigService;
    import yycg.business.pojo.vo.YpxxCustom;
    import yycg.business.pojo.vo.YpxxQueryVo;
    import yycg.business.service.YpxxService;
    import yycg.util.ExcelExportSXXSSF;
    import yycg.util.Ypxx;
    
    @Controller
    @RequestMapping("/ypml")
    public class YpxxAction {
    
        @Autowired
        YpxxService ypxxService;
        @Autowired
        private SystemConfigService systemConfigService;
        
        
        //导出页面的展示
        
        @RequestMapping("/exportYpxx")
        public String exPortYpxx(Model model) throws Exception
        {
        /**
         * 这些数据查出来后填充到查询页面上。
         */
            List<Dictinfo> yplblist=systemConfigService.findDictinfoByType("001");
            model.addAttribute("yplblist", yplblist);
            
            List<Dictinfo> jyztlist=systemConfigService.findDictinfoByType("003");
            model.addAttribute("jyztlist", jyztlist);
            
            return "/business/ypml/exportYpxx";
        }
        
        
        
        //导出提交
        @RequestMapping("/exportYpxxSubmit")
        public @ResponseBody SubmitResultInfo exPortYpxxsubmit(YpxxQueryVo ypxxQueryVo)throws Exception
        {
            //查询到数据
        List<YpxxCustom> list=ypxxService.findYpxxList(ypxxQueryVo);
            
        /**            导出文件存放物理路径
         * @param fileWebPath
         *            导出文件web下载路径
         * @param filePrefix
         *            导出文件名的前缀          
         * @param flushRows
         *            存放在内存的数据量
         * @param fieldNames
         *            导出文件列标题
         * @param fieldCodes
         *               导出数据对象的字段名称     
         * @param flushRows*/
        //导出文件存放的路径,并且是虚拟目录指向的路径
        String filePath = "d:/upload/linshi/";
        //导出文件的前缀
        String filePrefix="ypxx";
        //-1表示关闭自动刷新,手动控制写磁盘的时机,其它数据表示多少数据在内存保存,超过的则写入磁盘
        int flushRows=100;
        
        //指导导出数据的title
        List<String> fieldNames=new ArrayList<String>();
        fieldNames.add("流水号");
        fieldNames.add("通用名");
        fieldNames.add("剂型");
        fieldNames.add("规格");
        fieldNames.add("转换系数 ");
        fieldNames.add("生产企业");
        fieldNames.add("商品名称");
        fieldNames.add("中标价");
        fieldNames.add("交易状态");
    
        //告诉导出类数据list中对象的属性,让ExcelExportSXXSSF通过反射获取对象的值
        List<String> fieldCodes=new ArrayList<String>();
        fieldCodes.add("bm");//药品流水号
        fieldCodes.add("mc");//通用名
        fieldCodes.add("jx");
        fieldCodes.add("gg");
        fieldCodes.add("zhxs");
        fieldCodes.add("scqymc");
        fieldCodes.add("spmc");
        fieldCodes.add("zbjg");
        fieldCodes.add("jyztmc");
        
        
        //注意:fieldCodes和fieldNames个数必须相同且属性和title顺序一一对应,这样title和内容才一一对应
        
        
        //开始导出,执行一些workbook及sheet等对象的初始创建,以及表格建在哪里
        //"/upload/"本来是服务器的磁盘目录,但是我们是一台机器做开发,所以见建立一个虚拟目录。
        ExcelExportSXXSSF excelExportSXXSSF = ExcelExportSXXSSF.start(filePath, "/upload/", filePrefix, fieldNames, fieldCodes, flushRows);
        
        //准备导出的数据,将数据存入list,且list中对象的字段名称必须是刚才传入ExcelExportSXXSSF的名称
    
        
        //执行导出,把数据导入到excel表
        excelExportSXXSSF.writeDatasByObject(list);
    
            /*
             * new Object[]{list.size(),excelExportSXXSSF.exportFile()}参数:一共导出的数据数量,.exportFile()导出文件。
             */
            return ResultUtil.createSubmitResult(ResultUtil.createSuccess(Config.MESSAGE, 313, new Object[]{list.size(),excelExportSXXSSF.exportFile()}));
            
        }
        
        
        
        
        
        
    }

    写页面调试:

    修改menu.json:

    {
       "menus" : [{"icon" : "icon-sys","menuid" : "1","menuname" : "系统管理","url" : "","menus" : [
                     {"icon" : "icon-log","menuid" : "1_1","menuname" : "用户管理","url" : "/yycgproject/user/queryuser.action"
                     }] 
                   },
                   {"icon" : "icon-sys","menuid" : "1","menuname" : "药品目录 ","url" : "","menus" : [
                     {"icon" : "icon-log","menuid" : "1_1","menuname" : "药品目录导出","url" : "/yycgproject/ypml/exportYpxx.action"
                     }] 
                   }
                 ]
    }

    就会出现:

    然后点击“药品目录导出”进入到YpxxAction.java中的@RequestMapping("/exportYpxx")public String exPortYpxx(Model model) throws Exception然后

    进入到exportYpxx.jsp页面:

    exportYpxx.jsp这个页面:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <%@ page contentType="text/html; charset=UTF-8"%>
    <%@ include file="/WEB-INF/jsp/base/tag.jsp"%>
    <html> 
    <head>
    <title>药品目录导出</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    
    <%@ include file="/WEB-INF/jsp/base/common_css.jsp"%>
    <%@ include file="/WEB-INF/jsp/base/common_js.jsp"%>
    
    <script type="text/javascript">
    //药品信息导出
    function ypxxexport(){
        //调用ajax Form提交
        jquerySubByFId('ypxxlistFrom',ypxxExprot_callback,null,"json");
        
    }
    function ypxxExprot_callback(data){
        
        //在这里提示信息中有文件下载链接
        message_alert(data);
        
    }
    </script>
    
    </head>
    <body>
    <!-- 导出条件 -->
    
    <form id="ypxxlistFrom" name="ypxxlistFrom" action="${baseurl}ypml/exportYpxxSubmit.action" method="post">
    <TABLE  class="table_search">
                    <TBODY>
                        <TR>
                            <TD class="left">流水号:</TD>
                            <td ><INPUT type="text" name="ypxxCustom.bm" /></td>
                            <TD class="left">通用名:</td>
                            <td><INPUT type="text"  name="ypxxCustom.mc" /></TD>
                            
                            <TD class="left">药品类别:</TD>
                            <td >
                                <select id="ypxxCustom.lb" name="ypxxCustom.lb" style="150px">
                                    <option value="">全部</option>
                                    <c:forEach items="${yplblist}" var="value">
                                        <option value="${value.id}">${value.info}</option>
                                    </c:forEach>
                                </select>
                            </td>
                            <TD class="left">交易状态:</TD>
                            <td >
                                <select id="ypxxCustom.jyzt" name="ypxxCustom.jyzt" style="150px">
                                    <option value="">全部</option>
                                    <c:forEach items="${jyztlist}" var="value">
                                        <option value="${value.dictcode}">${value.info}</option>
                                    </c:forEach>
                                </select>
                                
                            </td>
                        </TR>
                        <tr>
                          <td colspan="12" style="text-align:center"><a id="btn" href="#" onclick="ypxxexport()" class="easyui-linkbutton" iconCls='icon-search'>导出</a></td>
                        </tr>
                    </TBODY>
                </TABLE>
                
    </form>
    
    
    </body>
    </html>
     点击导出进入到YpxxAction.java中的
     @RequestMapping("/exportYpxxSubmit")
        public @ResponseBody SubmitResultInfo exPortYpxxsubmit。
    解释一下这个函数中的 //"/upload/"本来是服务器的磁盘目录,但是我们是一台机器做开发,所以见建立一个虚拟目录。 ExcelExportSXXSSF excelExportSXXSSF = ExcelExportSXXSSF.start(filePath, "/upload/", filePrefix, fieldNames, fieldCodes, flushRows);
     
    就是说我们用eclipse建立一个虚拟的目录,生成的Excel表格就放在那里,在实现

    点击“点击下载”之后,下载excel表格。

     
    我们讲一下怎么建立虚拟目录:
    双击两次:

    进入到:

    这样就可以了。

    调试:成功。

    这样,我们的导出功能就做好了。

     
     
  • 相关阅读:
    MySQL的存储引擎
    MySQL的索引及执行计划
    MySQL的SQL基础应用
    MySQL基础入门
    代码质量检测SonarQube
    Jenkins持续集成
    Git版本控制及gitlab私有仓库
    jumpserver跳板机
    Keepalived高可用服务
    well-known file is not secure
  • 原文地址:https://www.cnblogs.com/shenxiaoquan/p/6100187.html
Copyright © 2020-2023  润新知