[英]Read and write an excel file from Azure Blob Storage without downloading it to local file in Java
我正在使用azure-storage依赖项创建 azure 目录的引用,然后在本地系统中下载文件后,我可以使用poi-ooxml 库进行读写,然后再次在 azure 存储中上传相同的文件。
我只想删除这个本地下载的东西。 有什么方法可以在不下载本地的情况下读取和写入excel文件。
使用的依赖项。
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-storage</artifactId>
<version>8.4.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.15</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.15</version>
</dependency>
代码
try {
CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);
CloudBlobClient blobClient = storageAccount.createCloudBlobClient();
CloudBlobContainer container = blobClient.getContainerReference(containerName);
CloudBlobDirectory downloadDirectory = container.getDirectoryReference(dowloadDirectoryName);
for(ListBlobItem blobItem: downloadDirectory.listBlobs()) {
if(blobItem instanceof CloudBlockBlob) {
CloudBlockBlob cloudBlob = (CloudBlockBlob) blobItem;
File inputFile = new File(cloudBlob.getName());
cloudBlob.downloadToFile(inputFile.getAbsolutePath());
FileInputStream inputStream = new FileInputStream(inputFile);
Workbook workbook = new HSSFWorkbook(inputStream);
/*Workbook workbook = WorkbookFactory.create(inputFile);*/
Sheet sheet = workbook.getSheetAt(0);
log.info("Cell No 0 : "+sheet.getRow(0).getCell(0).getStringCellValue());
for (int i = 1; i <= sheet.getLastRowNum(); i++) {
Cell reasonLabel = sheet.getRow(i).createCell(2);
reasonLabel.setCellValue("Added one cell");
}
File outputFile = new File(cloudBlob.getName().split("\\.")[0]+"01.xls");
FileOutputStream outputStream = new FileOutputStream(outputFile);
workbook.write(outputStream);
outputStream.close();
workbook.close();
CloudBlobDirectory uploadDirectory = container.getDirectoryReference(uploadDirectoryName);
CloudBlockBlob uploadBlob = uploadDirectory.getBlockBlobReference(outputFile.getName());
FileInputStream fileInputStream = new FileInputStream(outputFile);
uploadBlob.upload(fileInputStream, outputFile.getTotalSpace());
fileInputStream.close();
outputFile.delete();
}
}
} catch(Exception ex) {
log.error(ex.getMessage());
throw ex;
}
这是我使用 NPOI 从 Azure blob 读取文件的灵魂
foreach (var blob in blobContainer.GetBlobs())
{
var blobClient = blobContainer.GetBlobClient(blob.Name);
using MemoryStream memStream = new();
blobClient.DownloadTo(memStream);
memStream.Position = 0;
var wb = new XSSFWorkbook(memStream);
sheetName = wb.GetSheetAt(0).SheetName;
sheet = (XSSFSheet)wb.GetSheet(sheetName);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.