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');
}
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');
Hello everyone in my project, I am trying to export data from database to an xlsx file but I am not getting correct data.I have attached image of data.
I am using the following code.
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Writer\Xls;
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
$exportArray = array();
$query = mysqli_query($conn, "select * from table");
if(mysqli_num_rows($query) > 0){
while ($row = mysqli_fetch_assoc($query)) {
$exportArray[$exp]['id'] = $row['id'];
$exportArray[$exp]['name'] = $row['name'];
$exportArray[$exp]['address'] = $row['address'];
$exp++;
}
}
$array = array();
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'id');
$sheet->setCellValue('B1', 'name');
$sheet->setCellValue('C1', 'address');
$rowCount = 2;
foreach ($exportArray as $value) {
$sheet->setCellValue('A' . $rowCount, $value['id']);
$sheet->setCellValue('B' . $rowCount, $value['name']);
$sheet->setCellValue('C' . $rowCount, $value['address']);
$rowCount++;
}
$fileName = 'test123.xls';
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'. $fileName .'.xlsx"');
header('Cache-Control: max-age=0');
$writer->save('php://output');
exit();
When I see the sheet data using below code
$sheetData = $sheet->toArray(null, true, true, true);
print_r($sheetData);
I am getting the right output. Everything looks fine but I don't understand, why am I getting data in wrong format in sheet?
Try This:
<?php
require 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Helper\Sample;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
$conn = new mysqli("localhost","root","root","test");
$sql = "SELECT * FROM customers";
$result = $conn->query($sql);
$write_array = array();
$fileName = "excel.xlsx";
$write_array[] = array("id","name","address");
if($result->num_rows > 0)
{
while($row = $result->fetch_assoc())
{
$write_array[] = array($row["id"],$row["name"],$row["address"]);
}
}
$conn->close();
$spreadsheet = new Spreadsheet();
$spreadsheet->setActiveSheetIndex(0);
$spreadsheet->getActiveSheet()->fromArray($write_array,NULL,'A1');
$spreadsheet->getActiveSheet()->setTitle("My Excel");
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$fileName.'"');
header('Cache-Control: max-age=0');
header('Cache-Control: max-age=1');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: cache, must-revalidate');
header('Pragma: public');
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('php://output');
Add this use PhpOffice\PhpSpreadsheet\IOFactory;
Now use the following code to export data in Xlsx format
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="test.xlsx"');
header('Cache-Control: max-age=0');
header('Cache-Control: max-age=1');
header('Cache-Control: cache, must-revalidate');
header('Pragma: public');
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('php://output');
in my case none of the above solutions worked.
I finally managed to solve it by adding this line: ob_end_clean();
just before: $writer->save('php://output');
ob_end_clean();
$writer->save('php://output');
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);
I am using PHPExcel 1.8.0 in my project. When I want to generate and download a .xls file with some data from my database, the file is downloaded with garbage data. Here is my code:
$result = mysql_query("select * from my_table");
ob_start();
//excel code
$this->load->library('excel');
//activate worksheet number 1
$this->excel->setActiveSheetIndex(0);
//name the worksheet
$this->excel->getActiveSheet()->setTitle('test worksheet');
$rowNumber = 5;
while ($row = mysql_fetch_assoc($result)) {
$col = 'B';
foreach ($fields as $cell) {
$this->excel->getActiveSheet()->setCellValue($col .$rowNumber, $row[$cell]);
$col++;
}
$rowNumber++;
}
// ob_end_clean();
// ob_clean() ;
$filename='report.xls'; //save our workbook as this file name
header('Content-Type: application/vnd.ms-excel'); //mime type
header('Content-Disposition: attachment;filename="'.$filename.'"');
header('Cache-Control: max-age=0'); //no cache
$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');
$objWriter->save('php://output');
exit();
I have used both ob_end_clean() and ob_clean() but nothing works.
Try this code:
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
ob_end_clean();
$objWriter->save('php://output');