Announcement Announcement Module
No announcement yet.
Excel data binding Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Excel data binding

    Probably a trivial question.

    If a user wants to upload a file in a web interface he can easily use Spring's MultiPart support for it. But does Spring support automatic databinding for file types like Excel ? Maybe something like registering a custom editor that populates a bean holding a reference to some Excel value object (cfr. poi).
    I did notice Spring supports Excel as an integrated view technology via AbstractExcelView, but 1) I'm not sure if I should use this as a value object and 2) I don't immediately see how a Controller can set the url on an AbstractExcelView (wich would populate the poi workbook).

    The only way I see is using POI directly in my Controller.

    But maybe I'm completely confused, and gave part of the answer without realizing. In that case put me on the right track :wink:


  • #2
    But does Spring support automatic databinding for file types like Excel ?
    No, Spring does not support this. I think requirements for something like this will vary a lot (to what do you want to bind, what layout, etcetera), to create an appropriate abstraction for this.

    Of course you can implement this yourself.


    • #3
      Remember by the way that MVC states that controller should just create or update a model (database, filesystem, etcetera), and views should render responses to clients. Of course this isn't the golden rule you'll have to stick to forever, but think twice before you go and create a view in your controller.


      • #4
        Hi Alef,

        actually I thought only in terms of abstracting the HSSFWorkbook construction, similar to AbstractExcelView.getTemplateSource(String), not specifying any layout at all.

        Implementation I chose was to create a class AbstractExcelHandler that implements an interface FileHandler with one method :
        void handleFile(InputStream inputStream) throws Exception;
        For any specific type of Excel-file a custom class should extend AbstractExcelHandler and be configured as a spring bean.
        The Controller doesn't differ that much from the FileUploadController example (spring-reference, 12.8 ), except having a FileHandler instance that gets the custom class injected and it also registers a custom PropertyEditor that populates a MultipartFile instance so the metadata of the uploaded file is accessible.

        I think this is the basic approach Spring encourages, although I maybe shouldn't have restricted the file handling to an InputStream.
        I just wondered if it would be a good thing if Spring offers more abstraction for Excel (or PDF for that matter) then just a view.
        But then again, I admit I have still much to learn :wink: