码迷,mamicode.com
首页 > 编程语言 > 详细

java 导入导出excel

时间:2014-10-23 15:49:49      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   color   io   os   ar   使用   java   

一,导出excel

1,使用excel模板

public void exportLog() throws Exception{
        SystemUser currentUsr = getCurrentSystemUser();
        
        //该用户的所有日志
        List<TLogInfo> loglist=logService.getLogInfosByUserId(currentUsr.getId());
        
        List<TLogInfo> list=new ArrayList<TLogInfo>();
        if(loglist!=null && loglist.size()>0){
            for(TLogInfo log:loglist){
                TLogInfo export=new TLogInfo();
                export.setProName(log.getProName());
                export.setLogTitle(log.getLogTitle());
                export.setLogType(log.getLogType());
                export.setWorkTime(log.getWorkTime());
                export.setLogFillTime(log.getLogFillTime());
                list.add(export);
            }
        }
        String templateFileName = "xlstemp/personallog.xls";
        String resultFileName = "xlsresult/personallog.xls";
        String path = ExcelUtil.createExcel(templateFileName, list,
                resultFileName);
        File file = new File(path);
        byte[] bytes = FileUtils.readFileToByteArray(file);
        downloadFile("信息.xls", bytes);
        file.deleteOnExit();        
        
    }
 ExcelUtil类的createExcel方法
public static String createExcel(String templateFileName, List<?> list,
            String resultFileName) {
        // 创建XLSTransformer对象
        XLSTransformer transformer = new XLSTransformer();
        // 获取java项目编译后根路径
        String class_path = ExcelUtil.class.getClassLoader().getResource("").getPath();
        // 得到模板文件路径
        String srcFilePath = class_path + templateFileName;
        Map<String, Object> beanParams = new HashMap<String, Object>();
        beanParams.put("list", list);
        String destFilePath = class_path + resultFileName;
        try {
            // 生成Excel文件
            transformer.transformXLS(srcFilePath, beanParams, destFilePath);
            return destFilePath;
        } catch (ParsePropertyException e) {
            e.printStackTrace();
        } catch (InvalidFormatException e) {
            e.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

 

未完待续。。。。

java 导入导出excel

标签:des   style   blog   color   io   os   ar   使用   java   

原文地址:http://www.cnblogs.com/qingmaple/p/4045647.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!