ReadExcelUtil.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package platform.common.util;
  2. import org.apache.poi.hssf.usermodel.HSSFCell;
  3. import org.apache.poi.hssf.usermodel.HSSFRow;
  4. import org.apache.poi.hssf.usermodel.HSSFSheet;
  5. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  6. import org.apache.poi.xssf.usermodel.XSSFCell;
  7. import org.apache.poi.xssf.usermodel.XSSFRow;
  8. import org.apache.poi.xssf.usermodel.XSSFSheet;
  9. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  10. import org.springframework.web.multipart.MultipartFile;
  11. import java.io.IOException;
  12. import java.io.InputStream;
  13. import java.util.ArrayList;
  14. import java.util.List;
  15. public class ReadExcelUtil {
  16. public int totalRows; //sheet中总行数
  17. public static int totalCells; //每一行总单元格数
  18. /**
  19. * read the Excel .xlsx,.xls
  20. * @param file jsp中的上传文件
  21. * @return
  22. * @throws IOException
  23. */
  24. public List<ArrayList<String>> readExcel(MultipartFile file) throws IOException {
  25. if(file==null||ExcelUtil.EMPTY.equals(file.getOriginalFilename().trim())){
  26. return null;
  27. }else{
  28. String postfix = ExcelUtil.getPostfix(file.getOriginalFilename());
  29. if(!ExcelUtil.EMPTY.equals(postfix)){
  30. if(ExcelUtil.OFFICE_EXCEL_2003_POSTFIX.equals(postfix)){
  31. return readXls(file);
  32. }else if(ExcelUtil.OFFICE_EXCEL_2010_POSTFIX.equals(postfix)){
  33. return readXlsx(file);
  34. }else{
  35. return null;
  36. }
  37. }
  38. }
  39. return null;
  40. }
  41. /**
  42. * read the Excel 2010 .xlsx
  43. * @param file
  44. * @return
  45. * @throws IOException
  46. */
  47. @SuppressWarnings("deprecation")
  48. public List<ArrayList<String>> readXlsx(MultipartFile file){
  49. List<ArrayList<String>> list = new ArrayList<ArrayList<String>>();
  50. // IO流读取文件
  51. InputStream input = null;
  52. XSSFWorkbook wb = null;
  53. ArrayList<String> rowList = null;
  54. try {
  55. input = file.getInputStream();
  56. // 创建文档
  57. wb = new XSSFWorkbook(input);
  58. //读取sheet(页)
  59. for(int numSheet=0;numSheet<wb.getNumberOfSheets();numSheet++){
  60. XSSFSheet xssfSheet = wb.getSheetAt(numSheet);
  61. if(xssfSheet == null){
  62. continue;
  63. }
  64. totalRows = xssfSheet.getLastRowNum();
  65. //读取Row,从第4行开始
  66. for(int rowNum = 1;rowNum <= totalRows;rowNum++){
  67. XSSFRow xssfRow = xssfSheet.getRow(rowNum);
  68. if(xssfRow!=null){
  69. rowList = new ArrayList<String>();
  70. totalCells = xssfRow.getLastCellNum();
  71. //读取列,从第一列开始
  72. for(int c=0;c<=totalCells+1;c++){
  73. XSSFCell cell = xssfRow.getCell(c);
  74. if(cell==null){
  75. rowList.add(ExcelUtil.EMPTY);
  76. continue;
  77. }
  78. rowList.add(ExcelUtil.getXValue(cell).trim());
  79. }
  80. list.add(rowList);
  81. }
  82. }
  83. }
  84. return list;
  85. } catch (IOException e) {
  86. e.printStackTrace();
  87. } finally{
  88. try {
  89. input.close();
  90. } catch (IOException e) {
  91. e.printStackTrace();
  92. }
  93. }
  94. return null;
  95. }
  96. /**
  97. * read the Excel 2003-2007 .xls
  98. * @param file
  99. * @return
  100. * @throws IOException
  101. */
  102. public List<ArrayList<String>> readXls(MultipartFile file){
  103. List<ArrayList<String>> list = new ArrayList<ArrayList<String>>();
  104. // IO流读取文件
  105. InputStream input = null;
  106. HSSFWorkbook wb = null;
  107. ArrayList<String> rowList = null;
  108. try {
  109. input = file.getInputStream();
  110. // 创建文档
  111. wb = new HSSFWorkbook(input);
  112. //读取sheet(页)
  113. for(int numSheet=0;numSheet<wb.getNumberOfSheets();numSheet++){
  114. HSSFSheet hssfSheet = wb.getSheetAt(numSheet);
  115. if(hssfSheet == null){
  116. continue;
  117. }
  118. totalRows = hssfSheet.getLastRowNum();
  119. //读取Row,从第二行开始
  120. for(int rowNum = 1;rowNum <= totalRows;rowNum++){
  121. HSSFRow hssfRow = hssfSheet.getRow(rowNum);
  122. if(hssfRow!=null){
  123. rowList = new ArrayList<String>();
  124. totalCells = hssfRow.getLastCellNum();
  125. //读取列,从第一列开始
  126. for(short c=0;c<=totalCells+1;c++){
  127. HSSFCell cell = hssfRow.getCell(c);
  128. if(cell==null){
  129. rowList.add(ExcelUtil.EMPTY);
  130. continue;
  131. }
  132. rowList.add(ExcelUtil.getHValue(cell).trim());
  133. }
  134. list.add(rowList);
  135. }
  136. }
  137. }
  138. return list;
  139. } catch (IOException e) {
  140. e.printStackTrace();
  141. } finally{
  142. try {
  143. input.close();
  144. } catch (IOException e) {
  145. e.printStackTrace();
  146. }
  147. }
  148. return null;
  149. }
  150. }