使用Spring Batch Excel Extension对象(PO​​JO)变为空

时间:2019-01-09 05:56:28

标签: java excel spring apache-poi spring-batch-excel

我正在使用public void onClick(View v) { // WallpaperManager myWallpaperManager = WallpaperManager.getInstance(getApplicationContext()); InputStream is = getResources().openRawResource(R.raw.gradient); Bitmap b = BitmapFactory.decodeStream(is); try { // myWallpaperManager.setBitmap(+ R.drawable.gradient); getApplicationContext().setWallpaper(b); Toast.makeText(getApplicationContext(),"Wallpaper updated", Toast.LENGTH_SHORT ).show(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } 来读取Spring Batch Excel Extension文件。我克隆了源代码并做了Excel (.xlx)并将依赖项添加到了Spring Boot项目中。我还添加了mvn install

我的Excel文件包含简单的数据:

Apache poi-ooxml

这是我的Id Last Name First Name 3 Aguinaldo Emilio 4 Aquino Melchora 5 Dagohoy Francisco 6 Luna Antonio 7 Jacinto Emilio 班:

Student

我创建了实用程序类,其方法可以实际读取Excel文件:

@Entity
public class Student {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    @NotBlank(message = "{NotBlank.student.lastName}")
    private String lastName;
    @NotBlank(message = "{NotBlank.student.firstName}")
    private String firstName;
    private LocalDateTime createdAt;

    // getters, setters
}

上传文件后,我将选择一个文件以将其数据导入我的数据库:

public class ExcelUtils {
    public static <T> ItemReader<T> excelToItemReader(Path file, Class<T> clazz) throws Exception {
        PoiItemReader<T> reader = new PoiItemReader<>();
        reader.setLinesToSkip(1);
        System.out.println("File Name: " + file.toString()); // Displays: File Name: uploads\excel\<Excel file selected to import>
        Resource resource = new FileSystemResource(file);
        System.out.println("File exists? " + resource.exists()); // Displays: File exists? true
        reader.setResource(resource);
        reader.setRowMapper(excelRowMapper(clazz));
        return reader;
    }

    private static <T> RowMapper<T> excelRowMapper(Class<T> clazz) {
        BeanWrapperRowMapper<T> rowMapper = new BeanWrapperRowMapper<>();
        rowMapper.setTargetType(clazz);
        return rowMapper;
    }
}

我不明白为什么在控制台中完全没有错误登录时@PostMapping("/import") public String importStudents(@RequestParam String fileName, RedirectAttributes redirectAttributes) throws Exception { ItemReader<Student> studentItemReader = ExcelUtils.excelToItemReader(storageService.load(fileName), Student.class); Student student = studentItemReader.read(); if (student != null) { System.out.println("Student has data."); studentService.save(student); } else { System.out.println("Student is null"); throw new Exception("Student is null"); } redirectAttributes.addFlashAttribute("message", "You successfully imported students data from " + fileName + "!"); return "redirect:/students"; } 会得到student

0 个答案:

没有答案