zoukankan      html  css  js  c++  java
  • Excel导入

                 前几天写了怎么导出Excel,今天要做的是Excel导入数据。Excel导入是个比较常见的场景,一般公司都有工具类,
    笔者还是自己动手写了一个,方便后续参考。
           在导入的过程中,解决了当数字较长时导进来变成了科学计数法的问题。虽然网上好多解决方案,但我发现当确定该单元格的内容是数字后,直接把单元格类型设为字符串类型就可以了,不过07版本以后的excel还是要自己手动转换下。具体见下面代码加粗部分。

    一、maven依赖

    	<!-- poi的jar包  -->
    	<dependency>
    		<groupId>org.apache.poi</groupId> 
    		<artifactId>poi</artifactId>
    		<version>3.15</version>
    	</dependency>
    	<dependency>
    		<groupId>org.apache.poi</groupId>
    		<artifactId>poi-ooxml</artifactId>
    		<version>3.15</version>
    	</dependency>

    二、代码

    	/**
    	 * 解析Excel文件的方法
    	 * @param  excelFile	 Excel文件
    	 * @param  fileName	  文件名
    	 * @return List<ArrayList<String>>
    	 * @throws IOException
    	 */
    	public static List<ArrayList<String>> parseExcel(File excelFile,String fileName) throws IOException{
    		// 判断文件格式
    		boolean isExcel03 = fileName.matches(".+\.(?i)(xls)");//这里的(?i)代表忽略大小写
    		FileInputStream fis = new FileInputStream(excelFile);
    		// 读取工作簿
    		Workbook wb = isExcel03 ? new HSSFWorkbook(fis) : new XSSFWorkbook(fis);
    		// 读取工作表
    		Sheet sheet = wb.getSheetAt(0);	
    		
    		List<ArrayList<String>> resultData = new ArrayList<ArrayList<String>>();	//存放返回数据
    		// 开始每行的读取
    		for (int i = 0; i <= sheet.getLastRowNum(); i++) {
    			Row row = sheet.getRow(i);
    			if(null != row) {
    				ArrayList<String> rowData = new ArrayList<>();
    				short lastCellNum = row.getLastCellNum();
    				for (int j = 0; j < lastCellNum; j++) {
    					Cell cell = row.getCell(j);
    					String cellValue = null;
    					if(null != cell){
    						switch (cell.getCellType()) {
    							case Cell.CELL_TYPE_BLANK:		// 空白
    								cellValue = null;
    								break;
    							case Cell.CELL_TYPE_STRING:		// 文本
    								cellValue = cell.getStringCellValue();
    								break;
    							case Cell.CELL_TYPE_NUMERIC:	// 数字或者日期
    								if(DateUtil.isCellDateFormatted(cell)){		// 是否是日期
    									Date date = cell.getDateCellValue();
    									cellValue = date == null ? null : new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
    								} else {
    									//防止变成因为数字太长变成科学计数法
    									cell.setCellType(CellType.STRING);
    									if(isExcel03){
    										cellValue = cell.getStringCellValue();
    									} else {
    										BigDecimal bd = new BigDecimal(cell.getStringCellValue());
    										cellValue = bd.toPlainString();
    									}
    								}
    								break;
    							case Cell.CELL_TYPE_BOOLEAN:	// 布尔型
    								cellValue = String.valueOf(cell.getBooleanCellValue());
    								break;
    							default:
    								cellValue = null;
    								break;
    						}
    					}
    					rowData.add(cellValue); // 每个单元格数据存入行数据集合
    				}
    				resultData.add(rowData);	// 每行数据存入返回数据集合
    			}
    		}
    		// 关流
    		wb.close();
    		fis.close();
    		return resultData;
    	}



            这个代码其实很简单的,导入这里判读了一下是什么类型的文件,因此需要传入文件名。


    待续。。。
  • 相关阅读:
    Python之协程
    Python之线程 3
    js和jsp之间相互传值
    毕业设计记录
    毕业设计记录16
    mysql select一张表的字段数据insert写入另一张表,同时传入自定义数据
    MySQL防止重复插入相同记录
    毕业设计记录
    解决python mysql插入int型数据报错:TypeError: %d format: a number is required, not str
    毕业设计记录
  • 原文地址:https://www.cnblogs.com/zeng1994/p/7397630.html
Copyright © 2011-2022 走看看