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');
}
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');
How to export the Excel (.csv or .xls) variable from database? It does not work to insert variable from database e.g. $temp (it can echo the single value from variable)
require_once(JPATH_LIBRARIES . '/phpexcel/library/PHPExcel.php');
require_once(JPATH_LIBRARIES.'/phpexcel/library/PHPExcel/IOFactory.php');$query = "SELECT referreid,insert_date,datareference FROM `miqi_alpha_userpoints_details` WHERE `referreid`='$referrerid' AND `enabled`='1' ORDER BY `insert_date` DESC";
$db->setQuery( $query );
$results = $db-> loadAssocList();
$temp = $results[0]['referreid'];
$objPHPExcel = new PHPExcel();
$objPHPExcel->getActiveSheet()->setCellValue('A1','123');
$objPHPExcel->getActiveSheet()->setCellValue('B1',$temp);
header('Content-Type: text/plain;charset=utf-8'); //mime type
header('Content-Disposition: attachment;filename="abc.csv"'); //tell browser what's the file name
header('Cache-Control: max-age=0'); //no cache
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'CSV');
$objWriter->save('php://output');
exit;
I am trying to use PHPexcel plugin to generate downloadable excel file from mysql data. Unfortunately, I am only getting only the column headers and not their values in the downloaded excel file
Where am I going wrong ? Also, I would like unicode characters to show properly.
Following is my code of the downloadexcel.php file -
<?php
require_once 'Classes/PHPExcel.php';
require_once 'Classes/PHPExcel/Writer/Excel2007.php';
$objPHPExcel = new PHPExcel();
$data=array();
$sql="SELECT s.t_id,s.t_text,p.user_name,p.description,s.time,p.place from t
AS s INNER JOIN users AS p ON s.user_name=p.user_name order by s.time desc";
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result)) {
$array=array("T Link" => $row[0],"T"=>$row[1] , "User Name" => $row[2] ,
"User Profile" => $row[3], "Time" => $row[4] , "Place" => $row[5]);
array_push($data,$array);
}
$objPHPExcel->getActiveSheet()->setCellValue('A1', 'T Link');
$objPHPExcel->getActiveSheet()->setCellValue('B1', 'T');
$objPHPExcel->getActiveSheet()->setCellValue('C1', 'User Name');
$objPHPExcel->getActiveSheet()->setCellValue('D1', 'User Profile');
$objPHPExcel->getActiveSheet()->setCellValue('E1', 'Time');
$objPHPExcel->getActiveSheet()->setCellValue('F1', 'Place');
$row=2;
foreach($data as $row->$value) {
$objPHPExcel->getActiveSheet()->setCellValue('A'.$row,$value->t_id);
$objPHPExcel->getActiveSheet()->setCellValue('B'.$row,$value->t_text);
$objPHPExcel->getActiveSheet()->setCellValue('C'.$row,$value->user_name);
$objPHPExcel->getActiveSheet()->setCellValue('D'.$row,$value->description);
$objPHPExcel->getActiveSheet()->setCellValue('E'.$row,$value->time);
$objPHPExcel->getActiveSheet()->setCellValue('F'.$row,$value->place);
$row++;
}
header('Content-Type: application/vnd.openxmlformats-
officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="helloworld.xlsx"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
?>
Based on comments of Mark Baker, edited the code but still not getting any result. This is my edited code -
<?php
require_once 'Classes/PHPExcel.php';
require_once 'Classes/PHPExcel/Writer/Excel2007.php';
$objPHPExcel = new PHPExcel();
$data=array();
$sql="SELECT s.t_id,s.t_text,p.user_name,p.description,s.time,p.place from t
AS s INNER JOIN users AS p ON s.user_name=p.user_name order by s.time desc";
$result = mysqli_query($con,$sql);
$objPHPExcel->getActiveSheet()->setCellValue('A1', 'T Link');
$objPHPExcel->getActiveSheet()->setCellValue('B1', 'T');
$objPHPExcel->getActiveSheet()->setCellValue('C1', 'User Name');
$objPHPExcel->getActiveSheet()->setCellValue('D1', 'User Profile');
$objPHPExcel->getActiveSheet()->setCellValue('E1', 'Time');
$objPHPExcel->getActiveSheet()->setCellValue('F1', 'Place');
while ($row = mysqli_fetch_array($result))
{
$n=2;
foreach($data as $row) {
$objPHPExcel->getActiveSheet()->setCellValue('A'.$n,$row['t_id']);
$objPHPExcel->getActiveSheet()->setCellValue('B'.$n,$row['t_text']);
$objPHPExcel->getActiveSheet()->setCellValue('C'.$n,$row['user_name']);
$objPHPExcel->getActiveSheet()->setCellValue('D'.$n,$row['description']);
$objPHPExcel->getActiveSheet()->setCellValue('E'.$n,$row['time']);
$objPHPExcel->getActiveSheet()->setCellValue('F'.$n,$row['place']);
$n++;
}
}
header('Content-Type: application/vnd.openxmlformats-
officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="helloworld.xlsx"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
?>
Change this line:
foreach($data as $row->$value) {
to
foreach($data as $row => $value) {
"->" operator is used with objects. The difference between the two operators is explained here: https://stackoverflow.com/a/14037376/577778
Updated Code
<?php
require_once 'Classes/PHPExcel.php';
require_once 'Classes/PHPExcel/Writer/Excel2007.php';
$objPHPExcel = new PHPExcel();
$data=array();
$sql="SELECT s.t_id,s.t_text,p.user_name,p.description,s.time,p.place from t
AS s INNER JOIN users AS p ON s.user_name=p.user_name order by s.time desc";
$result = mysqli_query($con,$sql);
$objPHPExcel->getActiveSheet()->setCellValue('A1', 'T Link');
$objPHPExcel->getActiveSheet()->setCellValue('B1', 'T');
$objPHPExcel->getActiveSheet()->setCellValue('C1', 'User Name');
$objPHPExcel->getActiveSheet()->setCellValue('D1', 'User Profile');
$objPHPExcel->getActiveSheet()->setCellValue('E1', 'Time');
$objPHPExcel->getActiveSheet()->setCellValue('F1', 'Place');
$n=2;
while ($row = mysqli_fetch_array($result))
{
$objPHPExcel->getActiveSheet()->setCellValue('A'.$n,$row['t_id']);
$objPHPExcel->getActiveSheet()->setCellValue('B'.$n,$row['t_text']);
$objPHPExcel->getActiveSheet()->setCellValue('C'.$n,$row['user_name']);
$objPHPExcel->getActiveSheet()->setCellValue('D'.$n,$row['description']);
$objPHPExcel->getActiveSheet()->setCellValue('E'.$n,$row['time']);
$objPHPExcel->getActiveSheet()->setCellValue('F'.$n,$row['place']);
$n++;
}
header('Content-Type: application/vnd.openxmlformats-
officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="helloworld.xlsx"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
?>
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');