I have this code and works fine downloading with excel file with PHPExcel and CI but I want to download it as zip file and inside of it is the excel file.
$header = array(
'foodgroup','energy','protein','fat','cho','calcium','iron','thiamin','niacin','vitamin_c'
,'vitamin_a',
);
require_once APPPATH."/third_party/PHPExcel.php";
$sheet = new PHPExcel();
$file = $this->dietary_model->getById($subject_id,'dietary_subject');
$filename = $file->name;
$this->load->helper('date');
$date = date('Y-m-d');
//1st Sheet
$users = $this->dietary_model->getdata($subject_id,'1');
$sheet->setActiveSheetIndex(0);
$activeSheet = $sheet->getActiveSheet();
$activeSheet->setTitle('Day 1');
$activeSheet->getStyle('A1:T1')->getFont()->setBold(true);
$activeSheet->fromArray($header, null, 'A1');
$activeSheet->fromArray($users,null, 'A2');
//2nd Sheet
$users2 = $this->dietary_model->getdata($subject_id,'2');
$sheet->createSheet();
$sheet->setActiveSheetIndex(1);
$activeSheet2 = $sheet->getActiveSheet(1);
$activeSheet2->setTitle('Day 2');
$activeSheet2->getStyle('A1:T1')->getFont()->setBold(true);
$activeSheet2->fromArray($header, null, 'A1');
$activeSheet2->fromArray($users2,null, 'A2');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename='.$filename.' '.$date.'.xlsx');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($sheet, 'Excel2007');
echo '<script>console.log('.$objWriter.')</script>';
exit;
Try this:
$header = array(
'foodgroup','energy','protein','fat','cho','calcium','iron','thiamin','niacin','vitamin_c'
,'vitamin_a',
);
require_once APPPATH."/third_party/PHPExcel.php";
$sheet = new PHPExcel();
$file = $this->dietary_model->getById($subject_id,'dietary_subject');
$filename = $file->name;
$this->load->helper('date');
$date = date('Y-m-d');
//1st Sheet
$users = $this->dietary_model->getdata($subject_id,'1');
$sheet->setActiveSheetIndex(0);
$activeSheet = $sheet->getActiveSheet();
$activeSheet->setTitle('Day 1');
$activeSheet->getStyle('A1:T1')->getFont()->setBold(true);
$activeSheet->fromArray($header, null, 'A1');
$activeSheet->fromArray($users,null, 'A2');
//2nd Sheet
$users2 = $this->dietary_model->getdata($subject_id,'2');
$sheet->createSheet();
$sheet->setActiveSheetIndex(1);
$activeSheet2 = $sheet->getActiveSheet(1);
$activeSheet2->setTitle('Day 2');
$activeSheet2->getStyle('A1:T1')->getFont()->setBold(true);
$activeSheet2->fromArray($header, null, 'A1');
$activeSheet2->fromArray($users2,null, 'A2');
$objWriter = PHPExcel_IOFactory::createWriter($sheet, 'Excel2007');
$excel_file_tmp = tempnam("/tmp", 'your_prefix');
$objWriter->save($excel_file_tmp);
//zip
$zip_file_tmp = tempnam("/tmp", 'your_prefix');
$zip = new ZipArchive();
$zip->open($zip_file_tmp, ZipArchive::OVERWRITE);
$zip->addFile($excel_file_tmp, 'your_name.xlsx');
$zip->close();
//download
$download_filename = 'your_name.zip';
header("Content-Type: application/zip");
header("Content-Length: " . filesize($zip_file_tmp));
header("Content-Disposition: attachment; filename=\"" . $download_filename . "\"");
readfile($zip_file_tmp);
unlink($excel_file_tmp);
unlink($zip_file_tmp);
Related
I am using phpexcel library for generate excel file and i want change background color of call
How to set specific color to active cell when creating XLS document in PHPExcel?
here id my code which i am using
public function exportCSV() {
// create file name
$fileName = 'data-'.time().'.xlsx';
// load excel library
$this->load->library('excel');
// $listInfo = $this->export->exportList();
$ordersData = $this->admin_development->getDevelopmentDetails();
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
// set Header
$objPHPExcel->getActiveSheet()->SetCellValue('A1', 'Lott Number');
$objPHPExcel->getActiveSheet()->SetCellValue('B1', 'Slot Number');
$objPHPExcel->getActiveSheet()->SetCellValue('C1', 'Party Name');
// set Row
$rowCount = 2;
foreach ($ordersData as $list) {
$objPHPExcel->getActiveSheet()->SetCellValue('A' . $rowCount, $list->lott_number);
$objPHPExcel->getActiveSheet()->SetCellValue('B' . $rowCount, $list->category);
$objPHPExcel->getActiveSheet()->SetCellValue('C' . $rowCount, $list->party_name);
$objPHPExcel->getActiveSheet()
->getStyle('A' . $rowCount)
->getFill()
->setFillType(PHPExcel_Style_Fill::FILL_SOLID)
->getStartColor()
->setRGB('FF0000');
$rowCount++;
}
$filename = "tutsmake". date("Y-m-d-H-i-s").".csv";
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$filename.'"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'CSV');
$objWriter->save('php://output');
}
public function genrate_business_XLS_file($export_data) {
// create file name
$fileName = 'Business-report-'.date("d-M-Y").'-'.time().'.xlsx';
// load excel library
$this->load->library('excel');
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
// set Header
$objPHPExcel->getActiveSheet()->SetCellValue('A1', 'USER ID');
$objPHPExcel->getActiveSheet()->SetCellValue('B1', 'BUSINESS NAME');
$objPHPExcel->getActiveSheet()->SetCellValue('C1', 'PHONE');
$objPHPExcel->getActiveSheet()->SetCellValue('D1', 'EMAIL');
// set Row
$rowCount = 2;
foreach ($export_data as $val)
{
//echo"<pre>";print_r($val);die;
$action = '';
if($val['b_status']==0){
$action = 'Rejected Application';
} else if($val['b_status']==1){
$action = 'Approved Application';
}else if($val['b_status']==2){
$action = 'New Application';
}
$objPHPExcel->getActiveSheet()->SetCellValue('A' . $rowCount, $val['b_id']);
$objPHPExcel->getActiveSheet()->SetCellValue('B' . $rowCount, $val['b_name']?$val['b_name']:'N/A');
$objPHPExcel->getActiveSheet()->SetCellValue('C' . $rowCount, $val['b_phone']?$val['b_phone']:'N/A');
$objPHPExcel->getActiveSheet()->SetCellValue('D' . $rowCount, $val['b_email']?$val['b_email']:'N/A');
$rowCount++;
}
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save($fileName);
// download file
header("Content-Description: File Transfer");
header("Content-Type: application/vnd.ms-excel");
redirect(site_url().$fileName);
}`
Save the file to php output not on the server
header("Content-Type: application/vnd.ms-excel");
header('Content-Disposition: attachment;filename="'.$fileName.'"');
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save('php://output');
I an trying to export data to an Excel file using PHPExcel libraries with Cakephp 2.5.
My Codes :
<?php
App::import('Vendor', 'PHPExcel', array('file' => 'PHPExcel'.DS.'PHPExcel.php'));
App::import('Vendor', 'PHPExcel_IOFactory', array('file' => 'PHPExcel'.DS.'PHPExcel'.DS.'IOFactory.php'));
App::import('Vendor', 'PHPExcel_IOFactory', array('file' => 'PHPExcel'.DS.'PHPExcel'.DS.'Style.php'));
class LeadUploadController extends AppController {
public function exel_download($emp_id='')
{
$this->autoRender = false;
$this-> layout='ajax';
$objPHPExcel = new PHPExcel();
$serialnumber=0;
$tmparray =array("Sr.Number","Employee ID","Employee Name");
$sheet =array($tmparray);
$tmparray =array();
$serialnumber = $serialnumber + 1;
array_push($tmparray,$serialnumber);
$employeelogin = 'aa';
array_push($tmparray,$employeelogin);
$employeename = 'bb';
array_push($tmparray,$employeename);
array_push($sheet,$tmparray);
header('Content-type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="file.xlsx"');
$worksheet = $objPHPExcel->getActiveSheet();
foreach($sheet as $row => $columns) {
foreach($columns as $column => $data) {
$worksheet->setCellValueByColumnAndRow($column, $row + 1, $data);
}
}
$objPHPExcel->getActiveSheet()->getStyle("A1:I1")->getFont()->setBold(true);
$objPHPExcel->setActiveSheetIndex(0);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save(str_replace('.php', '.xlsx', __FILE__));
}
}
The problem is the downloaded Excel file contain no any data it completely blank with an error "can not open the file because of file format or extension not valid ". Have not any idea what's wrong with these code.
The following code should work on xls extension.
$objPHPExcel = new PHPExcel();
$serialnumber=0;
$tmparray =array("Sr.Number","Employee ID","Employee Name");
$sheet =array($tmparray);
$tmparray =array();
$serialnumber = $serialnumber + 1;
array_push($tmparray,$serialnumber);
$employeelogin = 'aa';
array_push($tmparray,$employeelogin);
$employeename = 'bb';
array_push($tmparray,$employeename);
array_push($sheet,$tmparray);
header('Content-type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="file.xls"');
$worksheet = $objPHPExcel->getActiveSheet();
foreach($sheet as $row => $columns) {
foreach($columns as $column => $data) {
$worksheet->setCellValueByColumnAndRow($column, $row + 1, $data);
}
}
$objPHPExcel->getActiveSheet()->getStyle("A1:I1")->getFont()->setBold(true);
$objPHPExcel->setActiveSheetIndex(0);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
I am using codeigniter and phpword I am trying to get the spacing like in this image below.
How ever my when I export the document out put is like
Question: In PHPWord how can I set the correct spacing I would like to make my out put the same as the first image.
public function export() {
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$results = $this->get_events_for_export();
foreach ($results as $result) {
$date = strtotime($result['event_date']);
$line = date('M', $date) .' '. date('d', $date) .' '. date('D', $date) .' '. htmlentities($result['event_title']);
$section->addText($line . "\n");
}
// Saving the document as OOXML file...
$filename = 'date1.docx';
header("Content-Description: File Transfer");
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Expires: 0');
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save("php://output");
exit();
}
After a couple of hours reading through some examples problem solved now using addParagraphStyle()
public function export() {
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$leftTabStyleName = 'centerTab';
$phpWord->addParagraphStyle($leftTabStyleName, array('tabs' => array(new \PhpOffice\PhpWord\Style\Tab('center', 4680))));
$section->addTextBreak();
// New portrait section
$section = $phpWord->addSection();
// Add listitem elements
$fontStyle = new \PhpOffice\PhpWord\Style\Font();
$fontStyle->setBold(false);
$fontStyle->setName('Tahoma');
$fontStyle->setSize(16);
$section->addText("\tClub Program " . date('Y') .' / ' . date('Y', strtotime('+1 year')), $fontStyle, $leftTabStyleName);
$multipleTabsStyleName = 'multipleTab';
$phpWord->addParagraphStyle(
$multipleTabsStyleName,
array(
'tabs' => array(
new \PhpOffice\PhpWord\Style\Tab('left', 1000),
new \PhpOffice\PhpWord\Style\Tab('center', 1000),
new \PhpOffice\PhpWord\Style\Tab('right', 1000),
)
)
);
$results = $this->get_events_for_export();
foreach ($results as $result) {
$date = strtotime($result['event_date']);
$section->addText(date('M', $date) ."\t". date('d', $date) ."\t". date('D', $date) ."\t". htmlentities($result['event_title']), null, $multipleTabsStyleName);
}
$filename = 'club_program-' . time() . '.docx';
header("Content-Description: File Transfer");
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Expires: 0');
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save("php://output");
exit();
}
I would like to export the data and insert the Excel file as an attachment. How to do that with codeigniter 2.2.4.
// PHP EXPORT DATA
...
...
$objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5');
header('Content-type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="test.xls"');
header('Cache-Control: max-age=0');
$objWriter->save('php://output');
// SEND MAIL
$this->load->library('email');
$this->email->from('test#gmail.com', 'Test');
$this->email->to('test#gmail.com', 'Test');
$this->email->subject('EXPORT DATA FILE');
$this->email->message("Test content");
$this->email->attach("INSERT HERE THE EXPORTED FILE");
try {
$this->email->send();
}
catch(Exception $e) {
echo $e->getMessage();
}
My controller :
public function export() {
$this->load->library('PHPExcel');
$this->load->library('PHPExcel/IOFactory');
$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()->setTitle("title")->setDescription("description");
// Assign cell values
$objPHPExcel->setActiveSheetIndex(0);
// /////////////////////////////////////////////////////
$wappi_query = 'SELECT * FROM ' . $my_table . '';
$wappi_list = $this->db->query($wappi_query)->result();
// /////////////////////////////////////////////////////
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth("10");
/*Protected Sheet */
$objPHPExcel->getActiveSheet()->getProtection()->setSheet(false);
/* File information */
$objPHPExcel->getProperties()->setCreator("")->setLastModifiedBy("")->setTitle("")->setSubject("")->setDescription("")->setKeywords("")->setCategory("");
$objPHPExcel->getDefaultStyle()->getFont()->setName('Tahoma')->setSize(10)->setBold(false);
$objPHPExcel->getActiveSheet()->setCellValue('A1', "ID");
$objPHPExcel->getActiveSheet()->setCellValue('B1', "UNITY");
$i = 2;
foreach ($wappi_list as $data) {
$objPHPExcel->getActiveSheet()->setCellValue('A' . $i . '', $data->id);
$objPHPExcel->getActiveSheet()->setCellValue('B' . $i . '', $data->unity);
$i++;
}
$objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5');
header('Content-type: application/vnd.ms-excel');
// It will be called file.xls
header('Content-Disposition: attachment; filename="unity_' . date('d-m-Y') . '.xls"');
header('Cache-Control: max-age=0');
$objWriter->save('php://output');
}