| | |
| | | |
| | | import com.alibaba.fastjson.JSONArray; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.poi.hssf.usermodel.HSSFDataValidation; |
| | | import org.apache.poi.hssf.usermodel.HSSFDateUtil; |
| | | import org.apache.poi.hssf.usermodel.HSSFWorkbook; |
| | |
| | | * 原文链接(不定时增加新功能): https://zyqok.blog.csdn.net/article/details/121994504 |
| | | */ |
| | | @SuppressWarnings("unused") |
| | | @Slf4j |
| | | public class ExcelUtils { |
| | | |
| | | private static final String XLSX = ".xlsx"; |
| | |
| | | |
| | | public static <T> List<T> readMultipartFile(MultipartFile mFile, Class<T> clazz) throws Exception { |
| | | JSONArray array = readMultipartFile(mFile); |
| | | log.info("array执行出来是:"+array); |
| | | return getBeanList(array, clazz); |
| | | } |
| | | |
| | |
| | | } |
| | | // 其余情况根据类型赋值 |
| | | String fieldClassName = field.getType().getSimpleName(); |
| | | String name = field.getName(); |
| | | try { |
| | | if ("String".equalsIgnoreCase(fieldClassName)) { |
| | | if ("String".equalsIgnoreCase(fieldClassName) || name.equals("phoe")) { |
| | | field.set(t, val); |
| | | } else if ("boolean".equalsIgnoreCase(fieldClassName)) { |
| | | field.set(t, Boolean.valueOf(val)); |
| | |
| | | return null; |
| | | } |
| | | in.close(); |
| | | // Workbook zhuanhuanexcel = zhuanhuanexcel(in); |
| | | return book; |
| | | } |
| | | |
| | |
| | | return s.trim(); |
| | | } |
| | | |
| | | private static Workbook zhuanhuanexcel(InputStream inputStream){ |
| | | Workbook workbook = null; |
| | | try { |
| | | workbook = WorkbookFactory.create(inputStream); |
| | | Sheet sheet = workbook.getSheetAt(0); // 获取第一个Sheet页 |
| | | |
| | | for (Row row : sheet) { |
| | | for (Cell cell : row) { |
| | | // 检查单元格的类型并转换为文本 |
| | | switch (cell.getCellType()) { |
| | | case STRING: |
| | | System.out.print(cell.getStringCellValue() + "\t"); |
| | | break; |
| | | case NUMERIC: |
| | | System.out.print(cell.getNumericCellValue() + "\t"); |
| | | break; |
| | | case BOOLEAN: |
| | | System.out.print(cell.getBooleanCellValue() + "\t"); |
| | | break; |
| | | case FORMULA: |
| | | System.out.print(cell.getCellFormula() + "\t"); |
| | | break; |
| | | default: break; |
| | | } |
| | | } |
| | | System.out.println(); // 换行,表示一行数据结束 |
| | | } |
| | | } catch (IOException e) { |
| | | throw new RuntimeException(e); |
| | | } finally { |
| | | try { |
| | | workbook.close(); |
| | | } catch (IOException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | } |
| | | return workbook; |
| | | } |
| | | |
| | | } |