• java用freemarker实现导出excel


    前几天做了jxl导出excel,现在用freemarker做一下

    freemarker导出excel和导出word步骤和是实现方法是相同的。

    1.制作excel模板

    2.将后缀名改为ftl,放到对应的位置下

    3.实现方法

      1 package org.lq.ssm.gp.controller;
      2 
      3 import java.io.BufferedWriter;
      4 import java.io.File;
      5 import java.io.FileOutputStream;
      6 import java.io.IOException;
      7 import java.io.OutputStreamWriter;
      8 import java.io.Writer;
      9 import java.util.ArrayList;
     10 import java.util.HashMap;
     11 import java.util.List;
     12 import java.util.Map;
     13 
     14 import org.lq.ssm.entity.LandUser;
     15 import org.springframework.web.bind.annotation.RequestMapping;
     16 
     17 import freemarker.template.Configuration;
     18 import freemarker.template.Template;
     19 import freemarker.template.TemplateException;
     20 import freemarker.template.TemplateExceptionHandler;
     21 
     22 public class exportExcel {
     23     
     24     private Configuration configuration = null;
     25     
     26     public exportExcel(){
     27          configuration = new Configuration();
     28          configuration.setDefaultEncoding("utf-8");
     29     }
     30     //@RequestMapping(params="print")
     31     public void print() throws IOException {
     32          
     33          configuration = new Configuration();
     34          configuration.setDefaultEncoding("utf-8");
     35          // 要填入模本的数据文件
     36         Map dataMap = new HashMap();
     37         //getData(dataMap);
     38         
     39         List<Map<String, Object>>list=new ArrayList<Map<String,Object>>();
     40         for(int i=0;i<10;i++){
     41             Map<String, Object>map=new HashMap<String, Object>();
     42             map.put("userName", "张三");
     43             map.put("landName", "张三");
     44             map.put("landScmj", "111111");
     45             map.put("landBzdate", "20170626");
     46             list.add(map);
     47         }
     48         dataMap.put("list", list);
     49         
     50         // 设置模本装置方法和路径,FreeMarker支持多种模板装载方法。可以重servlet,classpath,数据库装载,  
     51           // 这里我们的模板
     52         //configuration.setDirectoryForTemplateLoading(new File("G:\"));  
     53         
     54         
     55         configuration.setClassForTemplateLoading(this.getClass(), "/org/lq/ssm/gp/controller");
     56          //设置异常处理器
     57         configuration.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);
     58         Template t = null;
     59         try {
     60              // test.ftl为要装载的模板 
     61             t = configuration.getTemplate("land.ftl");
     62             t.setEncoding("utf-8");
     63 
     64         } catch (IOException e) {
     65             e.printStackTrace();
     66         }
     67         // 输出文档路径及名称
     68         File outFile = new File("G:/TTT/land.xls");
     69         Writer out = null;
     70         
     71         try {
     72             out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"));
     73         } catch (Exception e1) {
     74             e1.printStackTrace();
     75         }
     76 
     77         try {
     78             t.process(dataMap, out);
     79             out.close();
     80         } catch (TemplateException e) {
     81             e.printStackTrace();
     82         } catch (IOException e) {
     83             e.printStackTrace();
     84         }
     85     }
     86     
     87     
     88     
     89     /**
     90      * 注意dataMap里存放的数据Key值要与模板中的参数相对应 
     91      * @param dataMap
     92      * @throws IOException 
     93      * 
     94      */
     95 
     96     public static void main(String[] args) throws IOException {
     97         
     98         exportExcel exc=new exportExcel();
     99         exc.print();
    100     }
    101 
    102 }

    4.excel表格就好了。但是可能在打开文件的时候,出现错误。

    这时候修改模板内容,找到

    将值改大一点,就可以了。

  • 相关阅读:
    HTML+CSS基础
    学习C++——实践者的方法(转整)
    招聘要求看技术发展
    笔记本自建无线wifi
    sortAlgorithms
    耐得住寂寞,拥得了繁华
    C++函数重载实现原理浅析
    程序员指南
    tf.image.non_max_suppression()
    函数 不定长参数
  • 原文地址:https://www.cnblogs.com/xiaotian-222/p/7079786.html
Copyright © 2020-2023  润新知