简体   繁体   English

读取excel文件字节数组

[英]Read excel file byte array

byte[] test = getByteArry(excelfikepath)

I have one method where it returns the bytearray of the excel .xlsx file.我有一个方法,它返回bytearray的Excel中.xlsx文件。 To read this file i need to write these byte array using FileOutputStream on one server and from there i am calling another method which will read and process that excel from the server.要读取此文件,我需要在一台服务器上使用FileOutputStream写入这些字节数组,然后我将调用另一种方法,该方法将从服务器读取和处理 excel。

There is some limitation because of which i cant read excel file directly i have to put it onto another server and process.有一些限制,因为我无法直接读取 excel 文件,我必须将它放到另一台服务器和进程上。

Just wanted to know is there any way by which i can make use of this byte array and read excel file IN MEMORY instead of writing it on server.只是想知道有什么方法可以利用这个字节数组并在内存中读取excel文件而不是在服务器上写入它。

This will help to get byte array out of an excel file.这将有助于从 excel 文件中获取字节数组。

public static byte[] getFileByteArr(String fileName) throws InvalidFormatException, IOException {

        try (OPCPackage opcPackage = OPCPackage.open(new File(fileName))) {
            try (XSSFWorkbook workbook = (XSSFWorkbook) WorkbookFactory.create(opcPackage)) {
                try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
                    workbook.write(bos);
                    return bos.toByteArray();
                }
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM