I'm new with PHPEXCEL. My problem is PHPEXCEL(the code below) includes HTML in CSV output.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
The quick brown fox jumps over the lazy dog.
<?php
/** Include PHPExcel */
require_once dirname(__FILE__) . '/PHPExcel_1.8.0_doc/Classes/PHPExcel.php';
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
// Add some data
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', 'ID')
->setCellValue('B1', 'Name')
->setCellValue('C1', 'Description')
->setCellValue('D1', 'Type');
//ID
for($i=2; $i<=6; $i++){
$objPHPExcel->setActiveSheetIndex(0)->setCellValue('A'.$i, $i-1);
}
//Name
for($i=2; $i<=6; $i++){
$objPHPExcel->setActiveSheetIndex(0)->setCellValue('B'.$i, "Game" . ($i-1));
}
// Miscellaneous glyphs, UTF-8
/*$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A4', 'Miscellaneous glyphs')
->setCellValue('A5', 'éàèùâêîôûëïüÿäöüç');
*/
// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle('List of Games');
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);
// Redirect output to a client’s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="gcg_list.xls"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');
// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
?>
</body>
</html>
Output:
The quick brown fox jumps over the lazy dog. Џࡱက; ş+'ٰ#Hh 䄀Untitled SpreadsheetUnknown CreatorUnknown Creator#7Uҁ#7Uҁ ̇рB=%r8X"1܀Calibri�� �� �� �� �� �� �� �� �� �� �� �� �� �� �� ‸3f̀̿fff̙̙̀̌̿̿̿̀̿̀3f3̌̀̀fff3f3f33333f33333" List of GamesAAg恀 IDNameDescriptionTypeGame1Game2Game3Game4Game5 ̇р*+Dffffff濧ffffff濨迩迡"dXX333333ӿ333333ӿU}$ }$ }$ }$ # # # # >#ddgg Ս՜.+,0HPX`hp x䄀 WorksheetFeuilles de calculRoot Entry F7Uҁ7UҁSummaryInformation( F耀Workbook FDžDocumentSummaryInformation8 F쀀
If I make my code pure PHP(without HTML tags), PHPEXCEL will output correct CSV file.
Output:
ID Name Description Type
1 Game1
2 Game2
3 Game3
4 Game4
5 Game5
Can you help me know what's wrong with the code? Thanks
When you have HTML in your code, your webserver sends the HTML to your user. At this point, the webserver tells the browser accessing it what the format of the content it is sending is. (in your case, HTML, because that's at the top of your page)
You then output the PHPExcel file afterwards, but it's too late by this stage, because the user's browser has already been told that it's receiving content of the MIME type "text/html". There is no mechanism in place to tell a browser, in the middle of a request, that the content type has changed, and to start processing it differently.
I've created a sepate php file, called it and it works like charm. I want to make Mark Baker the answer however its a comment :)
I have problem. I created a web page with upload files, I have database (MySQL) with correctly URL to file (file path) (in database is only url to the file, file is in folder on the server). And now I trying write/search php script to download this file what was uploaded. All at web works well, but this download scripts don't work. I read and I think best way for me will be download with 'header', but I was trying and nothing. File is download to our disk, name is correctly, file extension ok, file is open in correct program, but if file have in name chars (';',':' etc.) file is download without these chars and is incorrectly (lack of extension, bad name). And second problem: all downloaded files are empty (0 b size), all is ok but they are empty, some tips ? Thanks in advance for help
My bad download codes:
<?php
$data=date('Y-m-d_H:i:s');
$nazwa=$_POST['downtitle'];
$urlek=$_POST['downurl'];
$extrozsz=$_POST['downext'];
$filePath = $urlek;
$fileName = $nazwa.$data.".".$extrozsz;
//------------------------------------------------------------------------
// $fd = fopen($filePath,"r");
// $size = filesize($filePath);
// $contents = fread($fd, filesize($filePath));
// fclose($fd);
// header("Content-Type: application/octet-stream");
// header("Content-Length: $size;");
// header("Content-Disposition: attachment; filename=$fileName");
// echo $contents;
//----------------------------------------------------------------------
// set_time_limit(0);
// while( $urlek = $response->fetch() )
// {
// $my_pliki = file_get_contents("$urlek");
// $my_file = fopen("$urlek","w+");
// fwrite($my_file,$my_pliki);
// fclose($my_file);
// }
//----------------------------------------------------------------------------
$quoted = $filePath;
$size = filesize($file);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $quoted);
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . $size);
?>
The filename parameter has some limits which are well described in RFC6266
So when you want use characters that are not present in the ISO-8859-1 character set then you need to use either filename* header or you need to strip/replace these characters from the filename. Nice solution to do so can be found here
Ok all is now ok, but another problem. I have code for showing news at website. All news are showing at same time (too long site). I wish to have at bottom of website numbers (at all websites under are this for forwarding on second subpage with older news). But how do it? Sup or sub is for small word, numbers etc, but all is retrieved form database:
$zapytanie='SELECT * FROM filetable WHERE Section="Other" ORDER BY ID DESC LIMIT 10';
This is in my php code query for showing latest 10 news, but if I add normally small numbers at bottom how construct code for showing next 10 news (no at where id="10-11" because if I will be have 1 thousand news or more it is too primitive) I must have automated code for showing pages with nextly 10 or 20 news how ?
Thanks, I edit my php at this:
<?php
$data=date('Y-m-d_H:i:s');
$nazwa=$_POST['downtitle'];
$urlek=$_POST['downurl'];
$extrozsz=$_POST['downext'];
$filePath = $urlek;
$fileName = $nazwa.$data.".".$extrozsz;
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=\"" . $fileName."\"");
header("Content-Description: File Transfer");
readfile($filePath);
?>
And all is now ok, but I have one question now: In MySQL database I have title, description, added date, premiere date and I wish to have search field. I done it in input type="search", but what script now use for searching in this specified tables in database, firstly open database this I know, but what later what command is for search in database ? I know about 'like' but some better? And after all return correct result and show all from database where this search word is ok Some tips? Sorry for weak english and thanks.
I have a big kml file I would just like to get certain parts of it.
<td>MUNICITY PSGC</td>
<td>153606000</td>
</tr>
<tr bgcolor="#D4E4F3">
<td>MUNICITY NAME</td>
<td>BUBONG</td>
<coordinates> 121.9116664538692,6.448888587819113,0 121.9119650206403,6.449515350125353,0 121.9119422292838,6.449720472334889,0 121.9122248421055,6.45000080602091,0 121.9122248421055,6.450554635986066,0 121.912635086524,6.450967159540255,0 121.9127809512066,6.451110745086737,0 121.9127809512066,6.4513887996373,0 121.9124983383845,6.451666854187351,0 121.9124983383845,6.451944908737914,0 121.9122248421055,6.452222963288534,0 121.9122248421055,6.45305484780431,0 121.9116664538692,6.453610956904925,0 121.9116664538692,6.454440562285072,0 121.9108345693535,6.455277005071878,0 121.9108345693535,6.455555059622498,0 121.9105519565314,6.455833114172549,0 121.9105519565314,6.456111168723112,0 121.9102784602525,6.45639150240919,0 121.9091571255082,6.45639150240919,0 121.9077759692987,6.455001229657285,0 121.9077759692987,6.454720895971263,0 121.9075001938841,6.454445120556159,0 121.9075001938841,6.453610956904925,0 121.9076301046167,6.452879354358174,0 121.9078283894188,6.452177380575449,0 121.9080266742212,6.451500477284355,0 121.9082249590239,6.450880552385172,0 121.9083320783998,6.450292535384961,0 121.9083639862988,6.449711355791806,0 121.9084323603685,6.44918715459022,0 121.9088904666361,6.448610533268493,0 121.909163962915,6.44833247871793,0 121.9097223511518,6.44833247871793,0 121.9099730560743,6.448090890338392,0 121.9116664538692,6.448888587819113,0</coordinates>
The end file should be (in an excel or csv file
Municity PSGC Municity Name Coordinates
153606000 BUBONG 121.9116664538692,6.448888587819113,0 121.9119650206403,6.449515350125353,0 121.9119422292838,6.449720472334889,0 121.9122248421055,6.45000080602091,0 121.9122248421055,6.450554635986066,0 121.912635086524,6.450967159540255,0 121.9127809512066,6.451110745086737,0 121.9127809512066,6.4513887996373,0 121.9124983383845,6.451666854187351,0 121.9124983383845,6.451944908737914,0 121.9122248421055,6.452222963288534,0 121.9122248421055,6.45305484780431,0 121.9116664538692,6.453610956904925,0 121.9116664538692,6.454440562285072,0 121.9108345693535,6.455277005071878,0 121.9108345693535,6.455555059622498,0 121.9105519565314,6.455833114172549,0 121.9105519565314,6.456111168723112,0 121.9102784602525,6.45639150240919,0 121.9091571255082,6.45639150240919,0 121.9077759692987,6.455001229657285,0 121.9077759692987,6.454720895971263,0 121.9075001938841,6.454445120556159,0 121.9075001938841,6.453610956904925,0 121.9076301046167,6.452879354358174,0 121.9078283894188,6.452177380575449,0 121.9080266742212,6.451500477284355,0 121.9082249590239,6.450880552385172,0 121.9083320783998,6.450292535384961,0 121.9083639862988,6.449711355791806,0 121.9084323603685,6.44918715459022,0 121.9088904666361,6.448610533268493,0 121.909163962915,6.44833247871793,0 121.9097223511518,6.44833247871793,0 121.9099730560743,6.448090890338392,0 121.9116664538692,6.448888587819113,0
Does anyone know of a PHP or Python Script to get this.
Thanks again guys.
I think you want to export to excel without HTML tags and show the result this way , so try this one :
<?php
header( "Content-type: application/vnd.ms-excel; charset=UTF-16LE" );
header("Content-Transfer-Encoding: binary");
header("Content-Disposition: attachment; filename='".date("Y-m-d")."'.xls");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
?>
<table><tr><td>MUNICITY PSGC</td><td>MUNICITY NAME</td><td>coordinates</td></tr>
<tr align=center><td valign=top>153606000</td><td valign=top>BUBONG </td><td valign=top>121.9116664538692,6.448888587819113,0 121.9119650206403,6.449515350125353,0 121.9119422292838,6.449720472334889,0 121.9122248421055,6.45000080602091,0 121.9122248421055,6.450554635986066,0 121.912635086524,6.450967159540255,0 121.9127809512066,6.451110745086737,0 121.9127809512066,6.4513887996373,0 121.9124983383845,6.451666854187351,0 121.9124983383845,6.451944908737914,0 121.9122248421055,6.452222963288534,0 121.9122248421055,6.45305484780431,0 121.9116664538692,6.453610956904925,0 121.9116664538692,6.454440562285072,0 121.9108345693535,6.455277005071878,0 121.9108345693535,6.455555059622498,0 121.9105519565314,6.455833114172549,0 121.9105519565314,6.456111168723112,0 121.9102784602525,6.45639150240919,0 121.9091571255082,6.45639150240919,0 121.9077759692987,6.455001229657285,0 121.9077759692987,6.454720895971263,0 121.9075001938841,6.454445120556159,0 121.9075001938841,6.453610956904925,0 121.9076301046167,6.452879354358174,0 121.9078283894188,6.452177380575449,0 121.9080266742212,6.451500477284355,0 121.9082249590239,6.450880552385172,0 121.9083320783998,6.450292535384961,0 121.9083639862988,6.449711355791806,0 121.9084323603685,6.44918715459022,0 121.9088904666361,6.448610533268493,0 121.909163962915,6.44833247871793,0 121.9097223511518,6.44833247871793,0 121.9099730560743,6.448090890338392,0 121.9116664538692,6.448888587819113,0</td></tr><table>
I've a php page with some forms used to upload images. Each form upload one image. Uploaded images replace images yet loaded in the page.
Here is a snippet:
<?php
// NO CACHING CODE
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
// FORM SUBMIT CODE
if (isset($_POST['submit_img1'])) {
// used for some photo manipulation and checks
require_once('WideImage/WideImage.php');
//... some code ...
unlink("path/" . $img1);
$img_name = "path/" . $img1;
$wideImage->saveToFile($img_name, 80);
// ... some code ...
}
// PRINT IMAGE IN THE PAGE
echo '<p><img src="path/' . $img1 . '" width="100" height="100" class="prof_img" /></p>';
?>
//FORM FOR CHANGE THE IMAGE
<form id="form_img1" name="form_img1" method="post" onSubmit="return checkFormImg1()"
action="" enctype="multipart/form-data">
<input class="prof_input" type="file" name="img1" id="img1" />
<input name="submit_img1" type="submit" class="prof_submit" value="Inserisci Immagine 1"
/>
</form>
The problem is that on reload (after submit button is pressed) the old images are displayed. But the new images have been properly uploaded replacing the old ones.
Excuse my english.
theres an easy little trick you can use for this. just append a random string to the end of the image file source like a $_GET parameter.
better than a random string is a timestamp, since your won't risk using the same number twice.
use it like this in your img tag
src='img.jpg?rand=1234'
Your NO CACHING CODE affects only the page where it called.
So, you have to take care of the images, not the page with links.