PHPExcel create a new file - php

I am trying to create a new Excel file by using PHPExcel library with this code :
include ('/lib/PHPExcel/PHPExcel/IOFactory.php');
include ('/lib/PHPExcel/PHPExcel.php');
$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()
->setCreator("admin")
->setLastModifiedBy("admin")
->setTitle("Test")
->setSubject("template file")
->setDescription("template file")
->setKeywords("Prout");
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->SetCellValue('A1', 'Hello');
$objPHPExcel->getActiveSheet()->setTitle('Simple');
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save(dirname(__FILE__).'/file/test.xlsx');
The file is created, but when i try to open it my excel 2010 tells me there is unreadable content inside the file and i can't open it.
I tried a different way on a different forum but i am always arriving to the same result. I do this manipulation by using Ajax with jquery. Can this be the cause of this error?

I find how to resolve my problem ... but only half of it. Because it's work with this code :
$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()
->setCreator("Temporaris")
->setLastModifiedBy("Temporaris")
->setTitle("Template Relevé des heures intérimaires")
->setSubject("Template excel")
->setDescription("Template excel permettant la création d'un ou plusieurs relevés d'heures")
->setKeywords("Template excel");
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->SetCellValue('A1', "12");
$writer = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="excel.xls"');
header('Cache-Control: max-age=0');
$writer->save('php://output');
But it's Excel5 not Excel2007 and when i remplace Excel5 by Excel2007 i have the same error message.

Did you try this ?
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);

Related

PHPExcel : CodeIgniter error on output

I have this controller which renders my data from database to the PHPExcel library.
https://arjunphp.com/how-to-use-phpexcel-with-codeigniter/
this is my controller :
public function downloadTournamentData() {
$this->load->model("Tournament");
$var_data = array();
$var_data['tournament'] = $this->Tournament->getTournamentData();
$this->load->library('excel');
$this->excel->setActiveSheetIndex(0);
$this->excel->getActiveSheet()->setTitle('test');
$this->excel->getActiveSheet()->setCellValue('A1', 'member_username');
$this->excel->getActiveSheet()->setCellValue('B1', 'member_name');
$this->excel->getActiveSheet()->setCellValue('C1', 'member_phone');
$this->excel->getActiveSheet()->setCellValue('D1', 'member_email');
$this->excel->getActiveSheet()->setCellValue('E1', 'member_idcard');
$this->excel->getActiveSheet()->setCellValue('F1', 'Date_created');
$this->excel->getActiveSheet()->setCellValue('G1', 'team_name');
$this->excel->getActiveSheet()->setCellValue('H1', 'warnet_name');
$this->excel->getActiveSheet()->setCellValue('I1', 'warnet_cp');
$this->excel->getActiveSheet()->setCellValue('J1', 'warnet_phone');
$this->excel->getActiveSheet()->setCellValue('K1', 'region_name');
$this->excel->getActiveSheet()->setCellValue('L1', 'City');
$cell_inc = 2;
foreach($var_data['tournament'] as $k => $v) {
$this->excel->getActiveSheet()->setCellValue('A'.$cell_inc, $v['member_username']);
$this->excel->getActiveSheet()->setCellValue('B'.$cell_inc, $v['member_name']);
$this->excel->getActiveSheet()->setCellValue('C'.$cell_inc, $v['member_phone']);
$this->excel->getActiveSheet()->setCellValue('D'.$cell_inc, $v['member_email']);
$this->excel->getActiveSheet()->setCellValue('E'.$cell_inc, $v['member_idcard']);
$this->excel->getActiveSheet()->setCellValue('F'.$cell_inc, $v['Date_created']);
$this->excel->getActiveSheet()->setCellValue('G'.$cell_inc, $v['team_name']);
$this->excel->getActiveSheet()->setCellValue('H'.$cell_inc, $v['warnet_name']);
$this->excel->getActiveSheet()->setCellValue('I'.$cell_inc, $v['warnet_cp']);
$this->excel->getActiveSheet()->setCellValue('J'.$cell_inc, $v['warnet_phone']);
$this->excel->getActiveSheet()->setCellValue('K'.$cell_inc, $v['region_name']);
$this->excel->getActiveSheet()->setCellValue('L'.$cell_inc, $v['City']);
$cell_inc++;
}
date_default_timezone_set("Asia/Jakarta");
$this_date = date("Y-m-d");
$filename='pb_turnamen_data-'.$this_date.'.xls'; //save our workbook as this file name
header('Content-Type: application/vnd.ms-excel; charset=UTF-8'); //mime type
header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
header('Cache-Control: max-age=0'); //no cache
//save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type)
//if you want to save it as .XLSX Excel 2007 format
$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');
//force user to download the Excel file without writing it to server's HD
$objWriter->save('php://output');
}
It does download the excel file. Only problem is that the data is mixed up and it's all wrong.
Why is this happening? Is there something wrong with my controller?
Please provide solution...
Add
ob_end_clean();
after
$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');
final code is
$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');
ob_end_clean();
$objWriter->save('php://output');

