簡體   English   中英

從Restful后端Java讀取上載的xlsx文件

[英]Read uploaded xlsx file from Restful backend java

我正在通過ajax-post調用從用戶界面上傳excel文件,並嘗試從支持的Restful服務Java代碼中讀取該文件,但我無法正確打印excel文件的內容。 文件名和其他屬性正確打印。

上傳的文件:test.xlsx

如果我使用以下代碼,則報錯

[java] org.jboss.resteasy.spi.UnhandledException: org.jboss.resteasy.core.ExceptionAdapter:  : null

POI版本:3.8

upload.java

public Response upload(final MultipartFormDataInput input)throws Exception{
    for (InputPart part : input.getParts()) { // you might get multiple files
        final String disposition = part.getHeaders().getFirst("Content-Disposition");
        final String fileName =  disposition.replaceFirst("(?i)^.*filename=\"([^\"]+)\".*$", "$1");
        InputStream inputStream = part.getBody(FileInputStream.class, null);
        try {
            Workbook workbook = WorkbookFactory.create(inputStream);
            Sheet datatypeSheet = workbook.getSheetAt(0);
            Iterator<Row> iterator = datatypeSheet.iterator();

            while (iterator.hasNext()) {
                Row currentRow = iterator.next();
                Iterator<Cell> cellIterator = currentRow.iterator();

                while (cellIterator.hasNext()) {

                    Cell currentCell = cellIterator.next();
                    if (currentCell.getCellType() == Cell.CELL_TYPE_STRING) {
                        System.out.print(currentCell.getStringCellValue() + "--");
                    } else if (currentCell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                        System.out.print(currentCell.getNumericCellValue() + "--");
                    } else if(currentCell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
                        System.out.print(currentCell.getBooleanCellValue() + "--");
                    } else {
                        System.out.print("<BLANK>--");
                    }
                }
                System.out.println();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    input.close();
    return Response.ok("OK").build();
}

如果我用

InputStream inputStream = part.getBody(InputStream.class,null);`然后我得到以下錯誤

  [java] Caused by: java.lang.IllegalArgumentException: Your InputStream was neither an OLE2 stream, nor an OOXML stream 

但是如果直接讀取Excel文件,它可以正常工作

我發現我們需要獲得excel流的問題

File.class而不是InputStream.class

public Response upload(final MultipartFormDataInput input)throws Exception{
    Map<String, List<InputPart>> formDataMap = input.getFormDataMap();
    File inputStream = formDataMap.get("files").get(0).getBody(File.class, null);
    String extraField = formDataMap.get("extraField").get(0).getBodyAsString();
    String CustomField =  formDataMap.get("CustomField").get(0).getBodyAsString();

    try {

        Workbook workbook = WorkbookFactory.create(inputStream);
        Sheet datatypeSheet = workbook.getSheetAt(0);
        Iterator<Row> iterator = datatypeSheet.iterator();

        while (iterator.hasNext()) {

            Row currentRow = iterator.next();
            Iterator<Cell> cellIterator = currentRow.iterator();

            while (cellIterator.hasNext()) {
                Cell currentCell = cellIterator.next();

                if (currentCell.getCellType() == Cell.CELL_TYPE_STRING) {
                    System.out.print(currentCell.getStringCellValue() + "--");
                } else if (currentCell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                    System.out.print(currentCell.getNumericCellValue() + "--");
                } else if(currentCell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
                    System.out.print(currentCell.getBooleanCellValue() + "--");
                } else {
                    System.out.print("<BLANK>--");
                }
            }
            System.out.println();

        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }


    input.close();
    return Response.ok("OK").build();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM