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;
Related
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');
I have a table on an html page that I need to export. I have searched other examples on here but nothing that works for me.
Here is what I have.
$filename = "DownloadReport";
//$table = dbGrid($q);
$table = "<div><table><tbody><tr><td>cars</td><td>cars</td><td>cars</td></tr></tbody></table></div>";
$tmpfile = tempnam(sys_get_temp_dir(), 'html');
file_put_contents($tmpfile, $table);
$excelHTMLReader = PHPExcel_IOFactory::createReader('HTML');
$objPHPExcel = $excelHTMLReader->load($tmpfile);
$objPHPExcel->getActiveSheet()->setTitle('anynameyouwant');
unlink($tmpfile);
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="' . $filename . '-' . date("y.m.d") . '.xlsx"');
header('Cache-Control: max-age=0');
$writer = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$writer->save('php://output');
exit;
It properly downloads the file. But for some reason its saving my entire html page with javascript and css files missing. I only need to export the table string. I have a dbGrid function to create my table but for testing I have a simple table string.
How can I export just that table string, not my entire html page.
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');
I have the data with new lines in database.
for example:
Testing Habit
Testing Habit Testing Habit
Testing Habit Testing Habit Testing Habit
I want to show the data with space instead of new line in csv downloaded file.
Now I am using PHPExcel library to download csv file.
Here is my code:
$output_file_name = "myfile";
$output_file = $output_file_name . ".csv";
$output_file = mb_convert_encoding($output_file, "SJIS", "UTF-8");
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
$sheet = $objPHPExcel->getActiveSheet();
$sheet->setCellValue("A1", '"'.str_replace(chr(10), ' ', $info['ModelName']['habits']).'"');
header('Content-Type: text/csv');
header('Content-Disposition: attachment;filename="'.$output_file.'"');
header('Cache-Control: max-age=0');
header("Pragma: no-cache");
header("Expires: 0");
$objWriter = new PHPExcel_Writer_CSV($objPHPExcel);
$objWriter->setDelimiter(',');
$objWriter->setEnclosure('');
$objWriter->setLineEnding("\r\n");
$objWriter->setSheetIndex(0);
$objWriter->setUseBOM(true);
$objWriter->save('php://output');
How to remove the line feed?
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);