Salini L
Salini L

Reputation: 879

error in PHPExcel

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

Upvotes: 1

Views: 13638

Answers (2)

Mark Baker
Mark Baker

Reputation: 212522

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

Upvotes: 2

user1767754
user1767754

Reputation: 25154

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

Upvotes: 2

Related Questions