156f69ffeaa5aa2d68318c50a5cf848d13e58994.svn-base 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. package com.sinosoft.em.evaluate.excel;
  2. import java.io.FileInputStream;
  3. import java.io.FileNotFoundException;
  4. import java.io.InputStream;
  5. import java.text.DecimalFormat;
  6. import java.util.ArrayList;
  7. import java.util.HashMap;
  8. import java.util.Iterator;
  9. import java.util.List;
  10. import java.util.Map;
  11. import java.util.Set;
  12. import org.apache.poi.hssf.usermodel.HSSFCell;
  13. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  14. import org.apache.poi.ss.usermodel.Cell;
  15. import org.apache.poi.ss.usermodel.Row;
  16. import org.apache.poi.ss.usermodel.Sheet;
  17. import org.apache.poi.ss.usermodel.Workbook;
  18. import org.apache.poi.xssf.usermodel.XSSFCell;
  19. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  20. import com.sinosoft.em.evaluate.EvalUtils;
  21. import com.sinosoft.lz.system.util.JsonUtil;
  22. public class ImportExcelUtils {
  23. /**
  24. * 适用于没有标题行的excel
  25. * @throws Exception
  26. */
  27. public static Map<String, List<String>> readInnerExcel(String filepath,int beginCol) throws Exception{
  28. String fileType = filepath.substring(filepath.lastIndexOf(".") + 1, filepath.length());
  29. InputStream is = null;
  30. Workbook wb = null;
  31. try {
  32. is = new FileInputStream(filepath);
  33. if (fileType.equals("xls")) {
  34. wb = new HSSFWorkbook(is);
  35. } else if (fileType.equals("xlsx")) {
  36. wb = new XSSFWorkbook(is);
  37. } else {
  38. throw new Exception("读取的不是excel文件");
  39. }
  40. Map<String, List<String>> result = new HashMap<String, List<String>>();
  41. Sheet sheet = wb.getSheetAt(0);
  42. int rowSize = sheet.getLastRowNum() + 1;
  43. for (int i = 3; i < rowSize; i++) {//遍历行
  44. List<String> sheetList = new ArrayList<String>();//对应sheet页
  45. Row row = sheet.getRow(i);
  46. if (row == null) {//略过空行
  47. continue;
  48. }
  49. int cellSize = row.getLastCellNum();//行中有多少个单元格,也就是有多少列
  50. for(int j=0;j<cellSize;j++){
  51. Cell cell = row.getCell(j);
  52. String value = "";
  53. if(cell.getCellType()==Cell.CELL_TYPE_BOOLEAN){
  54. value = String.valueOf(cell.getBooleanCellValue());
  55. }else if(cell.getCellType()==Cell.CELL_TYPE_NUMERIC){
  56. //value = String.valueOf(cell.getNumericCellValue());
  57. Object inputValue = null;// 单元格值
  58. Long longVal = Math.round(cell.getNumericCellValue());
  59. Double doubleVal = cell.getNumericCellValue();
  60. if(Double.parseDouble(longVal + ".0") == doubleVal){ //判断是否含有小数位.0
  61. inputValue = longVal;
  62. }else{
  63. inputValue = doubleVal;
  64. }
  65. DecimalFormat df = new DecimalFormat("#.####");
  66. value = String.valueOf(df.format(inputValue));
  67. }else{
  68. value = String.valueOf(cell.getStringCellValue());
  69. }
  70. if(j>=beginCol && j<=beginCol+8)
  71. sheetList.add(value);
  72. }
  73. result.put(sheetList.get(6)+"|"+sheetList.get(7), sheetList);
  74. }
  75. return result;
  76. } catch (FileNotFoundException e) {
  77. throw e;
  78. } finally {
  79. if (wb != null) {
  80. wb.close();
  81. }
  82. if (is != null) {
  83. is.close();
  84. }
  85. }
  86. }
  87. /**
  88. * 适用于没有标题行的excel--导入外部标准
  89. * @throws Exception
  90. */
  91. public static List<Map<String,String>> readInnerExcelOther(String filepath,int beginCol) throws Exception{
  92. String fileType = filepath.substring(filepath.lastIndexOf(".") + 1, filepath.length());
  93. InputStream is = null;
  94. Workbook wb = null;
  95. try {
  96. is = new FileInputStream(filepath);
  97. if (fileType.equals("xls")) {
  98. wb = new HSSFWorkbook(is);
  99. } else if (fileType.equals("xlsx")) {
  100. wb = new XSSFWorkbook(is);
  101. } else {
  102. throw new Exception("读取的不是excel文件");
  103. }
  104. List<Map<String,String>> result = new ArrayList<Map<String,String>>();
  105. Sheet sheet = wb.getSheetAt(0);
  106. int rowSize = sheet.getLastRowNum() + 1;
  107. for (int i = 2; i < rowSize; i++) {//遍历行
  108. //List<String> sheetList = new ArrayList<String>();//对应sheet页
  109. Map<String, String> map = new HashMap<String, String>();
  110. Row row = sheet.getRow(i);
  111. if (row == null) {//略过空行
  112. continue;
  113. }
  114. int cellSize = row.getLastCellNum();//行中有多少个单元格,也就是有多少列
  115. for(int j=0;j<cellSize;j++){
  116. Cell cell = row.getCell(j);
  117. String value = "";
  118. if(cell.getCellType()==Cell.CELL_TYPE_BOOLEAN){
  119. value = String.valueOf(cell.getBooleanCellValue());
  120. }else if(cell.getCellType()==Cell.CELL_TYPE_NUMERIC){
  121. //value = String.valueOf(cell.getNumericCellValue());
  122. Object inputValue = null;// 单元格值
  123. Long longVal = Math.round(cell.getNumericCellValue());
  124. Double doubleVal = cell.getNumericCellValue();
  125. if(Double.parseDouble(longVal + ".0") == doubleVal){ //判断是否含有小数位.0
  126. inputValue = longVal;
  127. }else{
  128. inputValue = doubleVal;
  129. }
  130. DecimalFormat df = new DecimalFormat("#.####");
  131. value = String.valueOf(df.format(inputValue));
  132. }else{
  133. value = String.valueOf(cell.getStringCellValue());
  134. }
  135. if(j==0){
  136. map.put("idx", value);
  137. }
  138. if(j==1){
  139. map.put("NAME", value);
  140. }
  141. if(j==2){
  142. map.put("STA_SCORE", value);
  143. }
  144. if(j==3){
  145. map.put("SUGGESTION", value);
  146. }
  147. if(j==4){
  148. map.put("CHECK_INSTRUCTION", value);
  149. }
  150. if(j==5){
  151. map.put("DESCRIPTION", value);
  152. }
  153. if(j==6){
  154. map.put("SCORE", value);
  155. }
  156. if(j==7){
  157. map.put("QUESTIONS", value);
  158. }
  159. if(j==8){
  160. map.put("SUGGESTIONS", value);
  161. }
  162. if(j==9){
  163. map.put("BASIS_ID", value);
  164. }
  165. if(j==10){
  166. map.put("FD_OBJECTID", value);
  167. }
  168. }
  169. result.add(map);
  170. }
  171. return result;
  172. } catch (FileNotFoundException e) {
  173. throw e;
  174. } finally {
  175. if (wb != null) {
  176. wb.close();
  177. }
  178. if (is != null) {
  179. is.close();
  180. }
  181. }
  182. }
  183. public static Map<String, String> getTypicalProblems(Map<String, String> remap, Map<String, List<String>> excelMap) {
  184. Set<String> keys = excelMap.keySet();
  185. Iterator<String> i = keys.iterator();
  186. List<Map<String,String>> typicallist = new ArrayList<Map<String,String>>();
  187. String typicalProblems = "";
  188. String score = "";
  189. String questions = "";
  190. String suggestions = "";
  191. String real_name = "";
  192. while(i.hasNext()){
  193. String key = i.next();
  194. if(key.contains(remap.get("FD_OBJECTID"))){
  195. if(!"".equals(excelMap.get(key).get(7))){
  196. boolean isnumber = isNumber(excelMap.get(key).get(2));
  197. //typicalProblems += (EvalUtils.formatXiaoShu(excelMap.get(key).get(1))+excelMap.get(key).get(2)+"\n");
  198. if("".equals(excelMap.get(key).get(2))){
  199. typicalProblems += (excelMap.get(key).get(1)+excelMap.get(key).get(2)+"\n");
  200. }else{
  201. typicalProblems += (excelMap.get(key).get(1)+(isnumber?" 共扣"+excelMap.get(key).get(2)+"分\n":" ("+excelMap.get(key).get(2)+")\n"));
  202. }
  203. Map<String,String> map = new HashMap<String,String>();
  204. map.put("basis_id", excelMap.get(key).get(7));
  205. map.put("business_id", excelMap.get(key).get(8));
  206. map.put("basis_score", EvalUtils.formatXiaoShu(excelMap.get(key).get(2)));
  207. map.put("basis_text", excelMap.get(key).get(1));
  208. map.put("question", excelMap.get(key).get(3));
  209. map.put("suggestion", excelMap.get(key).get(4));
  210. String ren = excelMap.get(key).get(5);
  211. typicallist.add(map);
  212. }
  213. if(!"".equals(excelMap.get(key).get(0)))
  214. score = EvalUtils.formatXiaoShu(excelMap.get(key).get(0));
  215. if(!"".equals(excelMap.get(key).get(3)))
  216. questions += (excelMap.get(key).get(3))+"\n";
  217. if(!"".equals(excelMap.get(key).get(4)))
  218. suggestions += (excelMap.get(key).get(4))+"\n";
  219. if(!"".equals(excelMap.get(key).get(5)))
  220. real_name =excelMap.get(key).get(5);
  221. }
  222. }
  223. remap.put("typicalProblems", typicalProblems);
  224. remap.put("typicallist", JsonUtil.list2json(typicallist));
  225. remap.put("score", score);
  226. remap.put("questions", questions);
  227. remap.put("suggestions", suggestions);
  228. remap.put("real_name", real_name);
  229. return remap;
  230. }
  231. //判断是否为数字
  232. public static boolean isNumber(String str){
  233. String reg = "^[0-9]+(.[0-9]+)?$";
  234. return str.matches(reg);
  235. }
  236. public static void main(String[] args) {
  237. //String filePath = "D:\\Users\\len\\Desktop\\excelDemo\\ceshi.xlsx";
  238. String filePath = "D:\\testdaoru.xlsx";
  239. try {
  240. /*Map<String, List<String>> map = ImportExcelUtils.readInnerExcelOther(filePath,6);*/
  241. Map<String, List<String>> map = ImportExcelUtils.readInnerExcel(filePath,14);
  242. System.out.println(JsonUtil.map2json(map));
  243. } catch (Exception e) {
  244. // TODO Auto-generated catch block
  245. e.printStackTrace();
  246. }
  247. }
  248. }