8b742e3763b0674dc155da45effe7cf4a4f3f34f.svn-base 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package com.sinosoft.em.evaluate.excel;
  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.List;
  8. import java.util.Map;
  9. import javax.servlet.http.HttpServletRequest;
  10. import org.apache.commons.fileupload.FileItemIterator;
  11. import org.apache.commons.fileupload.FileItemStream;
  12. import org.apache.commons.fileupload.servlet.ServletFileUpload;
  13. import org.apache.poi.hssf.usermodel.HSSFCell;
  14. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  15. import org.apache.poi.ss.usermodel.Cell;
  16. import org.apache.poi.ss.usermodel.Row;
  17. import org.apache.poi.ss.usermodel.Sheet;
  18. import org.apache.poi.ss.usermodel.Workbook;
  19. import org.apache.poi.xssf.usermodel.XSSFCell;
  20. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  21. public class ExcelUitl {
  22. /**
  23. * 适用于没有标题行的excel
  24. * @throws Exception
  25. */
  26. public static List<List<String>> readExcelWithoutTitle(String filepath) throws Exception{
  27. String fileType = filepath.substring(filepath.lastIndexOf(".") + 1, filepath.length());
  28. InputStream is = null;
  29. Workbook wb = null;
  30. try {
  31. is = new FileInputStream(filepath);
  32. if (fileType.equals("xls")) {
  33. wb = new HSSFWorkbook(is);
  34. } else if (fileType.equals("xlsx")) {
  35. wb = new XSSFWorkbook(is);
  36. } else {
  37. throw new Exception("读取的不是excel文件");
  38. }
  39. List<List<String>> result = new ArrayList<List<String>>();//对应excel文件
  40. Sheet sheet = wb.getSheetAt(0);
  41. int rowSize = sheet.getLastRowNum() + 1;
  42. for (int i = 3; i < rowSize; i++) {//遍历行
  43. List<String> sheetList = new ArrayList<String>();//对应sheet页
  44. Row row = sheet.getRow(i);
  45. if (row == null) {//略过空行
  46. continue;
  47. }
  48. int cellSize = row.getLastCellNum();//行中有多少个单元格,也就是有多少列
  49. for(int j=0;j<cellSize;j++){
  50. Cell cell = row.getCell(j);
  51. String value = "";
  52. if(cell.getCellType()==Cell.CELL_TYPE_BOOLEAN){
  53. value = String.valueOf(cell.getBooleanCellValue());
  54. }else if(cell.getCellType()==Cell.CELL_TYPE_NUMERIC){
  55. value = String.valueOf(cell.getNumericCellValue());
  56. }else{
  57. value = String.valueOf(cell.getStringCellValue());
  58. }
  59. if(j>=14)
  60. sheetList.add(value);
  61. }
  62. result.add(sheetList);
  63. }
  64. return result;
  65. } catch (FileNotFoundException e) {
  66. throw e;
  67. } finally {
  68. if (wb != null) {
  69. wb.close();
  70. }
  71. if (is != null) {
  72. is.close();
  73. }
  74. }
  75. }
  76. @SuppressWarnings("static-access")
  77. private String getValue(XSSFCell xssfRow) {
  78. if (xssfRow.getCellType() == xssfRow.CELL_TYPE_BOOLEAN) {
  79. return String.valueOf(xssfRow.getBooleanCellValue());
  80. } else if (xssfRow.getCellType() == xssfRow.CELL_TYPE_NUMERIC) {
  81. return String.valueOf(xssfRow.getNumericCellValue());
  82. } else {
  83. return String.valueOf(xssfRow.getStringCellValue());
  84. }
  85. }
  86. @SuppressWarnings("static-access")
  87. private String getValue(HSSFCell hssfCell) {
  88. if (hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN) {
  89. return String.valueOf(hssfCell.getBooleanCellValue());
  90. } else if (hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC) {
  91. return String.valueOf(hssfCell.getNumericCellValue());
  92. } else {
  93. return String.valueOf(hssfCell.getStringCellValue());
  94. }
  95. }
  96. public static void main(String[] args) {
  97. /*String filePath = "C:\\Users\\len\\Desktop\\excelDemo\\ceshi.xlsx";
  98. try {
  99. ExcelUitl.readExcelWithoutTitle(filePath);
  100. } catch (Exception e) {
  101. // TODO Auto-generated catch block
  102. e.printStackTrace();
  103. }*/
  104. System.out.println(Integer.parseInt("00.000"));
  105. }
  106. /**
  107. * 获取文件相信信息
  108. *
  109. * @param request
  110. * HttpServletRequest实例
  111. * @param imgPath
  112. * 图片路径
  113. * @param fileName
  114. * 图片名
  115. * @param path
  116. *
  117. * ftp保存的位置
  118. * @return
  119. * @throws IOException
  120. */
  121. public static Map getFileMessage(HttpServletRequest request,
  122. String imgPath, String fileName, String path) throws IOException {
  123. Map map = null;
  124. if (ServletFileUpload.isMultipartContent(request)) {
  125. // 创建ServletFileUpload实例
  126. ServletFileUpload fileUpload = new ServletFileUpload();
  127. FileItemIterator iter = null;
  128. FileItemStream item = null;
  129. InputStream is = null;
  130. try {
  131. // 解析request请求 返回FileItemStream的iterator实例
  132. iter = fileUpload.getItemIterator(request);
  133. // 迭代取出
  134. while (iter.hasNext()) {
  135. item = iter.next();// 获取文件流
  136. if (!item.isFormField()) {
  137. // 为什么要这么取出这个流呢,因为request.getInputStream()中,表单提交上来的不仅仅包含了文件,还带有参数,就算不带参数,也还有request中本身的一些其他东西(ps:没有研究过,但是试过不带参数拿到的流也是不对的。)直接拿会导致读取出来的文件变大,图片读取失败
  138. // 这里主要针对图片来写的,因为我用到的是转成图片,获取图片属性。
  139. is = item.openStream();
  140. if (is.available() > 0) {
  141. }
  142. }
  143. }
  144. } catch (Exception e) {
  145. e.printStackTrace();
  146. // return is;
  147. return map;
  148. } finally {
  149. fileUpload = null;
  150. iter = null;
  151. if (is != null) {
  152. is.close();
  153. is = null;
  154. }
  155. item = null;
  156. }
  157. }
  158. // return is;
  159. return map;
  160. }
  161. }