0

DocumentController.groovy

def upload() {
              def file =request.getFile('file')
              def excelImportService
              XSSFWorkbook book = new XSSFWorkbook(file.getInputStream());
              Map CONFIG_BOOK_COLUMN_MAP = [
             sheet:'Sheet1',
             startRow: 1,
             columnMap:  [
              //Col, Map-Key
              'A':'Question',
              'B':'SecType',
              'C':'Option1',
              'D':'Option2',
              'E':'Option3',
              'F':'Option4',
              'G':'CorrectAnswer',
              'H':'QuestionOrder'
             ]
            ]
         //Iterate through bookList and create/persists your domain instances
          def bookList = excelImportService.columns(book, CONFIG_BOOK_COLUMN_MAP)

While uploading the excel file with the same format getting the Error:

2014-04-30 15:49:31,024 [http-bio-8080-exec-9] ERROR errors.GrailsExceptionResolver  - InvalidFormatException occurred when processing request: [POST] /Application/document/upload - parameters:**
    upload: Upload                    
    Package should contain a content type part [M1.13]. Stacktrace follows:
    Message: Package should contain a content type part [M1.13]
        Line | Method
Gwenc37
  • 2,064
  • 7
  • 18
  • 22
  • http://stackoverflow.com/questions/10142282/exception-using-poi – tim_yates Apr 30 '14 at 10:59
  • Getting null pointer exception.2014-04-30 16:48:09,325 [http-bio-8080-exec-10] ERROR errors.GrailsExceptionResolver - NullPointerException occurred when processing request: [POST] /Application/document/upload - parameters: upload: Upload Cannot invoke method columns() on null object. Stacktrace follows: Message: Cannot invoke method columns() on null object – user3584616 Apr 30 '14 at 11:23
  • `def excelImportService` needs to be at class level, not method level – tim_yates Apr 30 '14 at 11:25

1 Answers1

0

Instead of:

XSSFWorkbook book = new XSSFWorkbook(file.getInputStream());

You should use:

Workbook book = WorkbookFactory.create(file.inputStream);

And

def excelImportService

Should be removed from the upload() method, and instead put as a property of the DocumentController class

tim_yates
  • 167,322
  • 27
  • 342
  • 338