Giter VIP home page Giter VIP logo

Comments (2)

mdeinum avatar mdeinum commented on May 7, 2024

Your usage of this class is at first a little weird as you are using it outside the scope of Spring Batch but as a regular way to parse Excel files. You are probably better of using Apache POI directly instead of this wrapper.

That being said, due to your usage you are missing some callback methods to properly initialize the reader and prepare it to read rows.

First you need to call afterPropertiesSet to ensure proper setup of the PoiItemReader and BeanWrapperRowMapper. Next you need to call open with an empty ExecutionContext to properly open the Excel file for reading. So your ExcelUtils should look something like this.

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));
        reader.afterPropertiesSet(); // To ensure proper set
        reader.open(new ExecutionContext()); // To ensure properly opened and accessible file
        return reader;
    }

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

And when you are doing reading the file you should actually call close() on the PoiItemReader to properly close the underlying resources. If you don't call close() you have a resource leak and eventually your application will halt with an error indicating that you have too many files open.

@PostMapping("/import")
public String importStudents(@RequestParam String fileName, RedirectAttributes redirectAttributes) throws Exception {
    PoiItemReader<Student> studentItemReader = ExcelUtils.excelToItemReader(storageService.load(fileName), Student.class);
    try {
      Student student = studentItemReader.read();
    finally {
     studentItemReader.close(); // Close reader to release resources
    }
    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";
}

from spring-batch-extensions.

mdeinum avatar mdeinum commented on May 7, 2024

Closing due to inactivity.

from spring-batch-extensions.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.