I am trying to write data to a template and download it.
If i use the sample files as a template, it works but once I use my own it says its corrupt.
Any thoughts?
My code is:
<?php
require_once 'PHPExcel.php';
// Create new PHPExcel object
$fileName = '31docproperties.xlsx'; //location of template for PID type
$fileType = 'Excel2007';
$objReader = PHPExcel_IOFactory::createReader($fileType);
$objPHPExcel = $objReader->load($fileName);
$objPHPExcel->setActiveSheetIndex(0)->setCellValue('B4', 'Test Data');
// Redirect output to a client’s web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="01simple.xlsx"');
header('Cache-Control: max-age=0');
ob_end_clean();
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;
?>
Related
this is my simple code to create an xlsx file and open it with openoffice
require(APPPATH.'/PHPExcel-1.8/Classes/PHPExcel.php');
require(APPPATH.'/PHPExcel-1.8/Classes/PHPExcel/Writer/Excel2007.php');
$this->load->library('excel');
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0)
->SetCellValue('A1', 'Calendario Attivit');
$filename = "prova".".xlsx";
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header('Content-Disposition: attachment;filename="'.$filename.'"');
header("Cache-Control: max-age=0");
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;
the file is downloaded but when open it i get this:
someone can help me ?
Try to add ob_end_clean() before the output :
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
ob_end_clean();
$objWriter->save('php://output');
I use latest PHPExcel library for creating .xls, .xlsx files. When i try to output created file like this:
ob_end_clean();
$file = 'test.xls';
header('Content-Type: application/vnd.ms-excel; charset=UTF-8');
header('Content-Disposition: attachment; filename="'.$file.'"');
header('Cache-Control: max-age=0');
header('Cache-Control: max-age=1');
$objPHPWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objPHPWriter->setIncludeCharts(TRUE);
$objPHPWriter->save('php://output');
exit;
everything is fine until I try to create and output any charts. So i changed my code according to example:
ob_end_clean();
$objPHPWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objPHPWriter->setIncludeCharts(TRUE);
$objPHPWriter->save(str_replace('.php', '.xlsx', __FILE__));
exit;
This is working, but the file is not downloaded. So i modified code to output created file:
ob_end_clean();
$file = 'test.xlsx';
header('Content-Type: Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; charset=UTF-8');
header('Content-Disposition: attachment; filename="'.$file.'"');
header('Cache-Control: max-age=0');
header('Cache-Control: max-age=1');
$objPHPWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objPHPWriter->setIncludeCharts(TRUE);
$objPHPWriter->save('php://output');
exit;
And this is not working, ends up with ERR_INVALID_RESPONSE.
Also when i store excel file with charts with second code block, it's not compatibile with MS Excel 2016. It says the file is corrupted and the data were replaced or deleted. What am I missing? Thanks a lot.
I figured out one simple solution, first step is to store created .xlsx file, second is to download file with header("Location:"). Redundant files are deleted with array_map function.
array_map('unlink', glob( __DIR__."/*.xlsx"));
$file = 'test.xlsx';
$objPHPWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objPHPWriter->setIncludeCharts(TRUE);
$objPHPWriter->save($file);
header("Location: $file");
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');
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');
I have some problem with PHPExcel where it keep throws couldn't reading (generated) file, here's the code:
include 'PHPExcel/PHPExcel.php';
$rtype = $_REQUEST['rtype'];
// set headers to redirect output to client browser as a file download
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="report_'.$rtype.'.xls"');
header('Cache-Control: no-cache');
header('Pragma: no-cache');
header('Expires: 0');
//-----Create a reader, set some parameters and read in the file-----
$objReader = PHPExcel_IOFactory::createReader('CSV');
$objReader->setDelimiter(',');
$objReader->setEnclosure('');
$objReader->setLineEnding("\r\n");
$objReader->setSheetIndex(0);
$objPHPExcel = $objReader->load('db_report.php?rtype='.$rtype.'&type=csv');
//-----Create a Writer and output the file to the browser-----
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
already checked db_report.php and it output a csv file, so nothing wrong for this file
You try to load String value 'db_report.php?rtype='.$rtype.'&type=csv' is it not url.
Load first data ex:
$data = file_get_contents('http://domen.com/db_report.php?rtype='.$rtype.'&type=csv');
$objPHPExcel = $objReader->load($data);