From 978423610ba0504ad90bc0a70eb827d6a741223a Mon Sep 17 00:00:00 2001 From: chenlw <874313221@qq.com> Date: Wed, 19 Oct 2016 10:28:23 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AF=BB=E5=8F=96excel=E4=B8=BA=20javabean?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../utils/excelUtils/ExcelOperation.java | 91 ++++++++++++++++--- 1 file changed, 79 insertions(+), 12 deletions(-) diff --git a/src/com/platform/utils/excelUtils/ExcelOperation.java b/src/com/platform/utils/excelUtils/ExcelOperation.java index f962c28b..cb7df7ab 100644 --- a/src/com/platform/utils/excelUtils/ExcelOperation.java +++ b/src/com/platform/utils/excelUtils/ExcelOperation.java @@ -3,6 +3,7 @@ package com.platform.utils.excelUtils; 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; @@ -25,26 +26,23 @@ import com.base.Constant; */ public class ExcelOperation { - /** 读取excel模板 + /** 写入excel * @param templateFilePath * @param list list中对象的属性顺序和模板的列顺序一一对应 + * @param excelInitRow //从第excelInitRow行 开始填充数据,( Constant.EXCEL_TEMPLATE_INIT_ROW) * @return HSSFWorkbook * @throws FileNotFoundException * @throws IOException - * @throws IllegalAccessException - * @throws IllegalArgumentException - */ - public static XSSFWorkbook writeExcelTemplate(String templateFilePath, List list) throws FileNotFoundException, IOException, IllegalArgumentException, IllegalAccessException { + * @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以上) - Date date = new Date(); XSSFWorkbook workbook = new XSSFWorkbook(res.getInputStream()); - Date date2 = new Date(); - System.out.println(" new XSSFWorkbook: "+ (date2.getTime()-date.getTime())); +// System.out.println(" new XSSFWorkbook: "+ (date2.getTime()-date.getTime())); - //从第几行 填充数据 - int excelInitRow = Constant.EXCEL_TEMPLATE_INIT_ROW; //读取工作表 XSSFSheet sheet = workbook.getSheetAt(0);//getSheet("Sheet1"); XSSFRow row; @@ -61,9 +59,15 @@ public class ExcelOperation { fields[j].setAccessible(true); cell = row.createCell((short) j,HSSFCellStyle.ALIGN_CENTER); cell.setCellType(HSSFCell.CELL_TYPE_STRING); - Object value = fields[j].get(obj); + Object value = fields[j].get(obj); if (null != value) { - cell.setCellValue(value.toString()); + //int型 + if (fields[j].getType().toString().contains("Integer")) { + cell.setCellValue(String.valueOf(value)); + } + else { + cell.setCellValue(value.toString()); + } } else { cell.setCellValue(""); @@ -85,6 +89,10 @@ public class ExcelOperation { return workbook; } + /** excel的cell样式设置 + * @param workbook + * @return + */ private static XSSFCellStyle getStyle(XSSFWorkbook workbook) { // 设置字体; XSSFFont font = workbook.createFont(); @@ -122,5 +130,64 @@ public class ExcelOperation { style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); return style; } + + /** 读取excel模板 + * @param workbook excel的XSSFWorkbook + * @param excelInitRow 读取数据的起始行 + * @param classz excel的每行数据导出 到的 类 + * @return + * @throws IllegalAccessException + * @throws InstantiationException + */ + public static List readExcelTemplate(XSSFWorkbook workbook, int excelInitRow, Class classz) throws InstantiationException, IllegalAccessException { + List list = null; + //读取工作表 + XSSFSheet sheet = workbook.getSheetAt(0);//getSheet("Sheet1"); + XSSFRow row; + XSSFCell cell = null; + //excel总行数 + int rowNum = sheet.getLastRowNum(); + row = sheet.getRow(0); + //excel总列数 + int colNum = row.getPhysicalNumberOfCells(); + // 填充数据的类的 属性个数 + int objColl = classz.getDeclaredFields().length; + //如果有数据 + if (rowNum > excelInitRow) { + list = new ArrayList<>(); + //遍历每行数据 + for (int i = excelInitRow; i < rowNum; i++) { + //获得当前行 + row = sheet.getRow(i); + //实例化 + Object newObj = classz.newInstance(); + Field[] fields = newObj.getClass().getDeclaredFields(); + boolean isAddObj = false; + //遍历每列的数据 + for (int j = 0; j < objColl; j++) { + cell = row.getCell(j); + if (null != cell) { + fields[j].setAccessible(true); + String cellString = cell.getStringCellValue(); + if (!"".equals(cellString)) { + isAddObj = true; + //int型 + if (fields[j].getType().toString().contains("Integer")) { + fields[j].set(newObj, Integer.valueOf(cellString)); + } + else { + fields[j].set(newObj, cellString); + } + } + + } + } + if (isAddObj) { + list.add(newObj); + } + } + } + return list; + } }