How to set cell color in phpExcel - php

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');
}

Related

php Excel downloading file in 2 places, 1st in htdocs and as wellas in downloads folder, how to stop file from downloading in htdocs or on server?

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');

PHPExcel: Some Lines are not exporting in my .xls file while importing but shown when i echo the output?

I am using PHPExcel with mysql and MongoDB to export data in Excel .XLS file but only in 1 Column some Lines are not showing and some are showing in same Columns while exporting although all are shown when i use $print_() to check output in browser
Here is my PHP Code -
$objPHPExcel = new PHPExcel();
$sheet = $objPHPExcel->getActiveSheet();
$sheet->setCellValue('M1', 'Headline');
$objPHPExcel->getActiveSheet()->getStyle('M')->applyFromArray($styleArray);
$objPHPExcel->getActiveSheet()->getStyle('M')->getFont()->setUnderline(true);
if ($result['type'] == "WEB") {
$sheet->setCellValue('M' . ($results + 2), $result['headline']);
$sheet->getCell('M' . ($results + 2))->getHyperlink()->setUrl($result['url']);
$sheet->getCell('M' . ($results + 2))->getHyperlink()->setTooltip('Navigate to website');
}
and this is my output
output
I post here an example without link (you can edit my code):
$objPHPExcel = new PHPExcel();
$result = $db->query("SELECT * FROM YOURTABLE") or die(mysql_error());
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->SetCellValue('M1', 'Headline');
$objPHPExcel->getActiveSheet()->getStyle("M1")->getFont()->setBold(true);
$rowCount = 2;
while($row = $result->fetch_assoc()){
$objPHPExcel->getActiveSheet()->SetCellValue('M'.$rowCount, mb_strtoupper($row['headline'],'UTF-8'));
$rowCount++;
}
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
header('Content-Type: application/vnd.ms-excel'); //mime type
header('Content-Disposition: attachment;filename="you-file-name.xlsx"'); //tell browser what's the file name
header('Cache-Control: max-age=0'); //no cache
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');

Export data and send mail attachment Codeigniter

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');
}

PHPExcel download as zip file

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);

Overflow width phpexcel

I'm using PHPExcel to generate a listing.
The problem is that the text overflows the width of the cell B.
How do I place a fixed width and length automatic, so that it does not overflow ?
Attachment:
Code:
$objPHPExcel = new PHPExcel();
$objPHPExcel->
getProperties()
->setCreator("TEDnologia.com")
->setLastModifiedBy("TEDnologia.com")
->setTitle("Exportar Excel con PHP")
->setSubject("Documento de prueba")
->setDescription("Documento generado con PHPExcel")
->setKeywords("usuarios phpexcel")
->setCategory("reportes");
$query = mysql_query('select * from articulos');
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', 'Nombre')
->setCellValue('B1', 'E-mail')
->setCellValue('C1', 'E-mail')
;
$i=2;
while($row = mysql_fetch_assoc($query)){
$xx = "A".$i;
$xxx = "B".$i;
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(10);
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue($xx, $row["cod"])
->setCellValue($xxx, $row["descripcion"]);
;
$xxxx = "C".$i;
$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setName('Logo');
$objDrawing->setDescription('Logo');
$objDrawing->setPath('archivos/articulos/'.$row["imagen"].'');
$objDrawing->setCoordinates($xxxx);
$objDrawing->setHeight(36);
$objDrawing->setWidth(36);
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
$i++;
}
$objPHPExcel->getActiveSheet()->setTitle('Usuarios');
$objPHPExcel->setActiveSheetIndex(0);
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="01simple.xls"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
$objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setWrapText(true);

Categories