I have this code and what it does is that it keeps information inside an excel, in this case "Hello World!" , but what happens is that the excel has to be closed so that said execution can be performed and when I try to do it with the open excl it sends me an error. Could you load information with an open excel?
this is the error that comes out
Warning: unlink(prueba/consulta.xlsx): Permission denied in C:\xampp\htdocs\importexcel\lib\PHPExcel\PHPExcel\Writer\Excel2007.php on line 214
Warning: ZipArchive::close(): Renaming temporary file failed: Invalid argument in C:\xampp\htdocs\importexcel\lib\PHPExcel\PHPExcel\Writer\Excel2007.php on line 347
Fatal error: Uncaught exception 'PHPExcel_Writer_Exception' with message 'Could not close zip file prueba/consulta.xlsx.' in C:\xampp\htdocs\importexcel\lib\PHPExcel\PHPExcel\Writer\Excel2007.php:348 Stack trace: #0 C:\xampp\htdocs\importexcel\f.php(24): PHPExcel_Writer_Excel2007->save('prueba/consulta...') #1 {main} thrown in C:\xampp\htdocs\importexcel\lib\PHPExcel\PHPExcel\Writer\Excel2007.php on line 348
and this is the code that I present
<?php
error_reporting(E_ALL);
set_time_limit(0);
date_default_timezone_set('Europe/London');
set_include_path(get_include_path() . PATH_SEPARATOR . 'lib/PHPExcel/');
require_once 'lib/PHPExcel/PHPExcel/IOFactory.php';
require_once 'lib/PHPExcel/PHPExcel.php';
$fileType = "Excel2007";
$fileName = "prueba/consulta.xlsx";
// Read the file
$objReader = PHPExcel_IOFactory::createReader($fileType);
$objPHPExcel = $objReader->load($fileName);
// Change the file
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A4', 'HELLO')
->setCellValue('B4', 'WORD!');
// Write the file
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, $fileType);
$objWriter->save($fileName);
?>
Related
So I am using the call php_com_dotnet in my code:
$word = new COM("word.application");
// Hide MS Word application window
$word->Visible = 0;
//Create new document
$word->Documents->Add();
// Define page margins
$word->Selection->PageSetup->LeftMargin = '2';
$word->Selection->PageSetup->RightMargin = '2';
// Define font settings
$word->Selection->Font->Name = 'Arial';
$word->Selection->Font->Size = 10;
// Add text
$word->Selection->TypeText("TEXT!");
// Save document
$filename = tempnam(sys_get_temp_dir(), "word");
$word->Documents[1]->SaveAs($filename);
// Close and quit
$word->quit();
unset($word);
header("Content-type: application/vnd.ms-word");
header("Content-Disposition: attachment;Filename=document_name.doc");
// Send file to browser
readfile($filename);
unlink($filename);
and I got his error:
Fatal error: Uncaught Error: Class 'COM' not found in /var/www/clients/client1/web1/web/nordin/save.php:6 Stack trace: #0 {main} thrown in /var/www/clients/client1/web1/web/nordin/save.php on line 6
so I looked around I added this to my php.ini file:
[COM_DOT_NET]
;extension=php_com_dotnet.dll
enable_dl = On
; extension_dir = "ext"
and it still doesn't work, what am I doing wrong? please help!
EDIT
my files are on a sftp server and I get error's like this:
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
The semi-colon before the extension is a comment and means that extension is not enabled. To enable it you must remove that semi-colon:
;extension=php_com_dotnet.dll
becomes
extension=php_com_dotnet.dll
You may need to restart IIS after making this change.
I'm trying to open an Excel file (.xlsx) that is protected by a password with PHPSpreadsheet (documentation). I know the password but I don't find a way to open it.
The load()method of \PhpOffice\PhpSpreadsheet\Reader\Xlsx doesn't give the possibility to insert a password and when I try to load the file it returns an error (of course).
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
$spreadsheet = $reader->load('hello world.xlsx');
$sheet = $spreadsheet->getActiveSheet();
echo $sheet->getCell('A1')->getValue() . "\n";
And here is the error
Warning: ZipArchive::getFromName(): Invalid or uninitialized Zip object in /PHPOffice/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Reader/Xlsx.php on line 311
Warning: ZipArchive::getFromName(): Invalid or uninitialized Zip object in /PHPOffice/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Reader/Xlsx.php on line 313
Warning: Invalid argument supplied for foreach() in /PHPOffice/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Reader/Xlsx.php on line 350
Warning: ZipArchive::getFromName(): Invalid or uninitialized Zip object in /PHPOffice/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Reader/Xlsx.php on line 311
Warning: ZipArchive::getFromName(): Invalid or uninitialized Zip object in /PHPOffice/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Reader/Xlsx.php on line 313
Warning: Invalid argument supplied for foreach() in /PHPOffice/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Reader/Xlsx.php on line 397
Warning: ZipArchive::getFromName(): Invalid or uninitialized Zip object in /PHPOffice/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Reader/Xlsx.php on line 311
Warning: ZipArchive::getFromName(): Invalid or uninitialized Zip object in /PHPOffice/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Reader/Xlsx.php on line 313
Warning: Invalid argument supplied for foreach() in /PHPOffice/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Reader/Xlsx.php on line 1855
Warning: ZipArchive::close(): Invalid or uninitialized Zip object in /PHPOffice/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Reader/Xlsx.php on line 1883
How can this code deal with passwords?
I welcome you to check out my PHPDecryptXLSXWithPassword repo.
It also works for DOCX/PPTX files, but this answer is specific to your question: first decrypt the file with password, and then use the decrypted file with PHPSpreadsheet.
Here is an example:
require_once('PHPDecryptXLSXWithPassword.php');
$encryptedFilePath = 'hello world.xlsx';
$password = 'mypassword'; // password to "open" the file
$decryptedFilePath = 'temp_path_to_decrypted_file.xlsx';
decrypt($encryptedFilePath, $password, $decryptedFilePath);
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
$spreadsheet = $reader->load($decryptedFilePath);
$sheet = $spreadsheet->getActiveSheet();
echo $sheet->getCell('A1')->getValue() . "\n";
Note: This is an experimental code, so use with caution. DO NOT use in production!
At this moment i can't try but, I suppose that you have to do something like this:
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
$spreadsheet = $reader->load('hello world.xlsx');
$sheet = $spreadsheet->getActiveSheet();
$sheet->getProtection()->setSheet(true);
$sheet->getProtection()->setPassword('THEPASSWORD');
echo $sheet->getCell('A1')->getValue() . "\n";
I'm not sure, tomorrow I will be to the office and can try.
I have registration.php in my data folder which is in the source folder so that isn't the issue. What I'm doing is recording my website's form data $dataRecord and trying to save it in this csv file that I have.
But I'm getting four errors:
Warning: fopen(data/registration.csv): failed to open stream: No such
file or directory in /users/s/j/sjflanag/www-root/cs008/lab11/form.php
on line 225
Warning: fputcsv() expects parameter 1 to be resource, boolean given
in /users/s/j/sjflanag/www-root/cs008/lab11/form.php on line 228
Warning: fclose() expects parameter 1 to be resource, boolean given in
/users/s/j/sjflanag/www-root/cs008/lab11/form.php on line 231
Fatal error: Call to undefined function sendMail() in
/users/s/j/sjflanag/www-root/cs008/lab11/form.php on line 273
$myFolder = 'data/';
$myFileName = 'registration';
$fileExt = '.csv';
$filename = $myFolder . $myFileName . $fileExt;
if ($debug) {
print PHP_EOL . '<p>filename is ' . $filename;
}
$file = fopen($filename, 'a');
fputcsv($file, $dataRecord);
fclose($file);
I've created a PHP project that converts JSON format into AVRO format.
The original project requires PHP libs that I'm not sure how to add on EMR.
This is the stderr log received by EMR:
PHP Warning: require_once(vendor/autoload.php): failed to open stream: No such file or directory in /mnt/var/lib/hadoop/tmp/nm-local-dir/usercache/hadoop/filecache/12/convert-json-to-avro.php on line 3
PHP Fatal error: require_once(): Failed opening required 'vendor/autoload.php' (include_path='.:/usr/share/pear:/usr/share/php') in /mnt/var/lib/hadoop/tmp/nm-local- dir/usercache/hadoop/filecache/12/convert-json-to-avro.php on line 3
log4j:WARN No appenders could be found for logger (amazon.emr.metrics.MetricsUtil).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
And here is the main code for the mapper:
#!/usr/bin/php
<?php
require_once 'vendor/autoload.php';
error_reporting(E_ALL);
ini_set('display_errors', 1);
$outputFile = __DIR__ . '/test_avro_out.avr';
$avroJsonSchema = file_get_contents(__DIR__ . '/HttpRequestEvent.avsc');
// Open $file_name for writing, using the given writer's schema
$avroWriter = AvroDataIO::open_file($outputFile, 'w', $avroJsonSchema);
$counter = 1;
while (($buf = fgets(STDIN)) !== false) {
try {
//replace ,null: with ,"null": to prevent map keys which are not strings.
$original = array("null:","userIp");
$replaceWith = array("\"null\":", "userIP");
$data = json_decode(str_replace($original, $replaceWith, $buf), true);
//print_r($buf);
if ($data === false || $data == null ) {
throw new InvalidArgumentException("Unable to parse JSON line");
}
$mapped = map_request_event($data);
var_dump($mapped);
//$avroWriter->append($mapped);
//echo json_encode($mapped), "\n";
} catch (Exception $ex) {
fprintf(STDERR, "Caught exception: %s\n", $ex->getMessage());
fprintf(STDERR, "Line num: %s\n",$counter);
fprintf(STDERR, "buf: %s\n", $buf);
}
$counter++;
}
$avroWriter->close();
Notice I'm using the require_once 'vendor/autoload.php'; which states that autoload.php is under the folder vendor.
What is the right way to load the vendor folder into the EMR cluster (there are needed files there)?
Should the require_once path change?
Thanks.
Following Guy's comment I've used a bash script similar to the one you can find here.
I've changed the require_once 'vendor/autoload.php' line in the code to point to the location where i dropped my files. (/home/hadoop/contents worked perfect).
lastly I've added an EMR bootstrap custom step where you can add the bash script so it can run before the PHP streaming step.
I save the XML generated from a URL to an XML file. Then I try to open it with PHP and save it as Excel2007 file using PHPExcel. But I get the following for each tag in the XML data file, every single one:
Warning: DOMDocument::loadHTMLFile(): Tag (note: this tag name here changes for every tag)results invalid in
./data.xml, line: 2 in C:\wamp\www\project\inc\functions.inc.php on
line 48
and this at the very end of the page with all the warnings:
Fatal error: Class 'SimpleXML' not found in
C:\wamp\www\project\inc\functions.inc.php on line 58
This is the code:
$sxml->asXML('data.xml'); // save as xml to server
$dom = new DOMDocument("1.0", "utf-8");
$dom->recover = true;
$dom->strictErrorChecking = false;
$dom->loadHTMLFile('./data.xml'); // LINE 48
if (!$dom)
{
throw new Exception("Could not load the lax XML file");
}
// Now you can work with your XML file using the $dom object.
// If you'd like using SimpleXML, do the following steps.
$xml = new SimpleXML($dom->saveXML()); // LINE 58
unset($dom);
/** PHPExcel_IOFactory */
require_once dirname(__FILE__) . '/../Classes/PHPExcel/IOFactory.php';
$inputFileName = "./data.xml";
/** Identify the type of $inputFileName **/
$inputFileType = PHPExcel_IOFactory::identify($inputFileName);
/** Create a new Reader of the type that has been identified **/
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
/** Load $inputFileName to a PHPExcel Object **/
$objPHPExcel = $objReader->load($inputFileName);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save(str_replace('.php', '.xlsx', __FILE__));