幾天來我一直在努力解決這個問題,并認為是時候問了。任務似乎很簡單 - 使用 PhpSpreadsheet 將 Excel 作業表寫入 Google Cloud Storage 存盤桶。
目前我的想法是檔案需要流式傳輸到存盤桶。另一個選項似乎是在記憶體中生成檔案,然后將其寫入存盤桶,我覺得這可能是資源密集型的。
我正在使用帶有標準環境和 PHP 7.4 的 App Engine。
為了測驗它,我一直在使用最基本的代碼來制作表格 -
require 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'Hello World !');
$writer = new Xlsx($spreadsheet);
$writer->save('hello world.xlsx');
然后我將結合對存盤桶的基本寫入 -
require 'vendor/autoload.php';
use Google\Cloud\Storage\StorageClient;
/**
* @param string $bucketName The name of your Cloud Storage bucket.
* @param string $objectName The name of your Cloud Storage object.
* @param string $source The path to the file to upload.
*/
function upload_object($bucketName, $objectName, $source)
{
// $bucketName = 'my-bucket';
// $objectName = 'my-object';
// $source = '/path/to/your/file';
$storage = new StorageClient();
$file = fopen($source, 'r');
$bucket = $storage->bucket($bucketName);
$object = $bucket->upload($file, [
'name' => $objectName
]);
}
這些基本上是來自 PhpSpreadsheet 和 Google 的代碼示例。我已經讓兩者都自己作業了。有沒有人有任何想法讓生成的 Excel 作業表最終出現在 Cloud Storage 存盤桶上?
// Added 2022-01-13 //////////////////////////////
After a few comments I've tried different options and have listed the results below. I think it is getting close, but not quite there.
<?php
require 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use Google\Cloud\Storage\StorageClient;
$bucketName = 'BUCKETNAME'; // Name of the GCS bucket
$objectName = 'new_file_name.xlsx'; // Name of the new file we are creating
$objectLocation = 'gs://'.$bucketName.'/'.$objectName; // Path of the new file
try {
$storage = new StorageClient();
$storage->registerStreamWrapper();
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'Hello World !');
$sheet->setCellValue('B1', 'Hello You !');
$writer = new Xlsx($spreadsheet);
// Trial 1
// This displays "Could not open file "gs://BUCKETNAME/new_file_name.xlsx" for writing." in the browser window.
$writer->save($objectLocation);
// Trial 2
// This creates an empty file in the GCS bucket and displays the XLSX file code on the screen
//$file = fopen($writer->save('php://output'), 'r');
//$bucket = $storage->bucket($bucketName);
//$object = $bucket->upload($file, [
// 'name' => $objectName
//]);
// Trial 3
// This displays "Could not open file "new_file_name.xlsx" for writing." in the browser window.
//$file = fopen($writer->save($objectName), 'r');
//$bucket = $storage->bucket($bucketName);
//$object = $bucket->upload($file, [
// 'name' => $objectName
//]);
}
catch (Exception $e) {
echo $e->getMessage();
}
uj5u.com熱心網友回復:
這是一個作業示例代碼:
<?php
require 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use Google\Cloud\Storage\StorageClient;
$bucketName = 'BUCKET_NAME'; // Name of the GCS bucket
$objectName = 'FILENAME.xlsx'; // Name of the new file we are creating
try {
$storage = new StorageClient();
$storage->registerStreamWrapper();
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'Hello World !');
$sheet->setCellValue('B1', 'Hello You !');
$writer = new Xlsx($spreadsheet);
ob_start();
$writer->save('php://output');
$content = ob_get_contents();
ob_end_clean();
$bucket = $storage->bucket($bucketName);
$object = $bucket->upload($content, [
'name' => $objectName
]);
}
catch (Exception $e) {
echo $e->getMessage();
}
?>
以下是 Google Cloud Storage 中包含內容的電子表格的螢屏截圖:


在 PHP 中,有一個名為PHP Output Control的參考。
我曾經ob_start()創建一個輸出緩沖區并ob_end_clean()洗掉最頂層的輸出緩沖區及其所有內容,而不向瀏覽器發送任何內容。
我還php://output根據PHPSpreadsheet Documentation將檔案臨時存盤在腳本的作業目錄中。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/439035.html
標籤:google-app-engine google-cloud-storage phpspreadsheet
