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.
Related
I want to download a word file from an existing template using PHPWord.
I tried this code but it generate corrupted file:
$file = ROOT . '/web/templates/recu.docx';
$PHPWord = new \PhpOffice\PhpWord\TemplateProcessor($file);
$PHPWord->setValue('Value1', date('d/m/Y'));
$PHPWord->setValue('Value2', "ffff");
$PHPWord->setValue('Value3', "sabrine");
$PHPWord->setValue('Value4', utf8_decode("555555"));
$PHPWord->setValue('Value5', "200000");
$new_file = "recu_".date('d_m_Y_H_i');
$PHPWord->saveAs(ROOT. '/web/uploads/recu/'.$new_file.'.docx');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment;filename="'.$new_file.'.docx"');
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($PHPWord,
'Word2007');
ob_clean();
$objWriter->save('php://output');
exit;
any suggestions why my code didn't work ?
Try this content header instead:
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
Since I dont want pdf to be stored in a folder instead PDF Out put need to be stored in db like below.
PDF is created using html2pdf.class.php
/////////////////////////////////////////////////////////////////////
$html2pdf = new HTML2PDF('P', 'A4', 'en');
$html2pdf->pdf->SetDisplayMode('fullpage');
$html2pdf->writeHTML($pdf_invoice_content, isset($_GET['vuehtml']));
$pdfdata = $html2pdf->Output('document.pdf', 'S');
////////////////////////////////////////////////////////////////////
By fetching the data from db, I want to create pdf. So I did like this but the pdf did not open.
//////////////////////////////////////////////////////////
$file_size = sizeof($pdfdata); // data fetch from db
$file_name = 'test1.pdf';
header("Content-length: $file_size");
header("Content-type: application/pdf");
header("Content-Disposition: attachment; filename=$file_name");
echo $pdfdata;
///////////////////////////////////////////////////////////
Any one have a solution for this.
I have stored the pdf data in db as blob and show the pdf like below.
$pdfdata = $pdf_invoice_content;
$file_name ='test1.pdf';
if($pdfdata){
header('Content-type: application/pdf;base64');
header('Content-Disposition: inline; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
echo $pdfdata;
}
This is working for me.
I am trying to create a xls file in php. And I am making a string in html format lets say <table><tr><td>Cell 1</td><td>Cell 2</td></tr></table> and want to print in xls file as it is. but My problem is that when I try to print in xls file its getting render. but I want raw html string without rendered in xls file.
Here is my code:-
<?php
$file="demo.xls";
$test="<table><tr><td>Cell 1</td><td>Cell 2</td></tr></table>";
header('Content-type: application/excel');
header("Content-Disposition: attachment; filename=$file");
echo $test;
?>php
You can also use a service called DocRaptor which does html to excel conversion. http://docraptor.com/ , Multiple language supported
Or You can use the below code to download the file in Excel but first you have to download the PHPExcel class files and include like below .
require_once "excel/Classes/PHPExcel.php";
$objTpl = new PHPExcel();
$l=0;
$row_num = 1;
$objTpl->getActiveSheet()->getDefaultStyle()->getFont()->setName('Calibri');
$objTpl->getActiveSheet()->getDefaultStyle()->getFont()->setSize(10);
$objTpl->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
$objTpl->getActiveSheet()->duplicateStyle($objTpl->getActiveSheet()->getStyle('A1'), 'B1:Z1');
$objTpl->getActiveSheet()->getStyle('A1:Z1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
// you can use loop over here remember to set $row_num = 2 if using loop and increment it inside $row_num++
$objTpl->getActiveSheet()->setCellValueByColumnAndRow($l++,$row_num, 'Cell 1');
$objTpl->getActiveSheet()->setCellValueByColumnAndRow($l++,$row_num, 'Cell 2');
$filename='some_file.xls'; //just some random filename
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$filename.'"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objTpl, 'Excel5'); //downloadable file is in Excel 2003 format (.xls)
$objWriter->save('php://output'); //send it to user, of course you can save it to disk also!
exit; //done.. exiting!
There is a way by either writing html in file or comma separated text
With HTML
$xlsx_file = $pathinfo['dirname']."/".$pathinfo['filename'].".xlsx" ;
$fh = fopen($xlsx_file, 'w+');<br>
fwrite($fh, $table); // your table <br>
fclose($fh);<br>
with array or string of csv
$data = '"Name"t"City"t"Country"' . "n";<br>
$data .= '"Ashok"t"New Delhi"t"India"' . "n";<br>
$data .= '"Krishna"t"Bangalore"t"India"' . "n";<br>
$f = fopen('data.xls' , 'wb');<br>
fwrite($f , $data );<br>
fclose($f);<br>
this is temporary solution but if you want full set of options you go for PHP Excel as suggested by others
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);
?>
I am trying to download excel file using PHPExcel, it's like it download the excel file nicely but the data in excel file is all crap.. it's not what i expected. I passed very basic methods to test my excel sheet data output.
here is the code i'am trying
else if($request->p['type'] == 'excel')
{
$report_type_name = "Graph Survey Report";
$ExcelReport = new ExcelApExport($sections, $group_definition, $questions, $sample_corrections);
$objPHPExcel = $ExcelReport->export($sets, $disp_filter);
header('Content-Type: application/vnd.ms-excel');
header("Content-Disposition: attachment; "
. escape_for_content_disposition("{$report_name} - {$report_type_name} - " . date("Y-m-d.H.i") . ".xls"));
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;
}
also I make object here and call phpexcel methods here
public function export($Sets, $disp_filter)
{
$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()->setTitle("Offic excel Test Document");
$objPHPExcel->getProperties()->setSubject(" Test Document");
$objPHPExcel->getProperties()->setDescription("Test document for XLS, generated using PHP classes.");
//echo date('H:i:s') . " Add some data\n";
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->SetCellValue('A1', 'Hello');
$objPHPExcel->getActiveSheet()->SetCellValue('B2', 'world!');
$objPHPExcel->getActiveSheet()->SetCellValue('C1', 'Hello');
$objPHPExcel->getActiveSheet()->SetCellValue('D2', 'world!');
return $objPHPExcel;
}
please can you suggest me why this crap data is showing up in my file instead of expected data.
thanks in advance
It needs basically
ob_clean();
before saving object.
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
This is the writer for .xlsx files. (See: https://github.com/PHPOffice/PHPExcel/blob/develop/Examples/01simple-download-xlsx.php)
So the right header would be:
// Redirect output to a client’s web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header("Content-Disposition: attachment; "
. escape_for_content_disposition("{$report_name} - {$report_type_name} - " . date("Y-m-d.H.i") . ".xlsx")); //.xlsX!
The writer for old .xls-files is:
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
See: https://github.com/PHPOffice/PHPExcel/blob/develop/Examples/01simple-download-xls.php
I ran into this problem recently and found another post here that is very helpful if ur still having the problem after using ob_clean()
check this post