java导入功能带检测反馈(详细注释)

其中url地址自行填充

底层工具类:

public List<Map<String, Object>> excelImport(final MultipartFile file, final String extension, final int startRom,
			final String[] fieldNames) throws IOException {

		Map<String, Object> map = null;
		final List<Map<String, Object>> mapList = new LinkedList<Map<String, Object>>();

		if (extension.equals("xls")) {
			HSSFWorkbook workBook = new HSSFWorkbook(file.getInputStream());
			final HSSFSheet sheet = workBook.getSheetAt(0);
			final int rows = sheet.getPhysicalNumberOfRows();

			for (int i = startRom; i < rows; i ++ ) {
				final HSSFRow row = sheet.getRow(i);
				if (row != null) {
					map = new HashMap<String, Object>();
					final int cells = row.getPhysicalNumberOfCells();
					for (int j = 0; j < cells; j ++ ) {
						final HSSFCell cell = row.getCell(j);
						if (cell != null) {
							switch (cell.getCellType()) {
								case XSSFCell.CELL_TYPE_FORMULA :
									switch (cell.getCachedFormulaResultType()) {
										case XSSFCell.CELL_TYPE_NUMERIC :
											map.put(fieldNames[j], cell.getNumericCellValue() + "");
											break;
										case XSSFCell.CELL_TYPE_STRING :
											map.put(fieldNames[j], cell.getStringCellValue());
											break;
										default :
											map.put(fieldNames[j], "");
											break;
									}

									break;
								case HSSFCell.CELL_TYPE_NUMERIC :
									if (String.valueOf(cell.getNumericCellValue()).matches(".*[E|e].*")) {
										final DecimalFormat df = new DecimalFormat("#.#");
										// 指定最长小数点位为10
										df.setMaximumFractionDigits(10);
										map.put(fieldNames[j],
												StringUtils.trimToEmpty(df.format(cell.getNumericCellValue())));
									} else if (HSSFDateUtil.isCellDateFormatted(cell)) {
										// add by dgx 2017-08-31 for
										// 获取单元格内容为日期格式的数据
										SimpleDateFormat sdf = null;
										if (cell.getCellStyle().getDataFormat() == HSSFDataFormat
												.getBuiltinFormat("h:mm")) {
											sdf = new SimpleDateFormat("HH:mm");
										} else {
											sdf = new SimpleDateFormat("yyyy-MM-dd");
										}

										if (null != cell.getDateCellValue()
												&& ! cell.getDateCellValue().toString().equals("")) {
											final Date date = cell.getDateCellValue();
											map.put(fieldNames[j], sdf.format(date));
										} else {
											map.put(fieldNames[j]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值