package com.jevon.controller; import com.jevon.model.*; import com.jevon.vo.BaseVo; import com.jevon.vo.req.InitAnalyseReqVo; import io.swagger.annotations.Api; import org.apache.commons.lang3.StringUtils; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.io.File; import java.io.FileInputStream; import java.util.*; /** * @author chen * @version 1.0 * @date 2019/10/17 0017 13:33 */ @RestController @RequestMapping(value = "/learn") @Api(value = "学情分析") public class LearnController { public BaseVo initAnalyse(@RequestBody InitAnalyseReqVo initAnalyseReqVo){ BaseVo baseVo = new BaseVo(); Date date = new Date(); try{ String fileUrl = initAnalyseReqVo.getUrl(); File excelFile = new File(fileUrl); // 获得工作簿 String file = excelFile.getName(); Workbook workbook = null; if (file.endsWith("xls")) { workbook = new HSSFWorkbook(new FileInputStream(excelFile)); } else { workbook = new XSSFWorkbook(new FileInputStream(excelFile)); } // 获得工作表 Sheet sheet = workbook.getSheetAt(0); int rows = sheet.getPhysicalNumberOfRows(); int column = 0 ; int courseNumber = 0; for (int i = 0; i < rows; i++) { // 获取第i行数据 Row sheetRow = sheet.getRow(i); if (i == 0) { //获取标题 String title = sheetRow.getCell(0).getStringCellValue().trim(); }else if(i == 1){ //获取表头 int j = 0 ; while (sheetRow.getCell(j) != null && StringUtils.isNotBlank(sheetRow.getCell(j).getStringCellValue().trim())) { j++ ; } column = j ; } else { for (int j = 0; j < column; j++) { Cell cell = sheetRow.getCell(j); if(cell != null){ cell.setCellType(CellType.STRING); } } } } }catch (Exception e){ System.out.println(e); baseVo.setSuccess(false); } return baseVo; } }