PageRenderTime 64ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/java/sourcecode/Excel/src/com/jeasonzhao/commons/excel/Cell.java

http://sqlreport.googlecode.com/
Java | 320 lines | 282 code | 38 blank | 0 comment | 35 complexity | ceb3bff690ff223b6dbcf03afdeb2f02 MD5 | raw file
  1. package com.jeasonzhao.commons.excel;
  2. import java.util.Calendar;
  3. import java.util.Date;
  4. import org.apache.poi.hssf.usermodel.HSSFCell;
  5. import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
  6. import org.apache.poi.hssf.usermodel.HSSFComment;
  7. import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
  8. import org.apache.poi.hssf.usermodel.HSSFHyperlink;
  9. import org.apache.poi.hssf.usermodel.HSSFPatriarch;
  10. import org.apache.poi.hssf.usermodel.HSSFRichTextString;
  11. import org.apache.poi.hssf.util.CellReference;
  12. import org.apache.poi.ss.usermodel.CellValue;
  13. import com.jeasonzhao.commons.utils.Algorithms;
  14. import com.jeasonzhao.commons.utils.ConvertEx;
  15. import com.jeasonzhao.commons.utils.DataTypes;
  16. public class Cell
  17. {
  18. private HSSFCell m_cell = null;
  19. private WorkBook m_workBook = null;
  20. private Sheet m_sheet = null;
  21. private Row m_row = null;
  22. public Cell(WorkBook workBook,Sheet sheet,Row row,HSSFCell cell)
  23. {
  24. m_cell = cell;
  25. m_workBook = workBook;
  26. m_sheet = sheet;
  27. m_row = row;
  28. }
  29. public String getSheetName()
  30. {
  31. return m_sheet.getName();
  32. }
  33. public String getExcelLoaction()
  34. {
  35. CellReference xr = new CellReference(this.getRowIndex(),this.getColumnIndex());
  36. return xr.formatAsString();
  37. }
  38. public int getRowIndex()
  39. {
  40. return this.m_cell.getRowIndex();
  41. }
  42. public int getColumnIndex()
  43. {
  44. return this.m_cell.getColumnIndex();
  45. }
  46. public Sheet getSheet()
  47. {
  48. return m_sheet;
  49. }
  50. public Row getRow()
  51. {
  52. return m_row;
  53. }
  54. public Cell setCellStyle(CellStyle c)
  55. {
  56. if(null != c)
  57. {
  58. c.bind(m_cell);
  59. }
  60. return this;
  61. }
  62. public Cell setType(CellType cellType)
  63. {
  64. m_cell.setCellType(cellType.getId());
  65. return this;
  66. }
  67. public CellType getType()
  68. {
  69. return(CellType) CellType.findConstant(CellType.class,m_cell.getCellType());
  70. }
  71. public Cell setValue(double value)
  72. {
  73. m_cell.setCellValue(value);
  74. return this;
  75. }
  76. public Cell setValue(int value)
  77. {
  78. m_cell.setCellValue((double) value);
  79. return this;
  80. }
  81. public Cell setValue(Date value)
  82. {
  83. m_cell.setCellValue(value);
  84. return this;
  85. }
  86. public Cell setValue(Calendar value)
  87. {
  88. m_cell.setCellValue(value);
  89. return this;
  90. }
  91. public Cell setValue(String value)
  92. {
  93. HSSFRichTextString text = new HSSFRichTextString(value);
  94. m_cell.setCellValue(text);
  95. return this;
  96. }
  97. public Cell setValue(Object value)
  98. {
  99. if(null == value)
  100. {
  101. this.m_cell.setCellValue((HSSFRichTextString)null);
  102. return this;
  103. }
  104. DataTypes dt = DataTypes.from(value.getClass());
  105. if(dt.equals(DataTypes.INT) || dt.equals(DataTypes.DOUBLE))
  106. {
  107. this.setValue(ConvertEx.toDouble(value));
  108. }
  109. else if(dt.equals(DataTypes.BOOL))
  110. {
  111. this.setValue(ConvertEx.toBool(value));
  112. }
  113. else if(dt.equals(DataTypes.DATE))
  114. {
  115. this.setValue(ConvertEx.toDate(value));
  116. }
  117. else
  118. {
  119. this.setValue(Algorithms.toString(value));
  120. }
  121. return this;
  122. }
  123. public Cell setFormula(String formula)
  124. {
  125. m_cell.setCellFormula(formula);
  126. return this;
  127. }
  128. public Cell setValue(boolean value)
  129. {
  130. m_cell.setCellValue(value);
  131. return this;
  132. }
  133. public String getFormula()
  134. {
  135. return m_cell.getCellFormula();
  136. }
  137. public boolean getBoolean()
  138. {
  139. return ConvertEx.toBool(this.getObject());
  140. }
  141. public double getDouble()
  142. {
  143. return ConvertEx.toDouble(this.getObject());
  144. }
  145. public java.util.Date getDate()
  146. {
  147. return ConvertEx.toDate(this.getObject());
  148. }
  149. public String getString()
  150. {
  151. return Algorithms.toString(this.getObject());
  152. }
  153. public Object getObject()
  154. {
  155. switch(m_cell.getCellType())
  156. {
  157. case org.apache.poi.hssf.usermodel.HSSFCell.CELL_TYPE_BLANK:
  158. return "";
  159. case org.apache.poi.hssf.usermodel.HSSFCell.CELL_TYPE_BOOLEAN:
  160. return Boolean.valueOf(m_cell.getBooleanCellValue());
  161. case org.apache.poi.hssf.usermodel.HSSFCell.CELL_TYPE_ERROR:
  162. return null;
  163. case org.apache.poi.hssf.usermodel.HSSFCell.CELL_TYPE_FORMULA:
  164. return getFormulaValue();
  165. case org.apache.poi.hssf.usermodel.HSSFCell.CELL_TYPE_NUMERIC:
  166. return new Double(m_cell.getNumericCellValue());
  167. case org.apache.poi.hssf.usermodel.HSSFCell.CELL_TYPE_STRING:
  168. default:
  169. org.apache.poi.hssf.usermodel.HSSFRichTextString text2 = m_cell.getRichStringCellValue();
  170. return text2.getString();
  171. }
  172. }
  173. private Object getFormulaValue()
  174. {
  175. if(null == m_cell || null == m_sheet || null == m_workBook)
  176. {
  177. return null;
  178. }
  179. HSSFFormulaEvaluator evaluator = new HSSFFormulaEvaluator(this.m_workBook.getHSSFWorkbook());
  180. CellValue obj = evaluator.evaluate(m_cell);
  181. if(null == obj)
  182. {
  183. return null;
  184. }
  185. switch(obj.getCellType())
  186. {
  187. case org.apache.poi.hssf.usermodel.HSSFCell.CELL_TYPE_BLANK:
  188. return "";
  189. case org.apache.poi.hssf.usermodel.HSSFCell.CELL_TYPE_BOOLEAN:
  190. return Boolean.valueOf(obj.getBooleanValue());
  191. case org.apache.poi.hssf.usermodel.HSSFCell.CELL_TYPE_ERROR:
  192. return null;
  193. case org.apache.poi.hssf.usermodel.HSSFCell.CELL_TYPE_NUMERIC:
  194. return new Double(obj.getNumberValue());
  195. case org.apache.poi.hssf.usermodel.HSSFCell.CELL_TYPE_STRING:
  196. default:
  197. return obj.getStringValue();
  198. }
  199. }
  200. public HSSFCell getHSSFCell()
  201. {
  202. return this.m_cell;
  203. }
  204. public CellStyle getCellStyle()
  205. {
  206. CellStyle ret = CellStyle.getCellStyle(this.m_workBook,m_cell.getCellStyle().getIndex());
  207. return ret;
  208. }
  209. public Cell setHyperLinkURL(String strAddress)
  210. {
  211. if(null != strAddress && strAddress.trim().length() > 0)
  212. {
  213. HSSFHyperlink link = new HSSFHyperlink(HSSFHyperlink.LINK_URL);
  214. link.setAddress(strAddress);
  215. this.m_cell.setHyperlink(link);
  216. }
  217. return this;
  218. }
  219. public Cell setHyperLinkDocument(String sheetName,String strCell)
  220. {
  221. if(null == sheetName)
  222. {
  223. return this;
  224. }
  225. if(null == strCell)
  226. {
  227. strCell = "A1"; //Default is the first cell of the specified Sheet.
  228. }
  229. return setHyperLinkDocument("'" + sheetName + "'!" + strCell);
  230. }
  231. public Cell setHyperLinkDocument(Sheet sheet,String strCell)
  232. {
  233. return setHyperLinkDocument(null == sheet ? null : sheet.getName(),strCell);
  234. }
  235. public Cell setHyperLinkDocument(Sheet sheet,Cell cell)
  236. {
  237. return setHyperLinkDocument(null == sheet ? null : sheet.getName(),null == cell ? null : cell.getExcelLoaction());
  238. }
  239. public Cell setHyperLinkDocument(String sheetName,Cell cell)
  240. {
  241. return setHyperLinkDocument(sheetName,null == cell ? null : cell.getExcelLoaction());
  242. }
  243. public Cell setHyperLinkDocument(Sheet sheet,int nrow,int ncol)
  244. {
  245. CellReference xr = new CellReference(nrow,ncol);
  246. return setHyperLinkDocument(null == sheet ? null : sheet.getName(),xr.formatAsString());
  247. }
  248. public Cell setHyperLinkDocument(String strAddress)
  249. {
  250. if(null != strAddress && strAddress.trim().length() > 0)
  251. {
  252. HSSFHyperlink link = new HSSFHyperlink(HSSFHyperlink.LINK_DOCUMENT);
  253. link.setAddress(strAddress);
  254. this.m_cell.setHyperlink(link);
  255. }
  256. return this;
  257. }
  258. public Cell addComment(String strCommecnt)
  259. {
  260. return this.addComment(null,strCommecnt);
  261. }
  262. public Cell addComment(String strAuthor,String strCommecnt)
  263. {
  264. HSSFComment comment = this.m_cell.getCellComment();
  265. if(null == comment)
  266. {
  267. HSSFPatriarch hssfp = m_sheet.getHSSFPatriarch();
  268. comment = hssfp.createComment(new HSSFClientAnchor(0,0,0,0,
  269. (short) (this.getColumnIndex() + 1),this.getRowIndex(),
  270. (short) (this.getColumnIndex() + 5),this.getRowIndex() + 5));
  271. comment.setString(new HSSFRichTextString(strCommecnt));
  272. comment.setAuthor(strAuthor);
  273. m_cell.setCellComment(comment);
  274. }
  275. else
  276. {
  277. comment.setAuthor(strAuthor);
  278. comment.setString(new HSSFRichTextString(strCommecnt));
  279. }
  280. return this;
  281. }
  282. }