I am using HTML code in a variable and I do not know how can I pass to generate Excel using PHPExcel.
I am able to generate Excel with load HTML file but if I want use HTML in a variable then I am confused. Please give me an idea on how to implement this.
My sample code is below.
<?php
require_once 'PHPExcel.php';
require_once 'PHPExcel/IOFactory.php';
$objPHPExcel = new PHPExcel();
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="name_of_file.xls"');
header('Cache-Control: max-age=0');
$html ='<table><tr><td><font color="blue">Hello</font></td><td><font style="color: blue;">Rest</font></td></tr><tr><td><font style="color: green;">Next</font></td><td><font style="color: Yellow;">Back</font></td></tr></table>';
$wizard = new PHPExcel_Helper_HTML;
$richText = $wizard->buildTextRun($html);
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->setCellValue('A1', $richText);
$objPHPExcel->getActiveSheet()->setTitle('Sheet 1');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output', 'w');
?>
When I am using it in variable then its showing all in column vise but I want that all should come new row vise.
Related
I am trying to render html into excel using php excel. Here is my code.
$value = "<ul><li>new</li></ul>";
$objTpl = new PHPExcel();
$objTpl->createSheet(0)
->setTitle("Title")
->setCellValue ( 'A1', 'Value' );
$objTpl->getSheet(0)->setCellValueExplicit("A2", $value);
header('Content-Type: application/vnd.ms-excel; ');
header('Content-Disposition: attachment;filename="Test"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objTpl, 'Excel5');
$objWriter->save('php://output');
PHP excel is creating the excel but the cell value is coming up as
<ul><li>new</li></ul>
But i want it to be shown as new.
Is there is way to do that in php. Please help!
I want to download excel file (instead of saving it). Since I want to export HTML table I am using PHPExcel_reader_HTML to read contents of HTML table. Excel file is created perfectly when I want to save it but when I want to download it, nothing happens. Does someone knows where's the problem?
Code:
<?php
// Load the table view into a variable
$html = $_POST["table"];
require_once('php/PHPExcel/Classes/PHPExcel.php');
// Put the html into a temporary file
$objPHPExcel = new PHPExcel();
$tmpfile = time().'.html';
file_put_contents($tmpfile, $html);
// Read the contents of the file into PHPExcel Reader class
$reader = new PHPExcel_Reader_HTML;
$content = $reader->load($tmpfile);
// Pass to writer and output as needed
$objWriter = PHPExcel_IOFactory::createWriter($content, 'Excel2007');
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header("Content-Disposition: attachment; filename=\"results.xlsx\"");
header("Cache-Control: max-age=0");
$objWriter->save("php://output");
ob_clean();
// Delete temporary file
unlink($tmpfile);
?>
Hi everyone I have created a program to export MySQL data to excel but at some point I am getting the error the time I am trying to open the excel file.
The error: The file you are trying to open is in a different format,than specified by the file extension.Verify that the file is not corrupt and is from the trusted source before opening the file.Do you want to open the file now?
When I click yes i get the some funny characters on that excel file.
Here is my code:
include 'setup.php';
_connectsurvey(); //Database
require_once 'PHPExcel.php';
$objPHPExcel = new PHPExcel();
$query = "SELECT * FROM questionanswer";
$result = mysql_query($query) or die(mysql_error());
$objPHPExcel = new PHPExcel();
$rowCount = 1;
while($row = mysql_fetch_array($result)){
$objPHPExcel->getActiveSheet()->SetCellValue('A'.$rowCount, $row['gender']);
$objPHPExcel->getActiveSheet()->SetCellValue('B'.$rowCount, $row['city']);
$rowCount++;
pr($objPHPExcel);
}
header('Content-Type: application/vnd.openxmlformats- officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="survey.xls"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
Near the bottom when you name the file use file extension .xlsx it is the proper file extension for openxml based documents from office 2007 should fix your issue.
header('Content-Disposition: attachment;filename="survey.xlsx"');
If you absolutely need to use xls change the output type to Excel2003 but then you need to change the content type header to match that as well.
I'm developing a basic php framework and y have a problem. I want to use PHPExcel for download reports in excel.
This is my Controller:
public function export(){
require_once ROOT."libs".DS."PHPExcel/PHPExcel.php";
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="sites.xls"');
header('Cache-Control: max-age=0');
$objPHPExcel = new PHPExcel();
$writer = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$writer->save('php://output');
}
The PHP library is here:
When I open the excel file get the following:
ÐÏࡱá;þÿ
þÿÿÿþÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿþÿà…ŸòùOh«‘+'³Ù0¸#Hh€˜
¤°äUntitled SpreadsheetUnknown CreatorUnknown Creator#`Do›ßÎ#`Do›ßÎ
ÌÑB°=¼%r8X"1ÜCalibriàõÿ À
ÌÑ*+‚€Áƒ„&ffffffæ?'ffffffæ?(è?)è?¡"dXX333333Ó?333333Ó?U}$
þÿÕÍÕœ.“—+,ù®0¼HPX`hp
xŽä
WorksheetFeuilles de calculRoot Entryÿÿÿÿÿÿÿÿ
I hope you can help. Thanks
I am using PHPExcel in my project to generate a xls file of data coming from the database. I have downloaded the library of PHPExcel and using it in my PHP class as give below :
Class name : A.class.php
Path : inside a folder named "inc";
PHPExcel Lib folder : inside "inc" and relative to A.class.php
class A{
// code stuff
public function phpexcelgenerate()
{
require_once 'PHPExcel_1.7.9_doc/Classes/PHPExcel.php';
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="sam.xlsx"');
header('Cache-Control: max-age=0');
$objPHPExcel = new PHPExcel;
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->SetCellValue('A1', "12");
$writer = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
// This line will force the file to download
$writer->save();
}
// code stuff
}
This code is generating a xls file but that file is fully empty. I am thinking that I have included the files of PHPExcel in a wrong way in PHP classes. Could anyone tell me where I am doing wrong OR an example how can I do the same ?
You need to pass a filename to the save() method. Then read the contents of that files and echo to the browser.
$writer = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$writer->save("excel.xls");
readfile("excel.xls");
first of all you need to read the file:
// Read the file
$objReader = PHPExcel_IOFactory::createReader(Excel5);
$objPHPExcel = $objReader->load($fileName);
now get the data from db:
//get data
$details = $this->main_model->get_details()->row();
then assign the data to correct cells:
$objPHPExcel->getActiveSheet()->setCellValue('A'.$row, $i);
$objPHPExcel->getActiveSheet()->setCellValue('B'.$row, $rec->eType);
at the end write the new file:
header('Content-Type: application/vnd.ms-excel'); //mime type
header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
header('Cache-Control: max-age=0'); //no cache
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
//force user to download the Excel file without writing it to server's HD
$objWriter->save('php://output');