front desk:
$.ajax({ type: "get", url: consts.dataServerHost+'/backend name/data/getRainData', dataType: "jsonp", jsonp: 'callbackName', data: { path: "excel file path", dataFormat:"jsonp" }, success: function (data) { let htmlStr = ""; for (let i in data) { htmlStr += `<li class='datetime-list' id='pe-${data[i]}'><span>${data[i]}</span></li>`; } $("#rainfall-date").html(htmlStr); for (let i in data) { $("#pe-" + data[i]).click(function () { let checked = false; if ($(this).hasClass("actived")) { $(this).removeClass("actived"); $(".rainfall-datetime ul li").removeClass("actived"); } else { $(".rainfall-datetime ul li").removeClass("actived"); $(this).addClass("actived"); checked = true; // evt.fire("syscontrol_baseinfo", { firer: this, data: { type: "rainfall", display: checked} }); } }); } }, error: function (e) { } });Backstage:
The two file locations are as follows:
dataManager:
@RequestMapping("/getRainData") public String getRainData(HttpServletRequest req, HttpServletResponse res) throws Exception { String localpath=req.getParameter("path"); File file = new File(localpath); String dateStr = ""; JSONArray jsonArray = new JSONArray(); if(file.exists()){ ArrayList<ArrayList<Object>> result = ExcelUtil.readExcel(file); for(int i = 0 ;i < result.size() ;i++){ if(!result.get(i).get(0).toString().isEmpty()&&!result.get(i).get(0).toString().equals("时间")){ // System.out.println(result.get(i).get(0).toString().substring(0,10)); String temp = result.get(i).get(0).toString().substring(0,10); if(!dateStr.contains(temp)) dateStr += temp+","; } } dateStr = dateStr.substring(0,dateStr.length()-1); String [] list = dateStr.split(","); for(int i=0;i<list.length;i++){ jsonArray.add(list[i]); } } if (req.getParameter("dataFormat") != null) { String dataFormat = req.getParameter("dataFormat").trim().toLowerCase(); if (dataFormat.equals("jsonp")) { String jsonp = req.getParameter("callbackName"); return jsonp + "(" + jsonArray.toString() + ")"; } } return jsonArray.toString(); }
excel:
//Default format when cell content is number private static DecimalFormat df = new DecimalFormat("0"); // Default cell formatted date string private static SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss"); // format the number private static DecimalFormat nf = new DecimalFormat("0.00"); public static ArrayList<ArrayList<Object>> readExcel(File file){ if(file == null){ return null; } if(file.getName().endsWith("xlsx")){ //Process ecxel2007 return readExcel2007(file); }else{ //Process ecxel2003 return readExcel2003(file); } } /* * @return stores the returned result in an ArrayList, the storage structure is similar to that of a two-digit array * lists.get(0).get(0) means cells with 0 rows and 0 columns in Excel in the past */ public static ArrayList<ArrayList<Object>> readExcel2003(File file){ try{ ArrayList<ArrayList<Object>> rowList = new ArrayList<ArrayList<Object>>(); ArrayList<Object> colList; HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(file)); HSSFSheet sheet = wb.getSheetAt(0); HSSFRow row; HSSFCell cell; Object value; for(int i = sheet.getFirstRowNum() , rowCount = 0; rowCount < sheet.getPhysicalNumberOfRows() ; i++ ){ row = sheet.getRow(i); colList = new ArrayList<Object>(); if(row == null){ // when the read row is empty if(i != sheet.getPhysicalNumberOfRows()){//Determine whether it is the last row rowList.add(colList); } continue; }else{ rowCount++; } for( int j = row.getFirstCellNum() ; j <= row.getLastCellNum() ;j++){ cell = row.getCell(j); if(cell == null || cell.getCellType() == HSSFCell.CELL_TYPE_BLANK){ // when the cell is empty if(j != row.getLastCellNum()){//Determine whether it is the last cell in the row colList.add(""); } continue; } switch(cell.getCellType()){ case XSSFCell.CELL_TYPE_STRING: //System.out.println(i + "行" + j + " 列 is String type"); value = cell.getStringCellValue(); break; case XSSFCell.CELL_TYPE_NUMERIC: if ("@".equals(cell.getCellStyle().getDataFormatString())) { value = df.format(cell.getNumericCellValue()); } else if ("General".equals(cell.getCellStyle() .getDataFormatString())) { value = nf.format(cell.getNumericCellValue()); } else { value = sdf.format(HSSFDateUtil.getJavaDate(cell .getNumericCellValue())); } //System.out.println(i + "行" + j // + " 列 is Number type ; DateFormt:" // + value.toString()); break; case XSSFCell.CELL_TYPE_BOOLEAN: //System.out.println(i + "行" + j + " 列 is Boolean type"); value = Boolean.valueOf(cell.getBooleanCellValue()); break; case XSSFCell.CELL_TYPE_BLANK: //System.out.println(i + "行" + j + " 列 is Blank type"); value = ""; break; default: //System.out.println(i + "行" + j + " 列 is default type"); value = cell.toString(); }// end switch colList.add(value); }//end for j rowList.add(colList); }//end for i return rowList; }catch(Exception e){ return null; } } public static ArrayList<ArrayList<Object>> readExcel2007(File file){ try{ ArrayList<ArrayList<Object>> rowList = new ArrayList<ArrayList<Object>>(); ArrayList<Object> colList; XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(file)); XSSFSheet sheet = wb.getSheetAt(0); XSSFRow row; XSSFCell cell; Object value; for(int i = sheet.getFirstRowNum() , rowCount = 0; rowCount < sheet.getPhysicalNumberOfRows() ; i++ ){ row = sheet.getRow(i); colList = new ArrayList<Object>(); if(row == null){ // when the read row is empty if(i != sheet.getPhysicalNumberOfRows()){//Determine whether it is the last row rowList.add(colList); } continue; }else{ rowCount++; } for( int j = row.getFirstCellNum() ; j <= row.getLastCellNum() ;j++){ cell = row.getCell(j); if(cell == null || cell.getCellType() == HSSFCell.CELL_TYPE_BLANK){ // when the cell is empty if(j != row.getLastCellNum()){//Determine whether it is the last cell in the row colList.add(""); } continue; } switch(cell.getCellType()){ case XSSFCell.CELL_TYPE_STRING: //System.out.println(i + "行" + j + " 列 is String type"); value = cell.getStringCellValue(); break; case XSSFCell.CELL_TYPE_NUMERIC: if ("@".equals(cell.getCellStyle().getDataFormatString())) { value = df.format(cell.getNumericCellValue()); } else if ("General".equals(cell.getCellStyle() .getDataFormatString())) { value = nf.format(cell.getNumericCellValue()); } else { value = sdf.format(HSSFDateUtil.getJavaDate(cell .getNumericCellValue())); } //System.out.println(i + "行" + j // + " 列 is Number type ; DateFormt:" // + value.toString()); break; case XSSFCell.CELL_TYPE_BOOLEAN: //System.out.println(i + "行" + j + " 列 is Boolean type"); value = Boolean.valueOf(cell.getBooleanCellValue()); break; case XSSFCell.CELL_TYPE_BLANK: //System.out.println(i + "行" + j + " 列 is Blank type"); value = ""; break; default: //System.out.println(i + "行" + j + " 列 is default type"); value = cell.toString(); }// end switch colList.add(value); }//end for j rowList.add(colList); }//end for i return rowList; }catch(Exception e){ //System.out.println("exception"); return null; } } public static void writeExcel(ArrayList<ArrayList<Object>> result,String path){ if(result == null){ return; } HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet("sheet1"); for(int i = 0 ;i < result.size() ; i++){ HSSFRow row = sheet.createRow(i); if(result.get(i) != null){ for(int j = 0; j < result.get(i).size() ; j ++){ HSSFCell cell = row.createCell(j); cell.setCellValue(result.get(i).get(j).toString()); } } } ByteArrayOutputStream os = new ByteArrayOutputStream(); try { wb.write(os); } catch (IOException e){ e.printStackTrace (); } byte[] content = os.toByteArray(); File file = new File(path);//The location where the Excel file is stored after it is generated. OutputStream fos = null; try { fos = new FileOutputStream(file); fos.write(content); os.close(); fos.close(); }catch (Exception e){ e.printStackTrace (); } } public static DecimalFormat getDf() { return df; } public static void setDf(DecimalFormat df) { ExcelUtil.df = df; } public static SimpleDateFormat getSdf() { return sdf; } public static void setSdf(SimpleDateFormat sdf) { ExcelUtil.sdf = sdf; } public static DecimalFormat getNf() { return nf; } public static void setNf(DecimalFormat nf) { ExcelUtil.nf = nf; }