使用 PHP 在 wamp 中打开受密码保护的 EXCEL
Open a password protected EXCEL in wamp using PHP
我有一个有密码的 excel(当你打开它时,它要求输入密码)。我想打开它并将其保存到一个新文件夹中。假设没有密码,我的脚本将是:
<?php
$url = 'http://localhost/1472742721137_Book1.xlsx';
$destination_folder = $_SERVER['DOCUMENT_ROOT'].'/YOLO/';
$newfname = $destination_folder .'Book1.xlsx'; //set your file ext
$file = fopen ($url, "rb");
if ($file) {
$newf = fopen ($newfname, "a"); // to overwrite existing file
if ($newf)
while(!feof($file)) {
fwrite($newf, fread($file, 1024 * 8 ), 1024 * 8 );
}
}
if ($file) {
fclose($file);
}
if ($newf) {
fclose($newf);
}
?>
如何在fopen
函数中提及文件的密码?它必须只有一行可以做到这一点。
否则,我将不得不使用 PHPExcel,但这是我想避免的事情,因为它给我带来了很多错误,即使我试图使其适用于非保护的 excel 文件。
我的步数:
a) 我下载了文件并从 github 中提取了它们。
b) 然后我添加了这个脚本:
<?php
include '/Classes/PHPExcel.php';
$url = '1472742721137_Book1.xlsx';
$destination_folder = $_SERVER['DOCUMENT_ROOT'].'/YOLO/';
$reader = PHPExcel_IOFactory::createReaderForFile($url);
$reader->setReadDataOnly(true);
$workbook = $reader->load($url);
$objWriter->save($destination_folder);
?>
c) 我遇到了这个错误:
更新:
我终于让脚本生效了。现在,我只需要添加获取密码的行。该文件有一个密码,我需要在我的脚本中添加该密码才能打开它然后保存它(没有密码)。
我的脚本:
<?php
include '/Classes/PHPExcel.php';
$url = 'AAAAA.xlsx';
$destination_folder = $_SERVER['DOCUMENT_ROOT'].'/YOLO/';
$fileType=PHPExcel_IOFactory::identify($url);
$objReader = PHPExcel_IOFactory::createReader($fileType);
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load($url);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, $fileType);
$objWriter->save($destination_folder.'/output.xlsx');
?>
如果您只处理 Excel 文件,我建议使用 PHPExcel 模块,available here
我认为自 1.8.0 版本以来,您可以打开密码保护的工作表
我有一个有密码的 excel(当你打开它时,它要求输入密码)。我想打开它并将其保存到一个新文件夹中。假设没有密码,我的脚本将是:
<?php
$url = 'http://localhost/1472742721137_Book1.xlsx';
$destination_folder = $_SERVER['DOCUMENT_ROOT'].'/YOLO/';
$newfname = $destination_folder .'Book1.xlsx'; //set your file ext
$file = fopen ($url, "rb");
if ($file) {
$newf = fopen ($newfname, "a"); // to overwrite existing file
if ($newf)
while(!feof($file)) {
fwrite($newf, fread($file, 1024 * 8 ), 1024 * 8 );
}
}
if ($file) {
fclose($file);
}
if ($newf) {
fclose($newf);
}
?>
如何在fopen
函数中提及文件的密码?它必须只有一行可以做到这一点。
否则,我将不得不使用 PHPExcel,但这是我想避免的事情,因为它给我带来了很多错误,即使我试图使其适用于非保护的 excel 文件。
我的步数:
a) 我下载了文件并从 github 中提取了它们。
b) 然后我添加了这个脚本:
<?php
include '/Classes/PHPExcel.php';
$url = '1472742721137_Book1.xlsx';
$destination_folder = $_SERVER['DOCUMENT_ROOT'].'/YOLO/';
$reader = PHPExcel_IOFactory::createReaderForFile($url);
$reader->setReadDataOnly(true);
$workbook = $reader->load($url);
$objWriter->save($destination_folder);
?>
c) 我遇到了这个错误:
更新:
我终于让脚本生效了。现在,我只需要添加获取密码的行。该文件有一个密码,我需要在我的脚本中添加该密码才能打开它然后保存它(没有密码)。
我的脚本:
<?php
include '/Classes/PHPExcel.php';
$url = 'AAAAA.xlsx';
$destination_folder = $_SERVER['DOCUMENT_ROOT'].'/YOLO/';
$fileType=PHPExcel_IOFactory::identify($url);
$objReader = PHPExcel_IOFactory::createReader($fileType);
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load($url);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, $fileType);
$objWriter->save($destination_folder.'/output.xlsx');
?>
如果您只处理 Excel 文件,我建议使用 PHPExcel 模块,available here 我认为自 1.8.0 版本以来,您可以打开密码保护的工作表