I am new to php. and I want to write some data to an excel file using php. I want to use PHPExcel for this purpose but I dont know how to do it I have downloaded and copes the to folders Clases and Expamles to my project folder then I tried require_once "Classes/PHPExcel/IOFactory.php" $objTpl = PHPExcel_IOFactory::load("template.xlsx"); just these two lines but its showing an error canoy open the file then I created an excel file at the location C:\wamp\www\scrapproj\Classes\PHPExcel\Reader again it shows an error at another line. The error showing is Fatal error: Uncaught exception 'PHPExcel_Reader_Exception' with message 'Could not open tempplate.xlsx for reading! File does not exist.' in C:\wamp\www\scrapproj\Classes\PHPExcel\Reader\Excel2007.php on line 82 Please somebody hep me to solve this.
updated Now I have updated my code like the following $objTpl = PHPExcel_IOFactory::load("template1.xlsx");
$objTpl->setActiveSheetIndex(0);
$objTpl->getActiveSheet()->setCellValue('A2', 'PHPExcel');
// $filename=mt_rand(1,100000).'.xlsx';
$objWriter = PHPExcel_IOFactory::createWriter($objTpl, 'Excel5');
if(is_readable('template1.xlsx'))
{
$objWriter->save('template1.xlsx');
} Now its its running without any error. But after running the page I cant open the excel file directly its showing the error Excel canot open the file template1.xlsx because the file format or its extension is not valid please help me to solve it
You are looking for the file in the current working directory, you can use the getcwd command to see what your working directory is.
1)
echo getcwd() . "\n";
http://www.php.net/manual/de/function.getcwd.php
Furthermore check if you have permission on that file.
2)
is_readable($filename)
http://www.php.net/manual/en/function.is-readable.php
Answer to your updated question (in future, please close answered questions and ask a new question)
You're using the Excel5 Writer
$objWriter = PHPExcel_IOFactory::createWriter($objTpl, 'Excel5');
Which is used for writing BIFF format files, which should have the extension .xls
But you're saving the file with a .xlsx extension
$objWriter->save('template1.xlsx');
which is for OfficeOpenXML format files
BIFF files are created using the Excel5 Writer, and have an extension of .xls
OfficeOpenXML files are created using the Excel2007 Writer, and have an extension of .xlsx
Don't mix and match, writers and extensions aren't interchangeable
Related
I'm getting error 500 when i try to read xlsx file using PHPExcel Api.
When i read file from input:
$objReader->load($_FILES['file']['tmp_name']);
=> it's not working. i get this error:
Message: Could not open for reading! File does not exist.
However when i log $_FILES['file']['tmp_name'] i get the file path:
C:\wamp64\tmp\xxxx.tmp
and when i try another file like this:
$objReader->load('c://test.xlsx);
it works
Controller:
$this->load->library('excel');
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objPHPExcel = $objReader->load($_FILES['file']['tmp_name']);
print $objPHPExcel->getActiveSheet()->getCell('B1')->getValue();
Normaly in my application system i am able to export content via PHP Exel. Now i can export the .xlsx file but if i would like to open it ill get the message : Can not open file because format or filename extension is invalid.
Error Code :
Fatal error: Uncaught exception 'Exception' with message 'Could not close zip file php://output.' in
/net/myvm0364/disc1/www/pc2/application/include/847dfknsa_2434_phpExcel/PHPExcel/Writer/Excel2007.php:346
Stack trace: #0
/net/vmits0364/disc1/www/pc2/application/include/exportMember.php(188):
PHPExcel_Writer_Excel2007->save('php://output') #1 {main} thrown in
/net/myvm0364/disc1/www/pc2/application/include/847dfknsa_2434_phpExcel/PHPExcel/Writer/Excel2007.php
on line 346
Did somebody know what i have to do ?
I have seen a similar case before on my server (then I used CSV because do not solve problem). The reason is that the excel file is too large (about 40000 or 50000 records, or file size larger than 15MB), making your server can't handle, so excel file will be broken.
Example:
The file content if successfully created is 20MB, but when the file creates 15MB, your server is overload, it will stop the process here. You can still create an excel file but can't open it.
You can try creating an excel file with a capacity of about 1MB or less than, if it work the problem may not be due to your code.
Hope can help you.
So sorry because bad english.
Have this project which aims to import an Excel sheet: the file is a report, not a flat file and there's no way to obtain such. So I'm using PHP Excel to read the file and save the data we need into the application.
Problem is that the file is in Excel 4.0 format which I can't edit (apparently), it can be opened and viewed in MS Excel application, but PHP Excel throws the exception when trying to read it with Excel5:
Fatal error: Uncaught exception 'PHPExcel_Reader_Exception' with message 'The filename C:/xampp/htdocs/uploads/PS.xls is not recognised as an OLE file' in C:\xampp\htdocs\includes\PHPExcel-1.8\Classes\PHPExcel\Shared\OLERead.php on line 90
PHP Excel identifies the file as in HTML format, but also, throws me the error:
Warning: DOMDocument::loadHTML(): Invalid char in CDATA 0x4 in Entity, line: 1 in C:\xampp\htdocs\includes\PHPExcel-1.8\Classes\PHPExcel\Reader\HTML.php on line 495
I suppose this is because the data contains special characters (UTF-8), but found nothing in PHPExcel that could change the encoding... neither could find anything about this problem and I'm starting to think there's no solution for this, except by asking the user to copy & paste the data to "decent" Excel file... That way works, but require some manual manipulation.
No it can't read it. The minimum BIFF format version that PHPExcel can read is BIFF 5 (from 1993).
Good luck finding anything capable of reading BIFF 4 (from 1992). BIFF 4 uses OLE1, while PHPExcel can only read .xls files using OLE2 (from BIFF 5 onwards). I'm not aware of any pure PHP extensions that can handle such an early format; you may need to use the commercial LibXl extension.
I am trying to read a RAF file in php using Imagick but somehow its not working. I have looked into the formats supported by Imagick and RAF is listed.
I later tried to simply read RAF file and echo it out but still no luck.
I keep on getting this error:
Uncaught exception 'ImagickException' with message 'unable to open image `/tmp/magick-H2rs1zGJ.ppm': No such file or directory
I tried rendering a png file saved in the same directory and it worked but somehow its not working for RAF.
$imagePath = "uploaded.raf";
$imagick = new \Imagick($imagePath);
header("Content-Type: image/jpg");
echo $imagick->getImageBlob();
Any help is appreciated.
Thank you!
Overview
I'm trying to directly download an Excel spreadsheet created using PHPExcel. I don't have server-level access so I can't install or enable mods (such as the Zip module).
The data is a guestlist for an event.
Code
<?php
if(isset($_GET["event_id"])&&
!empty($_GET["event_id"])){
//Include PHPExcel, Excel2007, classes
require_once("inc/PHPExcel/PHPExcel.php");
require_once("inc/PHPExcel/PHPExcel/Writer/Excel2007.php");
require_once("inc/classes.php");
//Zip not installed - change settings to use local compression
PHPExcel_Settings::setZipClass(PHPExcel_Settings::PCLZIP);
//Get event data
$event_id = intval($_GET["event_id"]);
$event = new Event($event_id);
$guests = $event->getGuests();
//Create new PHPExcel object
$spreadsheet = new PHPExcel();
//Add data
$spreadsheet->setActiveSheetIndex(0);
$spreadsheet->getActiveSheet()->SetCellValue("B2", "TMC Gateway");
$spreadsheet->getActiveSheet()->SetCellValue("B3", "Event register");
$spreadsheet->getActiveSheet()->SetCellValue("B5", "Name");
$spreadsheet->getActiveSheet()->SetCellValue("C5", "Member/Guest");
$spreadsheet->getActiveSheet()->SetCellValue("D5", "Checkin Time");
foreach($guests as $guest){
if($guest["degree"]=="guest"){
$arr[] = [$guest["name1"]." ".$guest["name2"], "Guest", $guest["checkintime"]];
} else {
$arr[] = [trim($guest["name2"]), "Member", $guest["checkintime"]];
}
}
$currentCell = 6;
foreach($arr as $a){
$spreadsheet->getActiveSheet()->SetCellValue("B$currentCell",$a[0]);
$spreadsheet->getActiveSheet()->SetCellValue("C$currentCell",$a[1]);
$spreadsheet->getActiveSheet()->SetCellValue("D$currentCell",$a[2]);
$currentCell++;
}
//Rename sheet
$spreadsheet->getActiveSheet()->setTitle("TMC Gateway");
//Open writer
$writer = new PHPExcel_Writer_Excel2007($spreadsheet);
//Set headers and force download
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment;filename=\"TMC_Gateway_Attendees-".$event_id.".xls\"");
$writer->save("php://output");
//Kill script
exit;
}
Issue
When processing originally and opening the file, I saw this error:
Fatal error: Class 'ZipArchive' not found in /home/loqui/public_html/doorapp/inc/PHPExcel/PHPExcel/Writer/Excel2007.php on line 227
I realised this is probably because the Zip module was either not installed or not enabled, so I followed these instructions at Class 'ZipArchive' not found error while using PHPExcel:
If you don't have ZipArchive installed/enabled for your PHP, and can't enable it yourself, then you can use
PHPExcel_Settings::setZipClass(PHPExcel_Settings::PCLZIP);
However, now when opening the file, this error appears:
Fatal error: Uncaught exception 'PHPExcel_Writer_Exception' with message 'Error zipping files : PCLZIP_ERR_READ_OPEN_FAIL (-2) : Unable to open temporary file '/tmppclzip-56df08ee0384c.tmp' in binary write mode' in /home/loqui/public_html/doorapp/inc/PHPExcel/PHPExcel/Shared/ZipArchive.php:108
Stack trace:
#0 /home/loqui/public_html/doorapp/inc/PHPExcel/PHPExcel/Writer/Excel2007.php(278): PHPExcel_Shared_ZipArchive->addFromString('_rels/.rels', '<?xml version="...')
#1 /home/loqui/public_html/doorapp/xls.php(66): PHPExcel_Writer_Excel2007->save('php://output')
#2 {main}
thrown in /home/loqui/public_html/doorapp/inc/PHPExcel/PHPExcel/Shared/ZipArchive.php on line 108
Question
As I don't have the Zip module enabled, and seemingly limited permissions in the working folder, how can I make this script download the correctly created Excel file?
If you are going to continue using PCLZIP, I would recommend checking the tmp directory that it is attempting to write to and see what user that directory is assigned to. More than likely Apache does not have write access to that tmp directory and thus is failing to write files inside of it. I am struggling with a similar problem on the ZipArchive front, but if you have sufficient permissions chown -R user:user foldername might alleviate your write issues.