Advertisement
kajacx

WriteToExcel

Jun 25th, 2014
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.41 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3.  
  4. public class ToExcel {
  5.     static interface WritableToExcel {
  6.         public int getCellCount();
  7.         public String getCellData(int cell);
  8.     }
  9.    
  10.     static class Employee implements WritableToExcel {
  11.         private String name;
  12.         private int pay;
  13.  
  14.         public Employee(String name, int pay) {
  15.             this.name = name;
  16.             this.pay = pay;
  17.         }
  18.        
  19.         @Override
  20.         public int getCellCount() {
  21.             return 2;
  22.         }
  23.  
  24.         @Override
  25.         public String getCellData(int cell) {
  26.             if(cell == 0) {
  27.                 return name;
  28.             }
  29.             if (cell == 1) {
  30.                 return pay + "";
  31.             }
  32.             return null;
  33.         }
  34.        
  35.     }
  36.    
  37.     static class ObejctTest implements WritableToExcel {
  38.         private String foo, boo, bar;
  39.  
  40.         public ObejctTest(String foo, String boo, String bar) {
  41.             this.foo = foo;
  42.             this.boo = boo;
  43.             this.bar = bar;
  44.         }
  45.  
  46.         @Override
  47.         public int getCellCount() {
  48.             return 3;
  49.         }
  50.  
  51.         @Override
  52.         public String getCellData(int cell) {
  53.             switch(cell) {
  54.                 case 0: return foo;
  55.                 case 1: return boo;
  56.                 case 2: return bar;
  57.                 default: return null;
  58.             }
  59.         }
  60.     }
  61.    
  62.     public static void main(String[] args) {
  63.         List<Employee> emp = new ArrayList<>();
  64.         emp.add(new Employee("Frank", 500));
  65.         emp.add(new Employee("Ben", 600));
  66.         emp.add(new Employee("Alice", 550));
  67.        
  68.         List<ObejctTest> obj = new ArrayList<>();
  69.         obj.add(new ObejctTest("one", "two", "three"));
  70.         obj.add(new ObejctTest("red", "green", "blue"));
  71.        
  72.         writeReportToExcel(emp, obj);
  73.     }
  74.    
  75.     static void writeReportToExcel(List<? extends WritableToExcel>... lists) {
  76.         //not really write to excel, but to stdout
  77.         for(List<? extends WritableToExcel> list: lists) {
  78.             System.out.println("New list");
  79.             for(WritableToExcel element: list) {
  80.                 for(int i = 0; i<element.getCellCount(); i++) {
  81.                     System.out.print(element.getCellData(i) +", ");
  82.                 }
  83.                 System.out.println();
  84.             }
  85.         }
  86.     }
  87. }
  88.  
  89. /*
  90. OUTPUT:
  91. New list
  92. Frank, 500,
  93. Ben, 600,
  94. Alice, 550,
  95. New list
  96. one, two, three,
  97. red, green, blue,
  98. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement