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.List; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFCellStyle; 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; /** * @author chen * excel2007操作 */ public class ExcelOperation { /** 写入excel * @param templateFilePath * @param list list中对象的属性顺序和模板的列顺序一一对应 * @param excelInitRow //从第excelInitRow行 开始填充数据,( Constant.EXCEL_TEMPLATE_INIT_ROW) * @return HSSFWorkbook * @throws FileNotFoundException * @throws IOException * @throws IllegalArgumentException * @throws IllegalAccessException *///从第几行 填充数据 public static XSSFWorkbook writeExcelTemplate(XSSFWorkbook workbook, int excelInitRow, List list) throws FileNotFoundException, IOException, IllegalArgumentException, IllegalAccessException { //读取工作表 XSSFSheet sheet = workbook.getSheetAt(0);//getSheet("Sheet1"); XSSFRow row; XSSFCell cell = null; int size = list.size(); int lengths = 0; XSSFCellStyle style = getStyle(workbook); for (int i = 0; i < size; i++) { row = sheet.createRow(excelInitRow); Object obj = list.get(i); Field[] fields = obj.getClass().getDeclaredFields(); lengths = fields.length; for (int j = 0; j < lengths; j++) { fields[j].setAccessible(true); cell = row.createCell((short) j,HSSFCellStyle.ALIGN_CENTER); cell.setCellType(HSSFCell.CELL_TYPE_STRING); Object value = fields[j].get(obj); if (null != value) { //int型 if (fields[j].getType().toString().contains("Integer")) { cell.setCellValue(String.valueOf(value)); } else { cell.setCellValue(value.toString()); } } else { cell.setCellValue(""); } cell.setCellStyle(style); } //下一行 excelInitRow++; } for (int k = 0; k < lengths; k++) { sheet.autoSizeColumn(k); int width = sheet.getColumnWidth(k); if (width < 1600) { sheet.setColumnWidth(k, 1600); }else { sheet.setColumnWidth(k, width + 1000); } } return workbook; } /** excel的cell样式设置 * @param workbook * @return */ private static XSSFCellStyle getStyle(XSSFWorkbook workbook) { // 设置字体; XSSFFont font = workbook.createFont(); //设置字体大小; font.setFontHeightInPoints((short)9); //设置字体名字; font.setFontName("Courier New"); //font.setItalic(true); //font.setStrikeout(true); // 设置样式; XSSFCellStyle style = workbook.createCellStyle(); //设置底边框; style.setBorderBottom(HSSFCellStyle.BORDER_THIN); //设置底边框颜色; style.setBottomBorderColor(HSSFColor.BLACK.index); //设置左边框; style.setBorderLeft(HSSFCellStyle.BORDER_THIN); //设置左边框颜色; style.setLeftBorderColor(HSSFColor.BLACK.index); //设置右边框; style.setBorderRight(HSSFCellStyle.BORDER_THIN); //设置右边框颜色; style.setRightBorderColor(HSSFColor.BLACK.index); //设置顶边框; style.setBorderTop(HSSFCellStyle.BORDER_THIN); //设置顶边框颜色; style.setTopBorderColor(HSSFColor.BLACK.index); //在样式用应用设置的字体; style.setFont(font); //设置自动换行; style.setWrapText(false); //设置水平对齐的样式为居中对齐; style.setAlignment(HSSFCellStyle.ALIGN_CENTER); //设置垂直对齐的样式为居中对齐; 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 readExcel4Update(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); cell = row.getCell(0); if ("-".equals(getValue(cell)) || "".equals(getValue(cell))) { continue; } //实例化 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 = getValue(cell); if (!"".equals(cellString)) { isAddObj = true; //int型 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); } } } } if (isAddObj) { list.add(newObj); } } } 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(); } }