tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Template Engines > Velocity > Object List to CSV

Object List to CSV 

Apache Velocity is a free, simple and powerful template engine written in 100% pure Java. This requires the libraries velocity-1.7.jar, oro-2.0.8.jar, commons-lang-2.4.jar, commons-collections-3.2.1.jar, commons-logging-1.1.jar, log4j-1.2.12.jar to be in classpath. The following example shows generating a simple CSV in Velocity. You need to escape the strings as per CSV specification to handle complex data.

File Name  :  
/VELOCITY001/config/csv.vm 

File Name  :  
com/bethecoder/tutorials/velocity/common/Student.java 
   
package com.bethecoder.tutorials.velocity.common;

public class Student {

  private String name;
  private int age;
  private String hobby;

  public Student() {
  }

  public Student(String name, int age, String hobby) {
    super();
    this.name = name;
    this.age = age;
    this.hobby = hobby;
  }

  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public int getAge() {
    return age;
  }
  public void setAge(int age) {
    this.age = age;
  }
  public String getHobby() {
    return hobby;
  }
  public void setHobby(String hobby) {
    this.hobby = hobby;
  }
  
  public String toString() {
    return "Student[name = " + name + ", age = " + age + ", hobby = " + hobby + "]";
  }
}
   

File Name  :  
com/bethecoder/tutorials/velocity/tests/ObjectList2CSVTest.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.velocity.tests;

import java.io.StringWriter;
import java.util.Arrays;
import java.util.List;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;

import com.bethecoder.tutorials.velocity.common.Student;

public class ObjectList2CSVTest {

  /**
   @param args
   */
  public static void main(String[] args)  {

    /**
     * Initialize engine and get template
     */
    VelocityEngine ve = new VelocityEngine();
    ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath")
    ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
        Template template = ve.getTemplate("csv.vm");
        
        /**
         * Prepare context data
         */
        Student std1 = new Student("Sriram"2"Chess");
        Student std2 = new Student("Sudhakar"29"Painting");
        Student std3 = new Student("Anu"28"Cooking");
        List<Student> students = Arrays.asList(std1, std2, std3);
        
        VelocityContext context = new VelocityContext();
        context.put("students", students);

        /**
         * Merge data and template
         */
        StringWriter swOut = new StringWriter();
        template.merge(context, swOut);
        
        System.out.println(swOut);
  }
}
   

It gives the following output,
"Name","Age","Hobby"
Sriram,2,Chess 
Sudhakar,29,Painting 
Anu,28,Cooking 



 
  


  
bl  br