PageRenderTime 57ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/filesearch/SearchLocalFile/src/com/searchlocal/filereader/ExcelReader.java

http://filesearch.googlecode.com/
Java | 203 lines | 180 code | 12 blank | 11 comment | 34 complexity | 5f77e78c971a439abb336bfd6fed2512 MD5 | raw file
  1. package com.searchlocal.filereader;
  2. import java.io.FileInputStream;
  3. import java.io.FileNotFoundException;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.util.ArrayList;
  7. import java.util.Date;
  8. import java.util.Iterator;
  9. import java.util.List;
  10. import org.apache.poi.hssf.usermodel.HSSFCell;
  11. import org.apache.poi.hssf.usermodel.HSSFDateUtil;
  12. import org.apache.poi.hssf.usermodel.HSSFRow;
  13. import org.apache.poi.hssf.usermodel.HSSFSheet;
  14. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  15. import org.apache.poi.xssf.usermodel.XSSFCell;
  16. import org.apache.poi.xssf.usermodel.XSSFRow;
  17. import org.apache.poi.xssf.usermodel.XSSFSheet;
  18. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  19. import com.searchlocal.bean.ExcelFileBean;
  20. import com.searchlocal.exception.LogicException;
  21. import com.searchlocal.util.CLogger;
  22. import com.searchlocal.util.StringUtils;
  23. public class ExcelReader {
  24. private static CLogger logger = new CLogger(ExcelReader.class);
  25. public List<ExcelFileBean> getExcelFile(ExcelFileBean bean) throws LogicException {
  26. InputStream inputStream = null;
  27. String filepath = bean.getPath();
  28. try {
  29. inputStream = new FileInputStream(filepath);
  30. if (StringUtils.is2007Doc(filepath)) {
  31. XSSFWorkbook book = new XSSFWorkbook(inputStream);
  32. return getBeanList(book, bean);
  33. } else {
  34. HSSFWorkbook book = new HSSFWorkbook(inputStream);
  35. return getBeanList(book, bean);
  36. }
  37. } catch (FileNotFoundException e) {
  38. logger.error("LG_E001", filepath);
  39. throw new LogicException("LG_E001", e);
  40. } catch (IOException e) {
  41. logger.error("LG_E003", filepath);
  42. throw new LogicException("LG_E003", e);
  43. } finally {
  44. if (inputStream != null) {
  45. try {
  46. inputStream.close();
  47. } catch (IOException e) {
  48. e.printStackTrace();
  49. }
  50. }
  51. }
  52. }
  53. private static List<ExcelFileBean> getBeanList(HSSFWorkbook workbook, ExcelFileBean bean) {
  54. HSSFSheet sheet;
  55. HSSFRow row;
  56. HSSFCell cell;
  57. String value;
  58. List<ExcelFileBean> beanList = new ArrayList<ExcelFileBean>();
  59. ExcelFileBean filebean;
  60. for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
  61. sheet = workbook.getSheetAt(i);
  62. String sheetName = workbook.getSheetName(i);
  63. for (int j = 0; j <= sheet.getLastRowNum(); j++) {
  64. row = sheet.getRow(j);
  65. String rowvalue = "";
  66. if (row != null) {
  67. for (Iterator iter = row.cellIterator(); iter.hasNext();) {
  68. cell = (HSSFCell) iter.next();
  69. if (cell != null) {
  70. int cellType = cell.getCellType();
  71. value = getCellValue(cell, cellType);
  72. if (value != null && !"".equals(value)) {
  73. rowvalue = rowvalue + "," + value;
  74. }
  75. }
  76. }
  77. if (rowvalue != null && !"".equals(rowvalue)) {
  78. filebean = new ExcelFileBean();
  79. rowvalue = rowvalue.substring(1);
  80. filebean.setFilename(bean.getFilename());
  81. filebean.setPath(bean.getPath());
  82. filebean.setContent(rowvalue);
  83. filebean.setRownb(j + 1);
  84. filebean.setSheetname(sheetName);
  85. filebean.setLastmodify(bean.getLastmodify());
  86. beanList.add(filebean);
  87. }
  88. }
  89. }
  90. }
  91. return beanList;
  92. }
  93. private static List<ExcelFileBean> getBeanList(XSSFWorkbook workbook, ExcelFileBean bean) {
  94. XSSFSheet sheet;
  95. XSSFRow row;
  96. XSSFCell cell;
  97. String value;
  98. List<ExcelFileBean> beanList = new ArrayList<ExcelFileBean>();
  99. ExcelFileBean filebean;
  100. // ??Excel?????
  101. for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
  102. workbook.getSheetAt(i);
  103. sheet = workbook.getSheetAt(i);
  104. String sheetName = workbook.getSheetName(i);
  105. for (int j = 0; j <= sheet.getLastRowNum(); j++) {
  106. row = sheet.getRow(j);
  107. String rowvalue = "";
  108. if (row != null) {
  109. for (Iterator iter = row.cellIterator(); iter.hasNext();) {
  110. cell = (XSSFCell) iter.next();
  111. if (cell != null) {
  112. int cellType = cell.getCellType();
  113. value = getCellValue(cell, cellType);
  114. if (value != null && !"".equals(value)) {
  115. rowvalue = rowvalue + "," + value;
  116. }
  117. }
  118. }
  119. if (rowvalue != null && !"".equals(rowvalue)) {
  120. filebean = new ExcelFileBean();
  121. rowvalue = rowvalue.substring(1);
  122. filebean.setFilename(bean.getFilename());
  123. filebean.setPath(bean.getPath());
  124. filebean.setContent(rowvalue);
  125. filebean.setRownb(j + 1);
  126. filebean.setSheetname(sheetName);
  127. filebean.setLastmodify(bean.getLastmodify());
  128. beanList.add(filebean);
  129. }
  130. }
  131. }
  132. }
  133. return beanList;
  134. }
  135. private static String getCellValue(HSSFCell cell, int cellType) {
  136. String returnvalue = "";
  137. switch (cellType) {
  138. case HSSFCell.CELL_TYPE_NUMERIC:
  139. if (HSSFDateUtil.isCellDateFormatted(cell)) {
  140. Date date = cell.getDateCellValue();
  141. returnvalue = date.toString();
  142. break;
  143. }
  144. returnvalue = String.valueOf(cell.getNumericCellValue());
  145. break;
  146. case HSSFCell.CELL_TYPE_STRING:
  147. returnvalue = cell.toString();
  148. break;
  149. case HSSFCell.CELL_TYPE_BOOLEAN:
  150. returnvalue = cell.getBooleanCellValue() ? "true" : "false";
  151. break;
  152. default:
  153. returnvalue = cell.toString();
  154. break;
  155. }
  156. return returnvalue;
  157. }
  158. private static String getCellValue(XSSFCell cell, int cellType) {
  159. String returnvalue = "";
  160. switch (cellType) {
  161. case XSSFCell.CELL_TYPE_NUMERIC:
  162. if (HSSFDateUtil.isCellDateFormatted(cell)) {
  163. Date date = cell.getDateCellValue();
  164. returnvalue = date.toString();
  165. break;
  166. }
  167. returnvalue = String.valueOf(cell.getNumericCellValue());
  168. break;
  169. case XSSFCell.CELL_TYPE_STRING:
  170. returnvalue = cell.toString();
  171. break;
  172. case XSSFCell.CELL_TYPE_BOOLEAN:
  173. returnvalue = cell.getBooleanCellValue() ? "true" : "false";
  174. break;
  175. default:
  176. returnvalue = cell.toString();
  177. break;
  178. }
  179. return returnvalue;
  180. }
  181. // public static void main(String[] args) {
  182. // try {
  183. // ExcelReader reader = new ExcelReader();
  184. // String excelFile = "E:\\test2007doc\\tasklist.xlsx";
  185. // List excelFilet2007 = reader.getExcelFile(excelFile);
  186. // System.out.println("ExcelText2007=======" + excelFilet2007.size());
  187. // } catch (Exception e) {
  188. // e.printStackTrace();
  189. // }
  190. // }
  191. }