PHPSpreadsheet 以流形式读取电子表格

问题描述 投票:0回答:2

我正在从 FTP 目录中提取电子表格,我需要使用 PHPSpreadsheet 来读取它。我可以很好地读取本地文件,但不知道当我获取数据流时该怎么做。

这可能吗?我没有找到太多支持它的文档。

不包括代码,因为我认为这个问题不需要它。

非常感谢!

php phpspreadsheet
2个回答
0
投票

如果我以XLSX文件为例,它无法被流式传输,因为它需要先膨胀;你可以将任何 .xlsx 重命名为 .zip,基本上就可以了。

如果您只想处理 XLSX 文件,您可以从 PHP 中解压缩它们,然后使用例如

xml 解析器
流式传输包含在膨胀的 worksheets 目录中的任何 XML 文件。行可在 xpath
/worksheet/sheetData/row
处获取,值可在 xpath
/worksheet/sheetData/row/c/v
处获取。

您可能需要针对您想要支持的每种类型的文件进行实现。


0
投票

这应该有效。

<?php
// FTP connection settings
$ftpServer = 'ftp.example.com';
$ftpUsername = 'your-username';
$ftpPassword = 'your-password';

// FTP file path
$ftpFilePath = '/path/to/spreadsheet.xlsx';

// Local temporary file path
$tempFilePath = '/path/to/temporary/file.xlsx';

// Connect to FTP server
$ftpConnection = ftp_connect($ftpServer);
ftp_login($ftpConnection, $ftpUsername, $ftpPassword);

// Download file from FTP directory
if (ftp_get($ftpConnection, $tempFilePath, $ftpFilePath, FTP_BINARY)) {
    // Open stream from local file
    $stream = fopen($tempFilePath, 'r');

    // Load spreadsheet using PHPSpreadsheet
    $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($stream);

    // Close the stream
    fclose($stream);

    // Process the spreadsheet data as needed
    // ...

    // Delete the temporary file
    unlink($tempFilePath);
} else {
    echo "Failed to download file from FTP directory.";
}

// Close FTP connection
ftp_close($ftpConnection);
?>
© www.soinside.com 2019 - 2024. All rights reserved.