POI读取xls文件和xlsx文件

直接放代码:

/**
	 * read the Excel 2003-2007 .xls
	 */
	public static List<ArrayList<String>> readXls(MultipartFile file){
		List<ArrayList<String>> list = new ArrayList<>();
		// IO流读取文件
		InputStream input = null;
		HSSFWorkbook wb = null;
		ArrayList<String> rowList = null;
		try {
			input = file.getInputStream();
			// 创建文档
			wb = new HSSFWorkbook(input);
			//读取sheet(页)
			for(int numSheet=0;numSheet<wb.getNumberOfSheets();numSheet++){
				HSSFSheet hssfSheet = wb.getSheetAt(numSheet);
				if(hssfSheet == null){
					continue;
				}
				totalRows = hssfSheet.getLastRowNum();
				//读取Row,从第二行开始
				for(int rowNum = 1;rowNum <= totalRows;rowNum++){
					HSSFRow hssfRow = hssfSheet.getRow(rowNum);
					if(hssfRow!=null){
						rowList = new ArrayList<>();
						totalCells = hssfRow.getLastCellNum();
						//读取列,从第一列开始
						for(short c=0;c<=totalCells+1;c++){
							HSSFCell cell = hssfRow.getCell(c);
							if(cell==null){
								continue;
							}
							cell.setCellType(Cell.CELL_TYPE_STRING);
							if(cell.getStringCellValue().trim().equals("")){
								rowList.add("");
								continue;
							}
							rowList.add(cell.getStringCellValue());
						}
						list.add(rowList);
					}
				}
			}
			return list;
		} catch (IOException e) {
			e.printStackTrace();
		} finally{
			try {
				input.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

下面是xlsx的读取

public static List<ArrayList<String>> readXlsx(MultipartFile file) {
		List<ArrayList<String>> list = new ArrayList<>();
		// IO流读取文件
		InputStream input = null;
		XSSFWorkbook wb = null;
		try {
			input = file.getInputStream();
			wb = new XSSFWorkbook(input);
			for(int numSheet=0;numSheet<wb.getNumberOfSheets();numSheet++){
				XSSFSheet xssfSheet = wb.getSheetAt(numSheet);
				if (xssfSheet == null) {
					continue;
				}

				for (int rowNum = 1; rowNum <= xssfSheet.getLastRowNum(); rowNum++) {
					XSSFRow xssfRow = xssfSheet.getRow(rowNum);
					if (xssfRow != null) {
						int minColIx = xssfRow.getFirstCellNum();
						int maxColIx = xssfRow.getLastCellNum();
						ArrayList<String> rowList = new ArrayList<String>();
						for (int colIx = minColIx; colIx < maxColIx; colIx++) {
							XSSFCell cell = xssfRow.getCell(colIx, Row.RETURN_BLANK_AS_NULL);
							if (cell == null) {
								rowList.add("");
								continue;
							}
							if (cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) {
								if (XSSFDateUtil.isCellDateFormatted(cell)) {
									rowList.add(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(XSSFDateUtil.getJavaDate(cell.getNumericCellValue())));
								} else {
									rowList.add(new BigDecimal(cell.getNumericCellValue()) + "");
								}
								continue;
							}
							cell.setCellType(Cell.CELL_TYPE_STRING);
							if (cell.getStringCellValue().trim().equals("")) {
								rowList.add("");
								continue;
							}
							rowList.add(cell.getStringCellValue());
						}
						list.add(rowList);
					}
				}
			}
            return list;
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				input.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

值得注意的是有时候导入的excel中的时间格式难处理,上面中的这段代码可以解决:

if (cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) {
								if (XSSFDateUtil.isCellDateFormatted(cell)) {
									rowList.add(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(XSSFDateUtil.getJavaDate(cell.getNumericCellValue())));
								} else {
									rowList.add(new BigDecimal(cell.getNumericCellValue()) + "");
								}

其中先判断这一列中的字段格式是否数字类型:cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC,如果是数字类型的,则再次判断是否是日期格式 ->
XSSFDateUtil.isCellDateFormatted(cell),是则格式化,否则则将其转化为字符串处理,
还需注意的是,因为数字类型excel默认转double的科学计数法,直接转String类型的话会造成x.xxxxxxxxEx的字符串,解决方法时先转成大数 -> new BigDecimal(),再将大数转为string,可以用String.valueOf(),我这直接 + “”,结果一样~

你可能感兴趣的:(Java,cookies)