This is a simple csv builder with Java 8.
See all the code for example 1 here
List<String> stringValues = Arrays.asList("Value1", "Value2", "Value3");
new CsvBuilder<String>()
.forElements(stringValues)
.column(String::toString)
.column(String::toUpperCase)
.column(String::toLowerCase)
.toString();
Value1;VALUE1;value1
Value2;VALUE2;value2
Value3;VALUE3;value3
See all the code for example 2 here
new CsvBuilder<SomeRandomClass>()
.forElements(listOfRandomClass)
.column(randomClass -> randomClass.getRandomStringValue())
.column(randomClass -> randomClass.getRandomIntegerValue().toString())
.column(randomClass -> randomClass.getRandomDoubleValue().toString())
.toString();
0_Value;0;0.0
1_Value;1;1.0
2_Value;2;2.0
3_Value;3;3.0
4_Value;4;4.0
See all the code for example 3 here
new CsvBuilder<Person>()
.forElements(people)
.column(Person::getName)
.column(p -> p.getAge().toString())
.column(Person::getAddress)
.column(Person::getCity)
.column(Person::getState)
.toString();
Name0;0;Address0;City0;State1
Name1;1;Address1;City1;State1
Name2;2;Address2;City2;State1
Name3;3;Address3;City3;State1
Name4;4;Address4;City4;State1
See all the code for example 4 here
String[] columnsName = {"Name","Age","Address","City","State"};
new CsvBuilder<Person>()
.forElements(people)
.header(columnsName)
.column(Person::getName)
.column(p -> p.getAge().toString())
.column(Person::getAddress)
.column(Person::getCity)
.column(Person::getState)
.toString();
Name;Age;Address;City;State
Name0;0;Address0;City0;State1
Name1;1;Address1;City1;State1
Name2;2;Address2;City2;State1
Name3;3;Address3;City3;State1
Name4;4;Address4;City4;State1
See all the code for example 5 here
String[] columnsName = {"Name","Age","Address","City","State"};
new CsvBuilder<Person>()
.forElements(people)
.header(columnsName)
.header("1", "2", "3")
.header("1", "2", "3", "4", "5")
.header("1", "2", "3", "4", "5", "6")
.column(Person::getName)
.column(p -> p.getAge().toString())
.column(Person::getAddress)
.column(Person::getCity)
.column(Person::getState)
.toString();
Name;Age;Address;City;State
1;2;3
1;2;3;4;5
1;2;3;4;5;6
Name0;0;Address0;City0;State1
Name1;1;Address1;City1;State1
Name2;2;Address2;City2;State1
Name3;3;Address3;City3;State1
Name4;4;Address4;City4;State1
See all the code for example 6 here
String[] columnsName = {"Name","Age","Address","City","State"};
new CsvBuilder<Person>()
.forElements(people)
.header(columnsName)
.header(CsvBuilder.blankColumns(columnsName.length))
.column(Person::getName)
.column(p -> p.getAge().toString())
.column(Person::getAddress)
.column(Person::getCity)
.column(Person::getState)
.toString();
Name;Age;Address;City;State
;;;;
Name0;0;Address0;City0;State1
Name1;1;Address1;City1;State1
Name2;2;Address2;City2;State1
Name3;3;Address3;City3;State1
Name4;4;Address4;City4;State1
See all the code for example 7 here
String[] footerValues = {"footer1", "40", "some", "value", "here"};
new CsvBuilder<Person>()
.forElements(people)
.column(Person::getName)
.column(p -> p.getAge().toString())
.column(Person::getAddress)
.column(Person::getCity)
.column(Person::getState)
.footer(footerValues)
.toString();
Name0;0;Address0;City0;State1
Name1;1;Address1;City1;State1
Name2;2;Address2;City2;State1
Name3;3;Address3;City3;State1
Name4;4;Address4;City4;State1
footer1;40;some;value;here
See all the code for example 9 here
String[] footerValues = {"footer1", "40", "some", "value", "here"};
new CsvBuilder<Person>()
.forElements(people)
.column(Person::getName)
.column(p -> p.getAge().toString())
.column(Person::getAddress)
.column(Person::getCity)
.column(Person::getState)
.footer(footerValues)
.footer("another", "way", "to", "set", "footer", "values")
.toString();
Name0;0;Address0;City0;State1
Name1;1;Address1;City1;State1
Name2;2;Address2;City2;State1
Name3;3;Address3;City3;State1
Name4;4;Address4;City4;State1
footer1;40;some;value;here
another;way;to;set;footer;values
See all the code for example 9 here
new CsvBuilder<Person>()
.forElements(people)
.header(CsvBuilder.rightPadding(5, "value1", "value2", "value3"))
.header(CsvBuilder.rightPadding(5, "value1"))
.header(CsvBuilder.rightPadding(8, "value1", "value2", "value3"))
.column(Person::getName)
.column(p -> p.getAge().toString())
.column(Person::getAddress)
.column(Person::getCity)
.column(Person::getState)
.toString();
value1;value2;value3;;
value1;;;;
value1;value2;value3;;;;;
Name0;0;Address0;City0;State1
Name1;1;Address1;City1;State1
Name2;2;Address2;City2;State1
Name3;3;Address3;City3;State1
Name4;4;Address4;City4;State1