View Javadoc

1   /*
2    * 
3    * 
4    */
5   package oscon2006.web.excel;
6   
7   import org.apache.poi.hssf.usermodel.HSSFCell;
8   import org.apache.poi.hssf.usermodel.HSSFDataFormat;
9   import org.apache.poi.hssf.usermodel.HSSFRow;
10  import org.apache.poi.hssf.usermodel.HSSFDateUtil;
11  
12  import wicket.Component;
13  import wicket.markup.html.basic.Label;
14  import wicket.model.IModel;
15  import wicket.extensions.markup.html.repeater.data.table.IColumn;
16  import wicket.extensions.markup.html.repeater.refreshing.Item;
17  
18  import java.util.*;
19  import java.text.*;
20  
21  public class ExcelColumn implements IColumn
22  {
23  	private boolean sortable = false;
24  	private static final String CELL_ID = "cell";
25  	private String name = "";
26  	
27  	public ExcelColumn(String colName)
28  	{
29  		this.name = colName;
30  	}
31  	
32  	public Component getHeader(String id)
33  	{
34  		return new Label(id, "");
35  	}
36  
37  	public String getSortProperty()
38  	{
39  		return null;
40  	}
41  
42  	public void setSortable(boolean b)
43  	{
44  		this.sortable = b;
45  	}
46  	
47  	public boolean isSortable()
48  	{
49  		return false;
50  	}
51  
52  	public void populateItem(Item item, 
53  										String componentId, 
54  										IModel rowModel)
55  	{
56  		int idx = item.getIndex();
57  		
58  		HSSFRow row = (HSSFRow) rowModel.getObject(null);
59  		
60  		HSSFCell c = row.getCell( (short) idx);
61  
62  		int rowNum = row.getRowNum();
63  		
64  		String s = "";
65  		
66  		if (c == null)
67  		{
68  			s = "";
69  		}
70  		else if (c.getCellType() == HSSFCell.CELL_TYPE_STRING)
71  		{
72  			s = c.getStringCellValue();
73  		}
74  		else if (c.getCellType() == HSSFCell.CELL_TYPE_NUMERIC)
75  		{
76  			short formatIndex = c.getCellStyle().getDataFormat();
77  			String formatPattern = HSSFDataFormat.getBuiltinFormat(formatIndex);
78  			
79  			if (HSSFDateUtil.isCellDateFormatted(c))
80  			{
81  				Date d = c.getDateCellValue();
82  				s = d.toString();
83  			}
84  			else
85  			{
86  				DecimalFormat fmt = new DecimalFormat(formatPattern); 
87  				s = fmt.format(c.getNumericCellValue());
88  			}
89  		}
90  		else if (c.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN)
91  		{
92  			s = c.getBooleanCellValue() + "";
93  		}
94  		
95  		item.add(new Label(CELL_ID, s));
96  
97  	}
98  
99  	public String toString()
100 	{
101 		return this.name;
102 	}
103 }