美文网首页Code
Java项目导出excel

Java项目导出excel

作者: 月夜骄阳 | 来源:发表于2017-06-15 16:07 被阅读0次

    项目框架:SpringMVC Spring Mybatis
    权限框架:Shiro
    一、创建对应的formbean 不是model,里面属性为需要导出的属性,这里我只导出编码和名称

    public class StoreBillInfoForm extends BaseForm {
        private static final long serialVersionUID = 1L;
    
        private String store_no;                                     编码
        private String store_name;                                   名称
    
        public String getStore_no() {
            return store_no;
        }
        public void setStore_no(String store_no) {
            this.store_no = store_no;
        }
        public String getStore_name() {
            return store_name;
        }
        public void setStore_name(String store_name) {
            this.store_name = store_name;
        }
    }
    

    二、点击导出按钮,跳转到action对应的方法
    findShopBillInfoByCondition方法要在service实习 和 在对应的xml里面写,将在下面写上。

    @Controller 
    public class StoreBillInfoAction extends BaseManageAction {
        private final String FILE_PATH = this.getClass().getClassLoader().getResource("").getPath();
    
        @RequestMapping(value="/control/store/billinfo/export")
        public void export(StoreBillInfoForm formbean, HttpServletResponse response) throws IOException{
            Condition condition = new Condition();
    
            QueryResult<StoreBillInfo> queryresult = to(StoreBillInfoService.class).findShopBillInfoByCondition(condition);
             String fileName = "供应商编码名称表(" + DateTimeUtils.fullStrFormat() + ").xls";            //导出excel文件名称
             List<StoreBillInfo> list = queryresult.getResultList();
             if(list != null && !list.isEmpty()){
                 BillSheet.billInfoExport(FILE_PATH, list, formbean, fileName, response);
             }
        }
    }
    

    三、同formbean一样 只写需要导出的属性

    public class StoreBillInfo extends AbstractEntity{
        private static final long serialVersionUID = 1L;
    
        private String store_no;
        private String store_name;
    
        public String getStore_no() {
            return store_no;
        }
        public void setStore_no(String store_no) {
            this.store_no = store_no;
        }
        public String getStore_name() {
            return store_name;
        }
        public void setStore_name(String store_name) {
            this.store_name = store_name;
        }
    }
    

    上面model对应的配置文件 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="com.ikcsoft.service.store.impl.StoreBillInfoServiceImpl">
        <resultMap id="entityResultMap" type="com.ikcsoft.model.store.StoreBillInfo">
            <result property="store_no" column="store_no" />
            <result property="store_name" column="store_name" />
        </resultMap>
        
        <!-- 导出商家编号  商家名 -->
        <select id="findShopBillInfoByCondition" parameterType="com.ikcsoft.utils.Condition" 
                resultType="com.ikcsoft.model.store.StoreBillInfo">
            SELECT  store_no,store_name FROM gl_store
            <if test="queryCondition !=null and queryCondition != ''" >
            ${queryCondition}
            </if>
        </select>
    
        <!-- 导出商家编号  商家名 -->
        <select id="findStoreBillInfoTotalRecord" parameterType="com.ikcsoft.utils.Condition" resultType="long">
            SELECT COUNT(*)  FROM gl_store
            <if test="queryCondition !=null and queryCondition != ''" >
            ${queryCondition}
            </if>
        </select>
    </mapper>
    

    四、第二步里面最后的方法 BillSheet.billInfoExport

    public static void billInfoExport(String path, List<StoreBillInfo> list, StoreBillInfoForm formbean, String fileName, HttpServletResponse response){
            
            try {
                String templateName = "xls/storebilllist.xls";         // 这个excel文件是导出excel的模板,需要放在指定位置,比如说第二行第一列写  编码  第二列写名称
                InputStream is = new FileInputStream(path + templateName);
                
                HSSFWorkbook workbook = new HSSFWorkbook(is);// 创建 一个excel文档对象  
                
                HSSFSheet sheet = workbook.getSheetAt(0);
                
                sheet.setDefaultColumnWidth(15);
                sheet.getRow(0).createCell(1).setCellValue(DateTimeUtils.fullFormat()); // 第一行第一列 设置一个时间
            
                if(list != null && !list.isEmpty()){
                    for (int i = 0; i < list.size(); i ++) {
                        StoreBillInfo storeBillInfo = list.get(i);
                        HSSFRow sheetRow = sheet.createRow(i + 2);// 创建一个行对象         从第3行开始 跳过第1行第2行
                        sheetRow.createCell(0).setCellValue(storeBillInfo.getStore_no()); 
                        sheetRow.createCell(1).setCellValue(storeBillInfo.getStore_name()); 
                    }
                }
                response.reset();
                response.setHeader("Content-Disposition", "attachment;fileName= " + new String(fileName.getBytes("GBK"),"ISO8859-1"));
                response.setContentType("application/x-download");
                
                OutputStream outExcel = response.getOutputStream();
                workbook.write(outExcel);
                outExcel.close();
                workbook.close();
            } catch (Exception e) {
                logger.error("导出供应商编码名称:", e);
            }   
        }
    }
    

    相关文章

      网友评论

        本文标题:Java项目导出excel

        本文链接:https://www.haomeiwen.com/subject/twquqxtx.html