diff --git a/src/com/platform/utils/excelUtils/ExcelOperation.java b/src/com/platform/utils/excelUtils/ExcelOperation.java new file mode 100644 index 00000000..86def9b8 --- /dev/null +++ b/src/com/platform/utils/excelUtils/ExcelOperation.java @@ -0,0 +1,121 @@ +package com.platform.utils.excelUtils; + +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.lang.reflect.Field; +import java.util.List; + +import org.apache.poi.hssf.usermodel.HSSFCell; +import org.apache.poi.hssf.usermodel.HSSFCellStyle; +import org.apache.poi.hssf.usermodel.HSSFFont; +import org.apache.poi.hssf.usermodel.HSSFRow; +import org.apache.poi.hssf.usermodel.HSSFSheet; +import org.apache.poi.hssf.usermodel.HSSFWorkbook; +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.XSSFFactory; +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; + +/** + * @author chen + * excel2007操作 + */ +public class ExcelOperation { + + /** 读取excel模板 + * @param templateFilePath + * @param list list中对象的属性顺序和模板的列顺序一一对应 + * @return HSSFWorkbook + * @throws FileNotFoundException + * @throws IOException + * @throws IllegalAccessException + * @throws IllegalArgumentException + */ + public static XSSFWorkbook readExcelTemplate(String templateFilePath, List list) throws FileNotFoundException, IOException, IllegalArgumentException, IllegalAccessException { + System.out.println(templateFilePath); +// InputStreamReader in= new InputStreamReader(Resource.class.getResourceAsStream(templateFilePath), "UTF-8"); + ClassPathResource res = new ClassPathResource(templateFilePath); + XSSFWorkbook workbook = new XSSFWorkbook(res.getInputStream());//getAbsolutePath() + + + //从第几行 填充数据 + int excelInitRow = 10; + //读取工作表 + 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) { + cell.setCellValue(value.toString()); + } + else { + cell.setCellValue(""); + } + cell.setCellStyle(style); + } + //下一行 + excelInitRow++; + } + for (int k = 0; k < lengths; k++) { + sheet.autoSizeColumn(k); + } + return workbook; + } + + 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; + } + +}