我有一个带有密码的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,但这是我要避免的事情,因为即使我试图使它适用于受NON保护的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)我收到此错误:
UPD:
我终于使脚本工作了。现在,我所需要做的就是添加将获得密码的行。该文件有一个密码,我需要在密码中添加该密码才能打开它,然后将其保存(没有密码)。
我的剧本:
<?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模块(可从此处获得)。我认为,自1.8.0版本以来,您可以打开受密码保护的工作表。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句