|  |  | @ -4,7 +4,6 @@ import java.io.FileNotFoundException; | 
			
		
	
		
		
			
				
					
					|  |  |  | import java.io.IOException; |  |  |  | import java.io.IOException; | 
			
		
	
		
		
			
				
					
					|  |  |  | import java.lang.reflect.Field; |  |  |  | import java.lang.reflect.Field; | 
			
		
	
		
		
			
				
					
					|  |  |  | import java.util.ArrayList; |  |  |  | import java.util.ArrayList; | 
			
		
	
		
		
			
				
					
					|  |  |  | import java.util.Date; |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | import java.util.List; |  |  |  | import java.util.List; | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | import org.apache.poi.hssf.usermodel.HSSFCell; |  |  |  | import org.apache.poi.hssf.usermodel.HSSFCell; | 
			
		
	
	
		
		
			
				
					|  |  | @ -16,9 +15,6 @@ import org.apache.poi.xssf.usermodel.XSSFFont; | 
			
		
	
		
		
			
				
					
					|  |  |  | import org.apache.poi.xssf.usermodel.XSSFRow; |  |  |  | import org.apache.poi.xssf.usermodel.XSSFRow; | 
			
		
	
		
		
			
				
					
					|  |  |  | import org.apache.poi.xssf.usermodel.XSSFSheet; |  |  |  | import org.apache.poi.xssf.usermodel.XSSFSheet; | 
			
		
	
		
		
			
				
					
					|  |  |  | import org.apache.poi.xssf.usermodel.XSSFWorkbook; |  |  |  | import org.apache.poi.xssf.usermodel.XSSFWorkbook; | 
			
		
	
		
		
			
				
					
					|  |  |  | import org.springframework.core.io.ClassPathResource; |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | import com.base.Constant; |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | /** |  |  |  | /** | 
			
		
	
		
		
			
				
					
					|  |  |  |  * @author chen |  |  |  |  * @author chen | 
			
		
	
	
		
		
			
				
					|  |  | @ -36,12 +32,7 @@ public class ExcelOperation { | 
			
		
	
		
		
			
				
					
					|  |  |  | 	 * @throws IllegalArgumentException |  |  |  | 	 * @throws IllegalArgumentException | 
			
		
	
		
		
			
				
					
					|  |  |  | 	 * @throws IllegalAccessException |  |  |  | 	 * @throws IllegalAccessException | 
			
		
	
		
		
			
				
					
					|  |  |  | 	 *///从第几行 填充数据
 |  |  |  | 	 *///从第几行 填充数据
 | 
			
		
	
		
		
			
				
					
					|  |  |  | 	public static XSSFWorkbook writeExcelTemplate(String templateFilePath,int excelInitRow, List list) throws FileNotFoundException, IOException, IllegalArgumentException, IllegalAccessException { |  |  |  | 	public static XSSFWorkbook writeExcelTemplate(XSSFWorkbook workbook, int excelInitRow, List list) throws FileNotFoundException, IOException, IllegalArgumentException, IllegalAccessException { | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  | //		InputStreamReader in= new InputStreamReader(Resource.class.getResourceAsStream(templateFilePath), "UTF-8");
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | 		ClassPathResource res = new ClassPathResource(templateFilePath); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | 		//此处 耗时太久:(原因:excel 2007 以后采用的方式不同造成的建议升级 到 poi 3.15以上)
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | 		XSSFWorkbook workbook = new XSSFWorkbook(res.getInputStream()); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //		System.out.println(" new XSSFWorkbook: "+ (date2.getTime()-date.getTime()));
 |  |  |  |  | 
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  | 		 |  |  |  | 		 | 
			
		
	
		
		
			
				
					
					|  |  |  | 		 //读取工作表
 |  |  |  | 		 //读取工作表
 | 
			
		
	
		
		
			
				
					
					|  |  |  | 		XSSFSheet sheet = workbook.getSheetAt(0);//getSheet("Sheet1");
 |  |  |  | 		XSSFSheet sheet = workbook.getSheetAt(0);//getSheet("Sheet1");
 | 
			
		
	
	
		
		
			
				
					|  |  | @ -139,7 +130,7 @@ public class ExcelOperation { | 
			
		
	
		
		
			
				
					
					|  |  |  | 	 * @throws IllegalAccessException  |  |  |  | 	 * @throws IllegalAccessException  | 
			
		
	
		
		
			
				
					
					|  |  |  | 	 * @throws InstantiationException  |  |  |  | 	 * @throws InstantiationException  | 
			
		
	
		
		
			
				
					
					|  |  |  | 	 */ |  |  |  | 	 */ | 
			
		
	
		
		
			
				
					
					|  |  |  | 	public static List readExcelTemplate(XSSFWorkbook workbook, int excelInitRow, Class<?> classz) throws InstantiationException, IllegalAccessException { |  |  |  | 	public static List readExcel4Update(XSSFWorkbook workbook, int excelInitRow, Class<?> classz) throws InstantiationException, IllegalAccessException { | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  | 		List list = null; |  |  |  | 		List list = null; | 
			
		
	
		
		
			
				
					
					|  |  |  | 		 //读取工作表
 |  |  |  | 		 //读取工作表
 | 
			
		
	
		
		
			
				
					
					|  |  |  | 		XSSFSheet sheet = workbook.getSheetAt(0);//getSheet("Sheet1");
 |  |  |  | 		XSSFSheet sheet = workbook.getSheetAt(0);//getSheet("Sheet1");
 | 
			
		
	
	
		
		
			
				
					|  |  | @ -159,6 +150,10 @@ public class ExcelOperation { | 
			
		
	
		
		
			
				
					
					|  |  |  | 	        for (int i = excelInitRow; i < rowNum; i++) { |  |  |  | 	        for (int i = excelInitRow; i < rowNum; i++) { | 
			
		
	
		
		
			
				
					
					|  |  |  | 	        	//获得当前行
 |  |  |  | 	        	//获得当前行
 | 
			
		
	
		
		
			
				
					
					|  |  |  | 	        	row = sheet.getRow(i); |  |  |  | 	        	row = sheet.getRow(i); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 	        	cell = row.getCell(0); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 	        	if ("-".equals(getValue(cell)) || "".equals(getValue(cell))) { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 					continue; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 				} | 
			
		
	
		
		
			
				
					
					|  |  |  | 	        	//实例化
 |  |  |  | 	        	//实例化
 | 
			
		
	
		
		
			
				
					
					|  |  |  | 	        	Object newObj = classz.newInstance(); |  |  |  | 	        	Object newObj = classz.newInstance(); | 
			
		
	
		
		
			
				
					
					|  |  |  | 	        	Field[] fields = newObj.getClass().getDeclaredFields(); |  |  |  | 	        	Field[] fields = newObj.getClass().getDeclaredFields(); | 
			
		
	
	
		
		
			
				
					|  |  | @ -168,12 +163,22 @@ public class ExcelOperation { | 
			
		
	
		
		
			
				
					
					|  |  |  | 	        		cell = row.getCell(j); |  |  |  | 	        		cell = row.getCell(j); | 
			
		
	
		
		
			
				
					
					|  |  |  | 	        		if (null != cell) { |  |  |  | 	        		if (null != cell) { | 
			
		
	
		
		
			
				
					
					|  |  |  | 		        		fields[j].setAccessible(true); |  |  |  | 		        		fields[j].setAccessible(true); | 
			
		
	
		
		
			
				
					
					|  |  |  | 		        		String cellString = cell.getStringCellValue(); |  |  |  | 		        		String cellString = getValue(cell); | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  | 		        		if (!"".equals(cellString)) { |  |  |  | 		        		if (!"".equals(cellString)) { | 
			
		
	
		
		
			
				
					
					|  |  |  | 		        			isAddObj = true; |  |  |  | 		        			isAddObj = true; | 
			
		
	
		
		
			
				
					
					|  |  |  | 			        		//int型
 |  |  |  | 			        		//int型
 | 
			
		
	
		
		
			
				
					
					|  |  |  | 					    	if (fields[j].getType().toString().contains("Integer")) { |  |  |  | 					    	if (fields[j].getType().toString().contains("Integer") ||fields[j].getType().toString().contains("int")) { | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  | 					    		fields[j].set(newObj, Integer.valueOf(cellString)); |  |  |  | 					    		double dnum = Double.valueOf(cellString); | 
			
				
				
			
		
	
		
		
	
		
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 					    		int inum = (int) dnum; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 					    		fields[j].set(newObj, inum); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 							} | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 					    	//double型
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 					    	else if (fields[j].getType().toString().contains("Double") ||fields[j].getType().toString().contains("double")) { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 					    		fields[j].set(newObj, Double.valueOf(cellString)); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 							} | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 					    	//boolean型
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 					    	else if (fields[j].getType().toString().contains("boolean") ||fields[j].getType().toString().contains("Boolean")) { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 					    		fields[j].set(newObj, Boolean.valueOf(cellString)); | 
			
		
	
		
		
			
				
					
					|  |  |  | 							} |  |  |  | 							} | 
			
		
	
		
		
			
				
					
					|  |  |  | 					    	else { |  |  |  | 					    	else { | 
			
		
	
		
		
			
				
					
					|  |  |  | 					    		fields[j].set(newObj, cellString); |  |  |  | 					    		fields[j].set(newObj, cellString); | 
			
		
	
	
		
		
			
				
					|  |  | @ -190,4 +195,25 @@ public class ExcelOperation { | 
			
		
	
		
		
			
				
					
					|  |  |  | 		return list; |  |  |  | 		return list; | 
			
		
	
		
		
			
				
					
					|  |  |  | 	} |  |  |  | 	} | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 	/** 获得cell里的字符串 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 	 * @param cell | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 	 * @return | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 	 */ | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 	private static String getValue(XSSFCell cell) { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 		String value =""; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 		switch (cell.getCellType()) { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 			case XSSFCell.CELL_TYPE_NUMERIC: | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 				value = String.valueOf(cell.getNumericCellValue()); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 				break; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 			case XSSFCell.CELL_TYPE_BOOLEAN: | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 				value = String.valueOf(cell.getBooleanCellValue()); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 				break; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 			case XSSFCell.CELL_TYPE_STRING: | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 				value = cell.getStringCellValue(); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 				break; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 			default: | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 				break; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 		} | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 		return value.trim(); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 	} | 
			
		
	
		
		
			
				
					
					|  |  |  | } |  |  |  | } | 
			
		
	
	
		
		
			
				
					|  |  | 
 |