Download Excel file using PHPExcel

I am trying to download excel file using PHPExcel, it's like it download the excel file nicely but the data in excel file is all crap.. it's not what i expected. I passed very basic methods to test my excel sheet data output.
here is the code i'am trying
else if($request->p['type'] == 'excel')
{
$report_type_name = "Graph Survey Report";
$ExcelReport = new ExcelApExport($sections, $group_definition, $questions, $sample_corrections);
$objPHPExcel = $ExcelReport->export($sets, $disp_filter);
header('Content-Type: application/vnd.ms-excel');
header("Content-Disposition: attachment; "
. escape_for_content_disposition("{$report_name} - {$report_type_name} - " . date("Y-m-d.H.i") . ".xls"));
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;
}
also I make object here and call phpexcel methods here
public function export($Sets, $disp_filter)
{
$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()->setTitle("Offic excel Test Document");
$objPHPExcel->getProperties()->setSubject(" Test Document");
$objPHPExcel->getProperties()->setDescription("Test document for XLS, generated using PHP classes.");
//echo date('H:i:s') . " Add some data\n";
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->SetCellValue('A1', 'Hello');
$objPHPExcel->getActiveSheet()->SetCellValue('B2', 'world!');
$objPHPExcel->getActiveSheet()->SetCellValue('C1', 'Hello');
$objPHPExcel->getActiveSheet()->SetCellValue('D2', 'world!');
return $objPHPExcel;
}
please can you suggest me why this crap data is showing up in my file instead of expected data.
thanks in advance
It needs basically
ob_clean();
before saving object.
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
This is the writer for .xlsx files. (See: https://github.com/PHPOffice/PHPExcel/blob/develop/Examples/01simple-download-xlsx.php)
So the right header would be:
// Redirect output to a client’s web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header("Content-Disposition: attachment; "
. escape_for_content_disposition("{$report_name} - {$report_type_name} - " . date("Y-m-d.H.i") . ".xlsx")); //.xlsX!
The writer for old .xls-files is:
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
See: https://github.com/PHPOffice/PHPExcel/blob/develop/Examples/01simple-download-xls.php
I ran into this problem recently and found another post here that is very helpful if ur still having the problem after using ob_clean()
check this post

File format or extension invalid PHPExcel

