
本文将介绍如何使用 PHP 读取托管在网络上的 Excel 文件,而无需先下载到服务器。我们将探讨使用 cURL 库获取文件内容,并结合 PhpSpreadsheet 库解析 Excel 数据的完整流程,并提供示例代码和注意事项,帮助开发者高效地从远程 Excel 文件中提取数据。
读取在线 Excel 文件主要分为两个步骤:
以下代码展示了如何使用 cURL 获取 Excel 文件内容:
<?php
function fetchExcelContent($url) {
$ch = curl_init($url);
// 设置 cURL 选项
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 将结果作为字符串返回
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 禁用 SSL 验证 (仅用于测试环境,生产环境不建议)
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 禁用 SSL 主机验证 (仅用于测试环境,生产环境不建议)
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // 允许重定向
curl_setopt($ch, CURLOPT_MAXREDIRS, 10); // 最大重定向次数
// 添加必要的请求头,模拟浏览器行为
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36',
'Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
]);
$excelData = curl_exec($ch);
if (curl_errno($ch)) {
echo 'cURL error: ' . curl_error($ch);
return false;
}
curl_close($ch);
return $excelData;
}
// 示例 URL
$url = 'https://www.idx.co.id/Portals/0/StaticData/ListedCompanies/Corporate_Actions/New_Info_JSX/Jenis_Informasi/01_Laporan_Keuangan/02_Soft_Copy_Laporan_Keuangan//Laporan%20Keuangan%20Tahun%202021/TW1/AALI/FinancialStatement-2021-I-AALI.xlsx';
$excelData = fetchExcelContent($url);
if ($excelData) {
// 文件内容已成功获取,可以传递给 PhpSpreadsheet 进行解析
echo "Excel 文件内容已成功获取。\n";
} else {
echo "获取 Excel 文件内容失败。\n";
}
?>代码解释:
立即学习“PHP免费学习笔记(深入)”;
注意事项:
首先,确保已经安装了 PhpSpreadsheet 库。可以使用 Composer 进行安装:
composer require phpoffice/phpspreadsheet
然后,可以使用以下代码解析从 cURL 获取的 Excel 数据:
<?php
require 'vendor/autoload.php'; // 引入 Composer 自动加载
use PhpOffice\PhpSpreadsheet\IOFactory;
// 假设 $excelData 已经包含了从 cURL 获取的 Excel 文件内容
if ($excelData) {
try {
// 使用 IOFactory 从字符串加载电子表格
$spreadsheet = IOFactory::load("data://text/plain;base64," . base64_encode($excelData));
// 获取活动工作表
$worksheet = $spreadsheet->getActiveSheet();
// 获取最高行和列索引
$highestRow = $worksheet->getHighestRow();
$highestColumn = $worksheet->getHighestColumn();
// 循环遍历每一行
for ($row = 1; $row <= $highestRow; ++$row) {
// 循环遍历每一列
for ($col = 'A'; $col <= $highestColumn; ++$col) {
// 获取单元格的值
$cellValue = $worksheet->getCell($col . $row)->getValue();
// 输出单元格的值
echo $cellValue . "\t";
}
echo "\n";
}
} catch (\PhpOffice\PhpSpreadsheet\Reader\Exception $e) {
echo 'Error loading file: ' . $e->getMessage();
}
} else {
echo "请先使用 cURL 获取 Excel 文件内容。\n";
}
?>代码解释:
立即学习“PHP免费学习笔记(深入)”;
注意事项:
将上述两个代码片段合并,即可得到一个完整的示例:
<?php
require 'vendor/autoload.php'; // 引入 Composer 自动加载
use PhpOffice\PhpSpreadsheet\IOFactory;
function fetchExcelContent($url) {
$ch = curl_init($url);
// 设置 cURL 选项
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 将结果作为字符串返回
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 禁用 SSL 验证 (仅用于测试环境,生产环境不建议)
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 禁用 SSL 主机验证 (仅用于测试环境,生产环境不建议)
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // 允许重定向
curl_setopt($ch, CURLOPT_MAXREDIRS, 10); // 最大重定向次数
// 添加必要的请求头,模拟浏览器行为
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36',
'Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
]);
$excelData = curl_exec($ch);
if (curl_errno($ch)) {
echo 'cURL error: ' . curl_error($ch);
return false;
}
curl_close($ch);
return $excelData;
}
// 示例 URL
$url = 'https://www.idx.co.id/Portals/0/StaticData/ListedCompanies/Corporate_Actions/New_Info_JSX/Jenis_Informasi/01_Laporan_Keuangan/02_Soft_Copy_Laporan_Keuangan//Laporan%20Keuangan%20Tahun%202021/TW1/AALI/FinancialStatement-2021-I-AALI.xlsx';
$excelData = fetchExcelContent($url);
if ($excelData) {
try {
// 使用 IOFactory 从字符串加载电子表格
$spreadsheet = IOFactory::load("data://text/plain;base64," . base64_encode($excelData));
// 获取活动工作表
$worksheet = $spreadsheet->getActiveSheet();
// 获取最高行和列索引
$highestRow = $worksheet->getHighestRow();
$highestColumn = $worksheet->getHighestColumn();
// 循环遍历每一行
for ($row = 1; $row <= $highestRow; ++$row) {
// 循环遍历每一列
for ($col = 'A'; $col <= $highestColumn; ++$col) {
// 获取单元格的值
$cellValue = $worksheet->getCell($col . $row)->getValue();
// 输出单元格的值
echo $cellValue . "\t";
}
echo "\n";
}
} catch (\PhpOffice\PhpSpreadsheet\Reader\Exception $e) {
echo 'Error loading file: ' . $e->getMessage();
}
} else {
echo "获取 Excel 文件内容失败。\n";
}
?>本文介绍了如何使用 PHP 读取在线 Excel 文件,而无需先下载到服务器。通过使用 cURL 获取文件内容,并结合 PhpSpreadsheet 解析 Excel 数据,可以方便地从远程 Excel 文件中提取数据。请注意,在生产环境中,应该配置正确的 SSL 证书,并根据实际情况调整请求头和错误处理。
以上就是使用 PHP 读取在线 Excel 文件:无需下载的解决方案的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号