简体   繁体   English

如何从 gcp 存储桶中读取 XLS 文件

[英]How read XLS file from gcp bucket

I have on bucket XLS file and I have to pull the file and read the data on the stream and work with data.我有桶XLS文件,我必须提取文件并读取流中的数据并处理数据。 I worked with CSV file and this is my code:我使用了CSV文件,这是我的代码:

try (ReadChannel reader = storage.reader(bucketName, fileName)) {   
            ByteBuffer bytes = ByteBuffer.allocate(BUFFER_SIZE);
            while (reader.read(bytes) > 0) {
                bytes.flip();
                // outChannel.write(bytes);
                set(new String(bytes.array(), "UTF-8"), fileName); 
                bytes.clear();
            }
        }

I think this might be what you are looking for.我想这可能就是你要找的。 GCS Input Channel GCS 输入通道

GcsService gcsService = GcsServiceFactory.createGcsService();
GcsFilename fileName = new GcsFilename("TestBucket", "Test1.xlsx"); 
GcsInputChannel readChannel = gcsService.openPrefetchingReadChannel(fileName, 0, BUFFER_SIZE);
InputStream inputStream = Channels.newInputStream(readChannel);

I've also found an interesting workaround that might help you, in this answer you have a piece of code that converts all the excel files to CSV to let you manipulate them as normal if you wish to.我还发现了一个可能对您有所帮助的有趣解决方法,在这个答案中,您有一段代码可以将所有 excel 文件转换为 CSV,以便您可以正常操作它们。

EDIT: The error was there because it was not initialized.编辑:错误在那里,因为它没有被初始化。 Have a look at my code edit.看看我的代码编辑。

In your case as far as I understood you might need to use " OutputChannel " not "InputChannel" but it's the same concept.在你的情况下,据我所知,你可能需要使用“ OutputChannel ”而不是“InputChannel”,但它是相同的概念。

Hope this helps.希望这可以帮助。

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

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