Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.List;
- public class ToExcel {
- static interface WritableToExcel {
- public int getCellCount();
- public String getCellData(int cell);
- }
- static class Employee implements WritableToExcel {
- private String name;
- private int pay;
- public Employee(String name, int pay) {
- this.name = name;
- this.pay = pay;
- }
- @Override
- public int getCellCount() {
- return 2;
- }
- @Override
- public String getCellData(int cell) {
- if(cell == 0) {
- return name;
- }
- if (cell == 1) {
- return pay + "";
- }
- return null;
- }
- }
- static class ObejctTest implements WritableToExcel {
- private String foo, boo, bar;
- public ObejctTest(String foo, String boo, String bar) {
- this.foo = foo;
- this.boo = boo;
- this.bar = bar;
- }
- @Override
- public int getCellCount() {
- return 3;
- }
- @Override
- public String getCellData(int cell) {
- switch(cell) {
- case 0: return foo;
- case 1: return boo;
- case 2: return bar;
- default: return null;
- }
- }
- }
- public static void main(String[] args) {
- List<Employee> emp = new ArrayList<>();
- emp.add(new Employee("Frank", 500));
- emp.add(new Employee("Ben", 600));
- emp.add(new Employee("Alice", 550));
- List<ObejctTest> obj = new ArrayList<>();
- obj.add(new ObejctTest("one", "two", "three"));
- obj.add(new ObejctTest("red", "green", "blue"));
- writeReportToExcel(emp, obj);
- }
- static void writeReportToExcel(List<? extends WritableToExcel>... lists) {
- //not really write to excel, but to stdout
- for(List<? extends WritableToExcel> list: lists) {
- System.out.println("New list");
- for(WritableToExcel element: list) {
- for(int i = 0; i<element.getCellCount(); i++) {
- System.out.print(element.getCellData(i) +", ");
- }
- System.out.println();
- }
- }
- }
- }
- /*
- OUTPUT:
- New list
- Frank, 500,
- Ben, 600,
- Alice, 550,
- New list
- one, two, three,
- red, green, blue,
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement