JXLS(エクセルのインポート、エクスポートツール)

JXLS(エクセルのインポート、エクスポートツール)

1:はじめに:

       jxlsは簡単ですが、軽量な特異的なマーカーを使用してExcelテンプレートファイルの出力フォーマットやレイアウトに定義され、エクスポートライブラリを優れています。Javaは成熟した輸出ツールPOL、JXLに優れ、それらはすべてExcelをエクスポートするためにJavaコードを使用して、不便な符号化効率が非常に低いメンテナンスです。

また、ツールのいくつかは、エクスポートテンプレートを実装するのは簡単です使用することができます。ツールはまだ維持され、国がより良いeasyPOIを行っている、外国人はこのJXLSです。

比較:

多くの複雑なプロジェクトの報告書が有害な状況になりやすい細胞があります(多数のセルをマージし、セルスタイル)、easyPOI処理時間がマージされ、ポイコードのメンテナンスコストが高いです。

 

2:Mavenの依存関係:

 

<依存性> 
    <のgroupId> org.jxls </のgroupId> 
    <たartifactId> jxls </たartifactId> 
    <バージョン> 2.4.6 </バージョン> 
</依存> 
<依存性> 
    <のgroupId> org.jxls </のgroupId> 
    <たartifactId > jxls-POI </たartifactId> 
    <バージョン> 1.0.12 </バージョン> 
</依存> 
<依存性> 
    <のgroupId> org.jxls </のgroupId> 
    <たartifactId> jxls-jexcel </たartifactId> 
    <バージョン> 1.0。 6 </バージョン> 
</依存>

 

 

2:以下は簡単な輸出を達成するために最初の操作を行います。

1:モデルを作成します。

 

 2:オフィスのxlsxでテンプレートを作成します

 

ここでは、いくつかのセルのテキスト注釈を参照してくださいオフィスできる注釈ショートカット(くそ+ F2) 最初にここに何の説明をクラスのコンテンツに注釈を付けていない、以下に述べます

 

3:工具类JxlsUtils.java

public class JxlsUtils{
     
    private static final String TEMPLATE_PATH="jxls-template";
     
    public static void exportExcel(InputStream is, OutputStream os, Map<String, Object> model) throws IOException{
        Context context = new Context();
        if (model != null) {
            for (String key : model.keySet()) {
                context.putVar(key, model.get(key));
            }
        }
        JxlsHelper jxlsHelper = JxlsHelper.getInstance();
        Transformer transformer  = jxlsHelper.createTransformer(is, os);
        JexlExpressionEvaluator evaluator = (JexlExpressionEvaluator)transformer.getTransformationConfig().getExpressionEvaluator();
        Map<String, Object> funcs = new HashMap<String, Object>();
      //  funcs.put("utils", new JxlsUtils());    //添加自定义功能
      //  evaluator.getJexlEngine().setFunctions(funcs);
        jxlsHelper.processTemplate(context, transformer);
    }
 
    public static void exportExcel(File xls, File out, Map<String, Object> model) throws FileNotFoundException, IOException {
            exportExcel(new FileInputStream(xls), new FileOutputStream(out), model);
    }
     
    public static void exportExcel(String templateName, OutputStream os, Map<String, Object> model) throws FileNotFoundException, IOException {
        File template = getTemplate(templateName);
        if(template!=null){
            exportExcel(new FileInputStream(template), os, model);    
        }
    }
     
     
    //获取jxls模版文件
 
    public static File getTemplate(String name){
        String templatePath = JxlsUtils.class.getClassLoader().getResource(TEMPLATE_PATH).getPath();
        File template = new File(templatePath, name);
        if(template.exists()){
            return template;
        }
        return null;
    }    
     
    // 日期格式化
    public String dateFmt(Date date, String fmt) {
        if (date == null) {
            return "";
        }
        try {
            SimpleDateFormat dateFmt = new SimpleDateFormat(fmt);
            return dateFmt.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
     
    // if判断
    public Object ifelse(boolean b, Object o1, Object o2) {
        return b ? o1 : o2;
    }
     
}

上面注释中的自定义功能可以先注释到,我们后面在讲

 

入口ObjectCollectionDemo.java

public class ObjectCollectionDemo {
    static Logger logger = LoggerFactory.getLogger(ObjectCollectionDemo.class);
     
    public static void main(String[] args) throws ParseException, IOException {
        logger.info("Running Object Collection demo");
         
        List<Employee> employees = generateSampleEmployeeData();
        OutputStream os = new FileOutputStream("target/object_collection_output.xls");
        Map<String , Object> model=new HashMap<String , Object>();
        model.put("employees", employees);
        model.put("nowdate", new Date());
        JxlsUtils.exportExcel("object_collection_template.xls", os, model);
        os.close();
    }
 
    public static List<Employee> generateSampleEmployeeData() throws ParseException {
        List<Employee> employees = new ArrayList<Employee>();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MMM-dd", Locale.US);
        employees.add( new Employee("Elsa", dateFormat.parse("1970-Jul-10"), 1500, 0.15) );
        employees.add( new Employee("Oleg", dateFormat.parse("1973-Apr-30"), 2300, 0.25) );
        employees.add( new Employee("Neil", dateFormat.parse("1975-Oct-05"), 2500, 0.00) );
        employees.add( new Employee("Maria", dateFormat.parse("1978-Jan-07"), 1700, 0.15) );
        employees.add( new Employee("John", dateFormat.parse("1969-May-30"), 2800, 0.20) );
        return employees;
    }
}

生成效果:

 

 好的,这样就很轻松的导出了是不是很简单

 


 

 

3:下面讲一下导出的细节,注意事项:

1:模板中的批注:

注意如果模板中对单元格加了批注,但是没有批注信息的话可能会报错:

 

 

 1-1:bean属性标记

jxls使用 Apache JEXL表达式语言来解析定义在excel模板中的表达式。JEXL与JSTL相似,并对JSTL进行了扩展。eg:

${department.chief.age} //属性可以是无限深度

${utils:dateFmt(date,"yyyy-MM-dd")} //自定义工具函数

1-2:XLS Area定义标记

 

おすすめ

転載: www.cnblogs.com/dw3306/p/11098841.html