Java读取blob生成excel_Azure Blob上托管了一个excel文件,如何将其读入FileStream?

ExcelDataReader可以从任何流中读取数据,而不仅仅是FileStream . 您可以使用WebClient(已废弃),HttpClient或Azure SDK来打开流并读取blob .

无论如何,读取或下载blob都会打开并读取流 . 而不是例如 . 下载blob或在缓冲区中读取其所有内容,您可以直接访问流 . 无论您使用哪种技术,最后都会在单个URL上打开一个流进行读取 .

在您的情况下,您可以下载并保留文件以重复使用它,或者您可以直接从流中读取 . 如果您没有写入磁盘文件的权限,或者您同时处理多个请求并且不想处理临时文件存储,则可能希望在Web应用程序中执行此操作 .

使用HttpClient,您可以使用GetStreamAsync方法打开流:

var client=new HttpClient();

client.BaseAddress = new Uri("https://imor.blob.core.windows.net/files");

// Set headers and credentials

// ...

using(var stream=await client.GetStreamAsync("ImEx.xlsx"))

{

var excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);

//Process the data

}

使用Azure SDK,您可以使用CloudBlob.OpenRead方法:

var blob = container.GetBlockBlobReference("Imex.xlsx");

using(var stream=await blob.OpenReadAsync())

{

var excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);

//Process the data

}

您可能希望将数据存储在内存缓冲区或文件中,例如用于缓存或重新处理 . 为此,您可以分别创建MemoryStream或FileStream,并将数据从blob流复制到目标流 .

使用HttpClient,您可以使用以下内容填充内存缓冲区:

//To avoid reallocations, create a buffer large enough to hold the file

using(var memStream=new MemoryStream(65536))

{

using(var stream=await client.GetStreamAsync("ImEx.xlsx"))

{

await stream.CopyToAsync(memStream);

}

memStream.Position=0;

var excelReader = ExcelReaderFactory.CreateOpenXmlReader(memStream);

}

使用SDK:

using(var memStream=new MemoryStream(65536))

{

//.....

var blob = container.GetBlockBlobReference("Imex.xlsx");

await stream.DownloadToStreamAsync(memStream);

memStream.Position=0;

var excelReader = ExcelReaderFactory.CreateOpenXmlReader(memStream);

//...

}

要下载文件,您可以使用FileStream替换MemoryStream .

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个Java读取blob生成excel的示例代码: ```java public void exportExcel() { // 连接数据库并查询需要的blob数据 Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; try { conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/databaseName", "username", "password"); pstmt = conn.prepareStatement("SELECT excel_data FROM table_name WHERE id = ?"); pstmt.setInt(1, 1); // 假设需要查询的记录ID为1 rs = pstmt.executeQuery(); if (rs.next()) { // 将blob数据转换为excel文件 InputStream inputStream = rs.getBinaryStream("excel_data"); Workbook workbook = WorkbookFactory.create(inputStream); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); workbook.write(outputStream); byte[] bytes = outputStream.toByteArray(); // 将生成excel文件输出到客户端 HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse(); response.setContentType("application/vnd.ms-excel"); response.setHeader("Content-Disposition", "attachment;filename=example.xlsx"); ServletOutputStream out = response.getOutputStream(); out.write(bytes); out.flush(); out.close(); } } catch (SQLException | IOException | InvalidFormatException e) { e.printStackTrace(); } finally { try { if (rs != null) rs.close(); if (pstmt != null) pstmt.close(); if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } ``` 这个示例代码中,首先连接数据库并查询需要的blob数据,然后将这个blob数据转换为excel文件。具体的方法是:先将blob数据读入一个InputStream中,然后使用Apache POI的WorkbookFactory类的create方法将这个InputStream转换为Workbook对象,最后将这个Workbook对象写入到一个ByteArrayOutputStream中,即可得到生成excel文件的字节数组。 最后,将这个字节数组输出到客户端即可完成excel文件的下载。具体的方法是:设置response的ContentType为application/vnd.ms-excel,设置response的Header为Content-Disposition:attachment;filename=example.xlsx,然后获取response的OutputStream并将生成excel文件的字节数组写入到这个OutputStream中,最后关闭OutputStream即可。 希望这个示例代码能够对你有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值