123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786 |
- package com.rf.youth.utils;
- import com.alibaba.fastjson.JSONObject;
- import com.rf.youth.file.excel.ExcelBean;
- import jodd.util.StringUtil;
- import org.apache.poi.hssf.usermodel.HSSFWorkbook;
- import org.apache.poi.ss.usermodel.*;
- import org.apache.poi.ss.util.CellRangeAddress;
- import org.apache.poi.xssf.usermodel.*;
- import java.beans.PropertyDescriptor;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.InputStream;
- import java.lang.reflect.Method;
- import java.math.BigDecimal;
- import java.text.DecimalFormat;
- import java.text.NumberFormat;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.List;
- import java.util.Map;
- /**
- * @author zzf
- * @description:Excel 导出工具了哦
- * @date 2021/1/27 9:18
- */
- public class ExcelUtil {
- /**
- * excel 版本
- **/
- private final static String excel2003L = ".xls";
- private final static String excel2007U = ".xlsx";
- /**
- * Excel导入
- */
- public static List<List<Object>> getBankListByExcel(InputStream in, String fileName) throws Exception {
- List<List<Object>> list = null;
- //创建Excel工作薄
- Workbook work = getWorkbook(in, fileName);
- if (null == work) {
- throw new Exception("创建Excel工作薄为空!");
- }
- Sheet sheet = null;
- Row row = null;
- Cell cell = null;
- list = new ArrayList<List<Object>>();
- //遍历Excel中所有的sheet
- System.out.println("work.getNumberOfSheets():" + work.getNumberOfSheets());
- for (int i = 0; i < work.getNumberOfSheets() - 1; i++) {
- sheet = work.getSheetAt(i);
- if (sheet == null) {
- continue;
- }
- int sheetSize = 0;
- //遍历当前sheet中的所有行
- //包涵头部,所以要小于等于最后一列数,这里也可以在初始值加上头部行数,以便跳过头部
- for (int j = sheet.getFirstRowNum(); j <= sheet.getLastRowNum(); j++) {
- //读取一行
- row = sheet.getRow(j);
- //去掉空行和表头
- if (row.getFirstCellNum() == j) {
- sheetSize = row.getLastCellNum();
- }
- if (row == null || row.getFirstCellNum() == j) {
- continue;
- }
- //遍历所有的列
- List<Object> li = new ArrayList<Object>();
- for (int y = row.getFirstCellNum(); y < sheetSize; y++) {
- cell = row.getCell(y);
- li.add(cell != null ? getCellValue(cell) : null);
- }
- list.add(li);
- }
- }
- return list;
- }
- /**
- * Excel分sheet导入
- */
- public static List<List<List<Object>>> getBankListByExcelSheet(InputStream in, String fileName) throws Exception {
- List<List<List<Object>>> list = null;
- //创建Excel工作薄
- Workbook work = getWorkbook(in, fileName);
- if (null == work) {
- throw new Exception("创建Excel工作薄为空!");
- }
- Sheet sheet = null;
- Row row = null;
- Cell cell = null;
- list = new ArrayList<List<List<Object>>>();
- //遍历Excel中所有的sheet
- for (int i = 0; i < work.getNumberOfSheets() - 1; i++) {
- List<List<Object>> sheets = new ArrayList<List<Object>>();
- sheet = work.getSheetAt(i);
- if (sheet == null) {
- continue;
- }
- int sheetSize = 0;
- //遍历当前sheet中的所有行
- //包涵头部,所以要小于等于最后一列数,这里也可以在初始值加上头部行数,以便跳过头部
- for (int j = sheet.getFirstRowNum(); j <= sheet.getLastRowNum(); j++) {
- //读取一行
- row = sheet.getRow(j);
- //去掉空行和表头
- if (row == null || row.getFirstCellNum() == j) {
- continue;
- }
- if (isAllRowEmpty(row, sheet.getRow(0))) {
- continue;
- }
- sheetSize = row.getLastCellNum();
- //遍历所有的列
- List<Object> li = new ArrayList<Object>();
- for (int y = row.getFirstCellNum(); y < sheetSize; y++) {
- cell = row.getCell(y);
- if (cell == null) {
- continue;
- }
- li.add(cell != null ? getCellValue(cell) : null);
- }
- sheets.add(li);
- }
- list.add(sheets);
- }
- return list;
- }
- /**
- * 验证excel是否全部为空
- *
- * @param row 当前行
- * @param firstRow 第一行标题行
- * @return
- */
- public static boolean isAllRowEmpty(Row row, Row firstRow) {
- int count = 0;
- //单元格数量
- int rowCount = firstRow.getLastCellNum() - firstRow.getFirstCellNum();
- //判断多少个单元格为空
- for (int c = 0; c < rowCount; c++) {
- Cell cell = row.getCell(c);
- if (cell == null || cell.getCellType() == Cell.CELL_TYPE_BLANK || StringUtil.isEmpty((cell + "").trim())) {
- count += 1;
- }
- }
- if (count == rowCount) {
- return true;
- }
- return false;
- }
- /**
- * 描述:根据文件后缀,自适应上传文件的版本
- */
- private static Workbook getWorkbook(InputStream inStr, String fileName) throws Exception {
- Workbook wb = null;
- String fileType = fileName.substring(fileName.lastIndexOf("."));
- if (excel2003L.equals(fileType)) {
- //2003-
- wb = new HSSFWorkbook(inStr);
- } else if (excel2007U.equals(fileType)) {
- //2007+
- wb = new XSSFWorkbook(inStr);
- } else {
- throw new Exception("解析的文件格式有误!");
- }
- return wb;
- }
- /**
- * 描述:对表格中数值进行格式化
- */
- private static Object getCellValue(Cell cell) {
- Object value = null;
- //格式化字符类型的数字
- DecimalFormat df = new DecimalFormat("0");
- //日期格式化
- SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd");
- //格式化数字
- DecimalFormat df2 = new DecimalFormat("0.00");
- switch (cell.getCellType()) {
- case Cell.CELL_TYPE_STRING:
- value = cell.getRichStringCellValue().getString();
- break;
- case Cell.CELL_TYPE_NUMERIC:
- if ("General".equals(cell.getCellStyle().getDataFormatString())) {
- value = df.format(cell.getNumericCellValue());
- } else if ("m/d/yy".equals(cell.getCellStyle().getDataFormatString())) {
- value = sdf.format(cell.getDateCellValue());
- } else {
- value = df2.format(cell.getNumericCellValue());
- }
- break;
- case Cell.CELL_TYPE_BOOLEAN:
- value = cell.getBooleanCellValue();
- break;
- case Cell.CELL_TYPE_BLANK:
- value = "";
- break;
- case Cell.CELL_TYPE_FORMULA: //公式类型
- try {
- if ("General".equals(cell.getCellStyle().getDataFormatString())) {
- value = df.format(cell.getNumericCellValue());
- } else if ("m/d/yy".equals(cell.getCellStyle().getDataFormatString())) {
- value = sdf.format(cell.getDateCellValue());
- } else {
- value = df2.format(cell.getNumericCellValue());
- }
- } catch (IllegalStateException e) {
- value = String.valueOf(cell.getRichStringCellValue());
- }
- break;
- default:
- break;
- }
- return value;
- }
- /**
- * @param clazz 数据类
- * @param objs 导出数据列表
- * @param resultMap 计算结果,各项得分
- * @param map 表头
- * @param fileName 文件名
- * @param sheetName sheet页命
- * @return
- * @throws Exception
- */
- public static XSSFWorkbook createExcelFile(Class clazz, List objs, List<Map<String, String>> resultMap, Map<Integer, List<ExcelBean>> map, String fileName, String sheetName) throws Exception {
- // 创建新的Excel工作簿
- XSSFWorkbook workbook = new XSSFWorkbook();
- // 在Excel工作簿中建一工作表,其名为缺省值, 也可以指定Sheet名称
- XSSFSheet sheet = workbook.createSheet(sheetName);
- // 以下为excel的字体样式以及excel的标题与内容的创建,下面会具体分析;
- //字体样式
- createFont(workbook);
- //创建标题(头)
- createTableHeader(sheet, map);
- //创建内容
- createTableRows(sheet, map, objs, resultMap, clazz);
- String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
- // File file = new File("./"+date+"/"+sheetName+"/"+fileName);
- // if(!file.exists()){
- //// file.createNewFile();
- // file.mkdirs();
- // }
- FileOutputStream fileOutputStream = null;
- if (OSUtil.isLinux()) {
- File file = new File("./心理检测/检测结果/" + new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + "/" + sheetName);
- if (!file.exists()) {
- //file.createNewFile();
- file.mkdirs();
- }
- fileOutputStream = new FileOutputStream("./心理检测/检测结果/" + new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + "/" + sheetName + "/" + fileName);
- } else {
- File file = new File("./心理检测/检测结果/" + new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + "/" + sheetName);
- if (!file.exists()) {
- //file.createNewFile();
- file.mkdirs();
- }
- fileOutputStream = new FileOutputStream("./心理检测/检测结果/" + new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + "/" + sheetName + "/" + fileName);
- }
- // FileOutputStream fileOutputStream= new FileOutputStream("./"+date+"/"+sheetName+"/"+fileName);
- // FileOutputStream fileOutputStream= new FileOutputStream(file);
- workbook.write(fileOutputStream);
- fileOutputStream.close();
- return workbook;
- }
- /**
- * @param clazzs 数据类列表
- * @param objs 三个情绪识别题目列表
- * @param resultJson 三个情绪识别的答案
- * @param tableHeader 三个情绪识别的表头
- * @param fileName 文件名
- * @param sheetName sheet页
- * @return
- * @throws Exception
- */
- public static void createEALLExcelFile(List<Class> clazzs, Map<String, List> objs, JSONObject resultJson, List<Map<Integer, List<ExcelBean>>> tableHeader, String fileName, String sheetName) throws Exception {
- // 创建新的Excel工作簿
- XSSFWorkbook workbook = new XSSFWorkbook();
- // 在Excel工作簿中建一工作表,其名为缺省值, 也可以指定Sheet名称
- XSSFSheet sheet = workbook.createSheet(sheetName);
- // 以下为excel的字体样式以及excel的标题与内容的创建,下面会具体分析;
- //字体样式
- createFont(workbook);
- //创建内容
- createEALLTableRows(sheet, objs, tableHeader, resultJson, clazzs);
- FileOutputStream fileOutputStream = null;
- if (OSUtil.isLinux()) {
- File file = new File("./心理检测/检测结果/" + new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + "/" + sheetName);
- if (!file.exists()) {
- file.mkdirs();
- }
- fileOutputStream = new FileOutputStream("./心理检测/检测结果/" + new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + "/" + sheetName + "/" + fileName);
- } else {
- File file = new File("./心理检测/检测结果/" + new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + "/" + sheetName);
- if (!file.exists()) {
- file.mkdirs();
- }
- fileOutputStream = new FileOutputStream("./心理检测/检测结果/" + new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + "/" + sheetName + "/" + fileName);
- }
- workbook.write(fileOutputStream);
- fileOutputStream.close();
- //return workbook;
- }
- private static void createEALLTableRows(XSSFSheet sheet, Map<String, List> objsMap, List<Map<Integer, List<ExcelBean>>> tableHeader, JSONObject resultJson, List<Class> clazzs) throws Exception {
- int rowindex = 0;
- for (int j = 0; j < tableHeader.size(); j++) {
- Map<Integer, List<ExcelBean>> map = tableHeader.get(j);
- Class clazz = clazzs.get(j);
- List objs = null;
- Map<String, String> resultMap = null;
- if (j == 0) {
- objs = objsMap.get("ECAT");
- resultMap = (Map<String, String>) resultJson.get("ECAT");
- } else if (j == 1) {
- rowindex += 5;
- objs = objsMap.get("EREC");
- resultMap = (Map<String, String>) resultJson.get("EREC");
- } else if (j == 2) {
- rowindex += 5;
- objs = objsMap.get("EMEM");
- resultMap = (Map<String, String>) resultJson.get("EMEM");
- }
- //创建标题(头)
- XSSFRow titleRow1 = sheet.createRow(rowindex);
- for (Map.Entry<Integer, List<ExcelBean>> entry : map.entrySet()) {
- List<ExcelBean> excels = entry.getValue();
- for (int x = 0; x < excels.size(); x++) {
- XSSFCell cellTitle = titleRow1.createCell(x);
- cellTitle.setCellValue(excels.get(x).headTextName);
- cellTitle.setCellStyle(fontStyle);
- }
- }
- rowindex++;
- int maxKey = 0;
- List<ExcelBean> ems = new ArrayList<>();
- for (Map.Entry<Integer, List<ExcelBean>> entry : map.entrySet()) {
- if (entry.getKey() > maxKey) {
- maxKey = entry.getKey();
- }
- }
- ems = map.get(maxKey);
- List<Integer> widths = new ArrayList<Integer>(ems.size());
- //渲染答题记录
- if (objs != null) {
- for (Object obj : objs) {
- XSSFRow row = sheet.createRow(rowindex);
- for (int i = 0; i < ems.size(); i++) {
- ExcelBean em = (ExcelBean) ems.get(i);
- String propertyName = em.getPropertyName();
- PropertyDescriptor propertyDescriptor = new PropertyDescriptor(propertyName, clazz);
- Method method = propertyDescriptor.getReadMethod();
- Object rtn = method.invoke(obj);
- String value = "";
- // 如果是日期类型进行转换
- if (rtn != null) {
- if (rtn instanceof Date) {
- value = new SimpleDateFormat("yyyy-MM-dd").format(rtn);
- } else if (rtn instanceof BigDecimal) {
- NumberFormat nf = new DecimalFormat("#,##0.00");
- value = nf.format((BigDecimal) rtn).toString();
- } else if ((rtn instanceof Integer) && (Integer.valueOf(rtn.toString()) < 0)) {
- value = "--";
- } else {
- value = rtn.toString();
- }
- }
- XSSFCell cell = row.createCell(i);
- cell.setCellValue(value);
- cell.setCellType(XSSFCell.CELL_TYPE_STRING);
- cell.setCellStyle(fontStyle2);
- // 获得最大列宽
- int width = value.getBytes().length * 300;
- // 还未设置,设置当前
- if (widths.size() <= i) {
- widths.add(width);
- continue;
- }
- // 比原来大,更新数据
- if (width > widths.get(i)) {
- widths.set(i, width);
- }
- }
- rowindex++;
- }
- }
- rowindex += 2;
- if (resultMap != null && resultMap.size() > 0) {
- //渲染最终各项得分
- XSSFRow titleRow = sheet.createRow(rowindex);
- rowindex += 1;
- XSSFRow scoreRow = sheet.createRow(rowindex);
- int i = 0;
- if (resultMap.size() > 0) {
- for (Map.Entry<String, String> entry : resultMap.entrySet()) {
- XSSFCell cellTitle = titleRow.createCell(i);
- cellTitle.setCellValue(entry.getKey());
- cellTitle.setCellType(XSSFCell.CELL_TYPE_STRING);
- cellTitle.setCellStyle(fontStyle2);
- XSSFCell cellScore = scoreRow.createCell(i);
- cellScore.setCellValue(entry.getValue());
- cellScore.setCellType(XSSFCell.CELL_TYPE_STRING);
- cellScore.setCellStyle(fontStyle2);
- i++;
- }
- }
- }
- // 设置列宽
- for (int index = 0; index < widths.size(); index++) {
- Integer width = widths.get(index);
- width = width < 2500 ? 2500 : width + 300;
- width = width > 10000 ? 10000 + 300 : width + 300;
- sheet.setColumnWidth(index, width);
- }
- }
- }
- private static void createEALLTableHeader(XSSFSheet sheet, Map<Integer, List<ExcelBean>> map, int rowindex) {
- ////cell起始位置
- //int startIndex = 0;
- ////cell终止位置
- //int endIndex = 0;
- //for (Map.Entry<Integer, List<ExcelBean>> entry : map.entrySet()) {
- // XSSFRow row = sheet.createRow(entry.getKey());
- // List<ExcelBean> excels = entry.getValue();
- // for (int x = 0; x < excels.size(); x++) {
- // //合并单元格
- // //if (excels.get(x).getCols() > 1) {
- // if (x == 0) {
- // endIndex += excels.get(x).getCols() - 1;
- // CellRangeAddress range = new CellRangeAddress(rowindex - 1, rowindex - 1, startIndex, endIndex);
- // sheet.addMergedRegion(range);
- // } else {
- // endIndex += excels.get(x).getCols();
- // CellRangeAddress range = new CellRangeAddress(rowindex - 1, rowindex - 1, startIndex, endIndex);
- // sheet.addMergedRegion(range);
- // }
- // startIndex += excels.get(x).getCols();
- // XSSFCell cell = row.createCell(startIndex - excels.get(x).getCols());
- // // 设置内容
- // cell.setCellValue(excels.get(x).getHeadTextName());
- // if (excels.get(x).getCellStyle() != null) {
- // // 设置格式
- // cell.setCellStyle(excels.get(x).getCellStyle());
- // }
- // cell.setCellStyle(fontStyle);
- // //} else {
- // // XSSFCell cell = row.createCell(x);
- // // // 设置内容
- // // cell.setCellValue(excels.get(x).getHeadTextName());
- // // if (excels.get(x).getCellStyle() != null) {
- // // // 设置格式
- // // cell.setCellStyle(excels.get(x).getCellStyle());
- // // }
- // // cell.setCellStyle(fontStyle);
- // //}
- // }
- //}
- }
- /**
- * @param clazz
- * @param objs
- * @param resultMap
- * @param map
- * @param fileName
- * @return
- * @throws Exception
- */
- public static XSSFWorkbook createExcelFileForPublic(Class clazz, List objs, List<Map<String, String>> resultMap, Map<Integer, List<ExcelBean>> map, String fileName, String filePath) throws Exception {
- // 创建新的Excel工作簿
- XSSFWorkbook workbook = new XSSFWorkbook();
- // 在Excel工作簿中建一工作表,其名为缺省值, 也可以指定Sheet名称
- XSSFSheet sheet = workbook.createSheet("用户信息");
- // 以下为excel的字体样式以及excel的标题与内容的创建,下面会具体分析;
- //字体样式
- createFont(workbook);
- //创建标题(头)
- createTableHeader(sheet, map);
- //创建内容
- createTableRows(sheet, map, objs, resultMap, clazz);
- String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
- FileOutputStream fileOutputStream = null;
- File file = new File(filePath);
- if (!file.exists()) {
- //file.createNewFile();
- file.mkdirs();
- }
- fileOutputStream = new FileOutputStream(filePath + fileName);
- workbook.write(fileOutputStream);
- fileOutputStream.close();
- return workbook;
- }
- private static XSSFCellStyle fontStyle;
- private static XSSFCellStyle fontStyle2;
- public static void createFont(XSSFWorkbook workbook) {
- // 表头
- fontStyle = workbook.createCellStyle();
- XSSFFont font1 = workbook.createFont();
- font1.setBold(true);
- font1.setFontName("黑体");
- // 设置字体大小
- font1.setFontHeightInPoints((short) 14);
- fontStyle.setFont(font1);
- // 下边框
- fontStyle.setBorderBottom(BorderStyle.THIN);
- // 左边框
- fontStyle.setBorderLeft(BorderStyle.THIN);
- // 上边框
- fontStyle.setBorderTop(BorderStyle.THIN);
- // 右边框
- fontStyle.setBorderRight(BorderStyle.THIN);
- // 居中
- fontStyle.setAlignment(HorizontalAlignment.CENTER);
- // 内容
- fontStyle2 = workbook.createCellStyle();
- XSSFFont font2 = workbook.createFont();
- font2.setFontName("宋体");
- // 设置字体大小
- font2.setFontHeightInPoints((short) 10);
- fontStyle2.setFont(font2);
- // 下边框
- fontStyle2.setBorderBottom(BorderStyle.THIN);
- // 左边框
- fontStyle2.setBorderLeft(BorderStyle.THIN);
- // 上边框
- fontStyle2.setBorderTop(BorderStyle.THIN);
- // 右边框
- fontStyle2.setBorderRight(BorderStyle.THIN);
- // 居中
- fontStyle2.setAlignment(HorizontalAlignment.CENTER);
- }
- /**
- * 根据ExcelMapping 生成列头(多行列头)
- *
- * @param sheet 工作簿
- * @param map 每行每个单元格对应的列头信息
- */
- private static final void createTableHeader(XSSFSheet sheet, Map<Integer, List<ExcelBean>> map) {
- //cell起始位置
- int startIndex = 0;
- //cell终止位置
- int endIndex = 0;
- for (Map.Entry<Integer, List<ExcelBean>> entry : map.entrySet()) {
- XSSFRow row = sheet.createRow(entry.getKey());
- List<ExcelBean> excels = entry.getValue();
- for (int x = 0; x < excels.size(); x++) {
- //合并单元格
- if (excels.get(x).getCols() > 1) {
- if (x == 0) {
- endIndex += excels.get(x).getCols() - 1;
- CellRangeAddress range = new CellRangeAddress(0, 0, startIndex, endIndex);
- sheet.addMergedRegion(range);
- } else {
- endIndex += excels.get(x).getCols();
- CellRangeAddress range = new CellRangeAddress(0, 0, startIndex, endIndex);
- sheet.addMergedRegion(range);
- }
- startIndex += excels.get(x).getCols();
- XSSFCell cell = row.createCell(startIndex - excels.get(x).getCols());
- // 设置内容
- cell.setCellValue(excels.get(x).getHeadTextName());
- if (excels.get(x).getCellStyle() != null) {
- // 设置格式
- cell.setCellStyle(excels.get(x).getCellStyle());
- }
- cell.setCellStyle(fontStyle);
- } else {
- XSSFCell cell = row.createCell(x);
- // 设置内容
- cell.setCellValue(excels.get(x).getHeadTextName());
- if (excels.get(x).getCellStyle() != null) {
- // 设置格式
- cell.setCellStyle(excels.get(x).getCellStyle());
- }
- cell.setCellStyle(fontStyle);
- }
- }
- }
- }
- /**
- * @param sheet 表格sheet页
- * @param map 表头
- * @param objs 答题列表
- * @param clazz 量表实体类
- * @param resultMap 测试结果得分
- * @throws Exception
- */
- private static void createTableRows(XSSFSheet sheet, Map<Integer, List<ExcelBean>> map, List objs, List<Map<String, String>> resultMap, Class clazz) throws Exception {
- int rowindex = map.size();
- int maxKey = 0;
- List<ExcelBean> ems = new ArrayList<>();
- for (Map.Entry<Integer, List<ExcelBean>> entry : map.entrySet()) {
- if (entry.getKey() > maxKey) {
- maxKey = entry.getKey();
- }
- }
- ems = map.get(maxKey);
- List<Integer> widths = new ArrayList<Integer>(ems.size());
- //渲染答题记录
- if (objs != null) {
- for (Object obj : objs) {
- XSSFRow row = sheet.createRow(rowindex);
- for (int i = 0; i < ems.size(); i++) {
- ExcelBean em = (ExcelBean) ems.get(i);
- String propertyName = em.getPropertyName();
- PropertyDescriptor propertyDescriptor = new PropertyDescriptor(propertyName, clazz);
- Method method = propertyDescriptor.getReadMethod();
- Object rtn = method.invoke(obj);
- String value = "";
- // 如果是日期类型进行转换
- if (rtn != null) {
- if (rtn instanceof Date) {
- value = new SimpleDateFormat("yyyy-MM-dd").format(rtn);
- } else if (rtn instanceof BigDecimal) {
- NumberFormat nf = new DecimalFormat("#,##0.00");
- value = nf.format((BigDecimal) rtn).toString();
- } else if ((rtn instanceof Integer) && (Integer.valueOf(rtn.toString()) < 0)) {
- value = "--";
- } else {
- value = rtn.toString();
- }
- }
- XSSFCell cell = row.createCell(i);
- cell.setCellValue(value);
- cell.setCellType(XSSFCell.CELL_TYPE_STRING);
- cell.setCellStyle(fontStyle2);
- // 获得最大列宽
- int width = value.getBytes().length * 300;
- // 还未设置,设置当前
- if (widths.size() <= i) {
- widths.add(width);
- continue;
- }
- // 比原来大,更新数据
- if (width > widths.get(i)) {
- widths.set(i, width);
- }
- }
- rowindex++;
- }
- }
- rowindex += 2;
- if (resultMap != null && resultMap.size() > 0) {
- for (int m = 0; m < resultMap.size(); m++) {
- Map<String, String> hashMap = resultMap.get(m);
- if (hashMap != null && hashMap.size() > 0) {
- //渲染最终各项得分
- XSSFRow titleRow = sheet.createRow(rowindex);
- rowindex += 1;
- XSSFRow scoreRow = sheet.createRow(rowindex);
- int i = 0;
- if (resultMap.size() > 0) {
- for (Map.Entry<String, String> entry : hashMap.entrySet()) {
- XSSFCell cellTitle = titleRow.createCell(i);
- cellTitle.setCellValue(entry.getKey());
- cellTitle.setCellType(XSSFCell.CELL_TYPE_STRING);
- cellTitle.setCellStyle(fontStyle2);
- XSSFCell cellScore = scoreRow.createCell(i);
- cellScore.setCellValue(entry.getValue());
- cellScore.setCellType(XSSFCell.CELL_TYPE_STRING);
- cellScore.setCellStyle(fontStyle2);
- i++;
- }
- }
- }
- rowindex += 2;
- }
- }
- // 设置列宽
- for (int index = 0; index < widths.size(); index++) {
- Integer width = widths.get(index);
- width = width < 2500 ? 2500 : width + 300;
- width = width > 10000 ? 10000 + 300 : width + 300;
- sheet.setColumnWidth(index, width);
- }
- }
- /**
- * 用户导入
- *
- * @param in
- * @param fileName
- * @return
- * @throws Exception
- */
- public static List<List<Object>> getBankListByExcelForUserImport(InputStream in, String fileName) throws Exception {
- List<List<Object>> list = null;
- //创建Excel工作薄
- Workbook work = getWorkbook(in, fileName);
- if (null == work) {
- throw new Exception("创建Excel工作薄为空!");
- }
- Sheet sheet = null;
- Row row = null;
- Cell cell = null;
- list = new ArrayList<List<Object>>();
- //遍历Excel中所有的sheet
- System.out.println("work.getNumberOfSheets():" + work.getNumberOfSheets());
- for (int i = 0; i < work.getNumberOfSheets(); i++) {
- sheet = work.getSheetAt(i);
- if (sheet == null) {
- continue;
- }
- int sheetSize = 0;
- //遍历当前sheet中的所有行
- //包涵头部,所以要小于等于最后一列数,这里也可以在初始值加上头部行数,以便跳过头部
- for (int j = sheet.getFirstRowNum(); j <= sheet.getLastRowNum(); j++) {
- //读取一行
- row = sheet.getRow(j);
- //去掉空行和表头
- if (row == null || row.getFirstCellNum() == j) {
- continue;
- }
- sheetSize = row.getLastCellNum();
- //判断当前行是否为空
- if (isRowEmpty(row)) {
- continue;
- }
- //遍历所有的列
- List<Object> li = new ArrayList<Object>();
- for (int y = row.getFirstCellNum(); y < sheetSize; y++) {
- cell = row.getCell(y);
- li.add(cell != null ? getCellValue(cell) : null);
- }
- list.add(li);
- }
- }
- return list;
- }
- /**
- * 判断行是否为空
- *
- * @param row 行
- * @return true:空 ;false:非空
- */
- public static boolean isRowEmpty(Row row) {
- for (int i = row.getFirstCellNum(); i < row.getLastCellNum(); i++) {
- Cell cell = row.getCell(i);
- if (cell != null && cell.getCellType() != Cell.CELL_TYPE_BLANK) {
- return false;
- } else {
- break;
- }
- }
- return true;
- }
- }
|