I try to use PHPExcel library for my report, this is my first time i use PHPExcel ,but error dialog appear telling that file format / extension invalid ,this is the syntax :
include '../_class/PHPExcel.php';
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
// Set document properties
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
->setLastModifiedBy("Rizal")
->setTitle("Laporan Anggota")
->setSubject("anggota")
->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
->setKeywords("office 2007 openxml php")
->setCategory("Test result file");
// Create the worksheet
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->setCellValue('A7', "No")
->setCellValue('B7', "NIS")
->setCellValue('C7', "NISN")
->setCellValue('D7', "Name")
->setCellValue('E7', "J.K")
->setCellValue('F7', "Birth")
->setCellValue('G7', "Address")
->setCellValue('H7', "Job")
// Redirect output to a client’s web browser (Excel2007)
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save(str_replace('.php', '.xlsx', __FILE__));
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="Test.xlsx"');
header('Cache-Control: max-age=0');
anyone can help me to solve it?
Try this example
<?php
$name = rand('0','10000').'.xlsx';
$n = rand('0','100');
error_reporting(E_ALL);
include 'PHPExcel/Classes/PHPExcel.php';
include 'PHPExcel/Classes/PHPExcel/Writer/Excel2007.php';
$o = new PHPExcel();
$o->getProperties()->setCreator('Creator');
$o->getProperties()->setLastModifiedBy('Last Modified');
$o->getProperties()->setTitle("Office 2007 XLSX Document ");
$o->getProperties()->setSubject('Subject');
$o->getProperties()->setDescription("Office 2007 XLSX, generated using PHP classes.");
$o->setActiveSheetIndex(0);
$o->getActiveSheet()->SetCellValue('A1', 'Name');
$o->getActiveSheet()->SetCellValue('B1', 'Rating');
$o->getActiveSheet()->SetCellValue('A2', 'Shawshank Redemption');
$o->getActiveSheet()->SetCellValue('B2', $n);
$o->getActiveSheet()->SetCellValue('A3', 'A Clockwork Orange');
$o->getActiveSheet()->SetCellValue('B3', $n);
$o->getActiveSheet()->setTitle('Films');
$objWriter = new PHPExcel_Writer_Excel2007($o);
$objWriter->save('excel/'.$name); // exce/ = filepath
?>
I think the problem you are having relates to the $objWriter variable.

PHPExcel and Php MVC Framework

I'm developing a basic php framework and y have a problem. I want to use PHPExcel for download reports in excel.
This is my Controller:
public function export(){
require_once ROOT."libs".DS."PHPExcel/PHPExcel.php";
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="sites.xls"');
header('Cache-Control: max-age=0');
$objPHPExcel = new PHPExcel();
$writer = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$writer->save('php://output');
}
The PHP library is here:
When I open the excel file get the following:
ÐÏࡱá;þÿ
þÿÿÿþÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿþÿà…ŸòùOh«‘+'³Ù0¸#Hh€˜
¤°äUntitled SpreadsheetUnknown CreatorUnknown Creator#`Do›ßÎ#`Do›ßÎ
ÌÑB°=¼%r8X"1ÜCalibriàõÿ À
ÌÑ*+‚€Áƒ„&ffffffæ?'ffffffæ?(è?)è?¡"dXX333333Ó?333333Ó?U}$
þÿÕÍÕœ.“—+,ù®0¼HPX`hp
xŽä
WorksheetFeuilles de calculRoot Entryÿÿÿÿÿÿÿÿ
I hope you can help. Thanks

PHPExcel is not generating correct xls file

I am using PHPExcel in my project to generate a xls file of data coming from the database. I have downloaded the library of PHPExcel and using it in my PHP class as give below :
Class name : A.class.php
Path : inside a folder named "inc";
PHPExcel Lib folder : inside "inc" and relative to A.class.php
class A{
// code stuff
public function phpexcelgenerate()
{
require_once 'PHPExcel_1.7.9_doc/Classes/PHPExcel.php';
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="sam.xlsx"');
header('Cache-Control: max-age=0');
$objPHPExcel = new PHPExcel;
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->SetCellValue('A1', "12");
$writer = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
// This line will force the file to download
$writer->save();
}
// code stuff
}
This code is generating a xls file but that file is fully empty. I am thinking that I have included the files of PHPExcel in a wrong way in PHP classes. Could anyone tell me where I am doing wrong OR an example how can I do the same ?
You need to pass a filename to the save() method. Then read the contents of that files and echo to the browser.
$writer = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$writer->save("excel.xls");
readfile("excel.xls");
first of all you need to read the file:
// Read the file
$objReader = PHPExcel_IOFactory::createReader(Excel5);
$objPHPExcel = $objReader->load($fileName);
now get the data from db:
//get data
$details = $this->main_model->get_details()->row();
then assign the data to correct cells:
$objPHPExcel->getActiveSheet()->setCellValue('A'.$row, $i);
$objPHPExcel->getActiveSheet()->setCellValue('B'.$row, $rec->eType);
at the end write the new file:
header('Content-Type: application/vnd.ms-excel'); //mime type
header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
header('Cache-Control: max-age=0'); //no cache
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
//force user to download the Excel file without writing it to server's HD
$objWriter->save('php://output');

Categories