Basic example - Inspiredsoft/exporter GitHub Wiki

Text Exporter

Suppose we have a class representing a user with some basic properties:

package it.inspired.exporter.test;

import it.inspired.exporter.annotation.ExpoElement;
import it.inspired.exporter.annotation.Unexportable;

@ExpoElement
public class User {

    private String name;
    private String surname;
    private String login;
    private String password;

    public User(String name, String surname, String login, String password) {
        super();
        this.name = name;
        this.surname = surname;
        this.login = login;
        this.password = password;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    public String getSurname() {
        return surname;
    }
    public void setSurname(String surname) {
        this.surname = surname;
    }

    public String getLogin() {
        return login;
    }
    public void setLogin(String login) {
        this.login = login;
    }

    @Unexportable
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }	
}

As you can se the User class is annotated with @ExpoElement that tells the framework that the data contained can be exported accessing all the properties via reflection. The only property to exclude is the password that is marked @Unexportable.

The following Main class instantiate a TextExporter and export the data.

package it.inspired.exporter.test;

import it.inspired.exporter.TextExporter;

import java.beans.IntrospectionException;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

public class TextMain {
    public static void main(String[] args) {

        TextExporter txt = new TextExporter();
	
        /*
        * Generate the test data
        */
        List<User> list = new ArrayList<User>();
        list.add( new User( "John", "Doe", "jodoe", "john.doe.1" ) );
        list.add( new User( "Jane", "Doe", "jadoe", "jane.doe.2" ) );
	
        try {
            txt.init();
            txt.export(list);
            txt.finalyze();
            txt.write( System.out );
		
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IntrospectionException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Finally this is the output:

Login;Name;Surname
"jodoe";"John";"Doe"
"jadoe";"Jane";"Doe"

The header row can be removed using the option:

txt.setEnabledHeader( false );

#Excel Exporter In a very similar way the following main class export the data in a Microsoft Excel spreadsheet.

public class ExcelMain {

    public static void main(String[] args) {
	
        ExcelExporter excel = new ExcelExporter();
	
            /*
             * Generate the test data
             */
            List<User> list = new ArrayList<User>();
            list.add( new User( "John", "Doe", "jodoe", "john.doe.1" ) );
            list.add( new User( "Jane", "Doe", "jadoe", "jane.doe.2" ) );
	
            try {
                OutputStream output = new FileOutputStream("users.xls");
		
                excel.init();
                excel.export( list );
                excel.finalyze();
                excel.write( output );
                
                output.close();
		
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (IntrospectionException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

L'excel generato è il seguente:

https://goo.gl/photos/2w9uJeGMkdNVTeFN6

⚠️ **GitHub.com Fallback** ⚠️