From a03f86d6ca20f5aa529444eeef4d58f918321d4a Mon Sep 17 00:00:00 2001 From: chenlw <874313221@qq.com> Date: Fri, 21 Oct 2016 09:02:16 +0800 Subject: [PATCH] =?UTF-8?q?excel=EF=BC=8C=E8=AF=BB=E5=86=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../utils/excelUtils/ExcelOperation.java | 54 ++++++++++++++----- 1 file changed, 40 insertions(+), 14 deletions(-) diff --git a/src/com/platform/utils/excelUtils/ExcelOperation.java b/src/com/platform/utils/excelUtils/ExcelOperation.java index cb7df7ab..c7cb9563 100644 --- a/src/com/platform/utils/excelUtils/ExcelOperation.java +++ b/src/com/platform/utils/excelUtils/ExcelOperation.java @@ -4,7 +4,6 @@ import java.io.FileNotFoundException; import java.io.IOException; import java.lang.reflect.Field; import java.util.ArrayList; -import java.util.Date; import java.util.List; 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.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; -import org.springframework.core.io.ClassPathResource; - -import com.base.Constant; /** * @author chen @@ -36,12 +32,7 @@ public class ExcelOperation { * @throws IllegalArgumentException * @throws IllegalAccessException *///从第几行 填充数据 - public static XSSFWorkbook writeExcelTemplate(String templateFilePath,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())); + public static XSSFWorkbook writeExcelTemplate(XSSFWorkbook workbook, int excelInitRow, List list) throws FileNotFoundException, IOException, IllegalArgumentException, IllegalAccessException { //读取工作表 XSSFSheet sheet = workbook.getSheetAt(0);//getSheet("Sheet1"); @@ -139,7 +130,7 @@ public class ExcelOperation { * @throws IllegalAccessException * @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; //读取工作表 XSSFSheet sheet = workbook.getSheetAt(0);//getSheet("Sheet1"); @@ -159,6 +150,10 @@ public class ExcelOperation { for (int i = excelInitRow; i < rowNum; i++) { //获得当前行 row = sheet.getRow(i); + cell = row.getCell(0); + if ("-".equals(getValue(cell)) || "".equals(getValue(cell))) { + continue; + } //实例化 Object newObj = classz.newInstance(); Field[] fields = newObj.getClass().getDeclaredFields(); @@ -168,12 +163,22 @@ public class ExcelOperation { cell = row.getCell(j); if (null != cell) { fields[j].setAccessible(true); - String cellString = cell.getStringCellValue(); + String cellString = getValue(cell); if (!"".equals(cellString)) { isAddObj = true; //int型 - if (fields[j].getType().toString().contains("Integer")) { - fields[j].set(newObj, Integer.valueOf(cellString)); + if (fields[j].getType().toString().contains("Integer") ||fields[j].getType().toString().contains("int")) { + 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 { fields[j].set(newObj, cellString); @@ -190,4 +195,25 @@ public class ExcelOperation { 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(); + } }