PHP two ways to export excel

Source: Internet
Author: User
Tags explode jquery library

What you need: jquery library, Phpexcel plugin, page export Excel effect test file Explode.php,excel Export function implement file exp.php and explode_excel.php, file related content under this article:

explode.php: Test page content: After you visit this page, click Explode1 and Explode2 Excel, which can export two different effects, with the following code:

<! DOCTYPE HTML Public '-//w3c//dtd XHTML 1.0 transitional//en ' Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ' >
<meta http-equiv= ' content-type ' content= ' text/html; Charset=utf-8 '/>
<script src= ' http://www1.qixoo.com/js/jquery.js ' ></script>
<title></title>
<style>
td{
Text-align:center;
font-size:12px;
Font-family:arial, Helvetica, Sans-serif;
border: #1C7A80 1px solid;
Color: #152122;
width:100px;
}
table,tr{
Border-style:none;
}
. title{
Background: #7DDCF0;
Color: #FFFFFF;
Font-weight:bold;
}
</style>
<body>
<script>
$ (document). Ready (function () {
$ (' #explode1 '). Click (function () {
window.location.href= ' exp.php ';
});

$ (' #explode2 '). Click (function () {
window.location.href= ' explode_excel.php ';
});
})
</script>
<table width= "border=" 1 ">
<tr>
&LT;TD class= ' title ' >Date</td>
&LT;TD class= ' title ' colspan= "5" style= ' Width:500px;text-align:center; ' >csat score</td>
&LT;TD class= ' title ' >grand total</td>
&LT;TD class= ' title ' >CSAT</td>
</tr>
<tr>
<td>08/01/11</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0%</td>
</tr>
<tr>
<td>08/01/11</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0%</td>
</tr>
<tr>
<td>08/01/11</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0%</td>
</tr>
</table>
<br/>
<input type= ' button ' id= ' explode1 ' value= ' Explode ' style= ' margin-left:620px;background-color: #10899E; color: White;padding:3px;font-weight:bold; ' ><input type= ' button ' id= ' explode2 ' value= ' Explode2 ' style= ' background-color: #10899E; color:white;padding:3px ; font-weight:bold;margin-left:24px; ' >
</body>
First, use the Phpexcel plugin to export the contents of Excel, the exp.php file:

<?php
Header ("Content-type:text/html;charset=utf-8");
/** Error Reporting */
Error_reporting (E_all);
/** Phpexcel */
Include_once ' phpexcel.php ';

/** phpexcel_writer_excel2003 for creating XLS files */
Include_once ' phpexcel/writer/excel5.php ';

Create New Phpexcel Object
$objPHPExcel = new Phpexcel ();

Set Properties
$objPHPExcel->getproperties ()->setcreator ("Li Han Regiment");
$objPHPExcel->getproperties ()->setlastmodifiedby ("Li Han Regiment");
$objPHPExcel->getproperties ()->settitle ("Office" XLSX Test Document ");
$objPHPExcel->getproperties ()->setsubject ("Office" XLSX Test Document ");
$objPHPExcel->getproperties ()->setdescription ("Test document for Office-XLSX, generated using PHP classes.");

ADD some data
$objPHPExcel->setactivesheetindex (0);
$objPHPExcel->getactivesheet ()->setcellvalue (' A1 ', ' Date ');
Merge cells:
$objPHPExcel->getactivesheet ()->mergecells (' b1:f1 ');
$objPHPExcel->getactivesheet ()->setcellvalue (' B1 ', ' CSAT score ');
$objPHPExcel->getactivesheet ()->setcellvalue (' G1 ', ' Grand total ');
$objPHPExcel->getactivesheet ()->setcellvalue (' H1 ', ' CSAT ');
$objPHPExcel->getactivesheet ()->setcellvalue (' A2 ', ' 08/01/11 ');
$objPHPExcel->getactivesheet ()->setcellvalue (' B2 ', ' 0 ');
$objPHPExcel->getactivesheet ()->setcellvalue (' C2 ', ' 0 ');
$objPHPExcel->getactivesheet ()->setcellvalue (' D2 ', ' 0 ');
$objPHPExcel->getactivesheet ()->setcellvalue (' E2 ', ' 0 ');
$objPHPExcel->getactivesheet ()->setcellvalue (' F2 ', ' 0 ');
$objPHPExcel->getactivesheet ()->setcellvalue (' G2 ', ' 0 ');
$objPHPExcel->getactivesheet ()->setcellvalue (' H2 ', ' 0% ');
$objPHPExcel->getactivesheet ()->setcellvalue (' A3 ', ' 08/01/11 ');
$objPHPExcel->getactivesheet ()->setcellvalue (' B3 ', ' 0 ');
$objPHPExcel->getactivesheet ()->setcellvalue (' C3 ', ' 0 ');
$objPHPExcel->getactivesheet ()->setcellvalue (' D3 ', ' 0 ');
$objPHPExcel->getactivesheet ()->setcellvalue (' E3 ', ' 0 ');
$objPHPExcel->getactivesheet ()->setcellvalue (' F3 ', ' 0 ');
$objPHPExcel->getactivesheet ()->setcellvalue (' G3 ', ' 0 ');
$objPHPExcel->getactivesheet ()->setcellvalue (' H3 ', ' 0% ');
$objPHPExcel->getactivesheet ()->setcellvalue (' A4 ', ' 08/01/11 ');
$objPHPExcel->getactivesheet ()->setcellvalue (' B4 ', ' 0 ');
$objPHPExcel->getactivesheet ()->setcellvalue (' C4 ', ' 0 ');
$objPHPExcel->getactivesheet ()->setcellvalue (' D4 ', ' 0 ');
$objPHPExcel->getactivesheet ()->setcellvalue (' E4 ', ' 0 ');
$objPHPExcel->getactivesheet ()->setcellvalue (' F4 ', ' 0 ');
$objPHPExcel->getactivesheet ()->setcellvalue (' G4 ', ' 0 ');
$objPHPExcel->getactivesheet ()->setcellvalue (' H4 ', ' 0% ');

Rename Sheet
$objPHPExcel->getactivesheet ()->settitle (' Csat ');


Save Excel File
$objWriter = new phpexcel_writer_excel2007 ($objPHPExcel);

$objWriter = new Phpexcel_writer_excel5 ($objPHPExcel);
$objWriter->save (Str_replace ('. php ', '. xls ', __file__));
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-type:application/vnd.ms-execl");
Header ("Content-type:application/octet-stream");
Header ("Content-type:application/download");
Header ("Content-disposition:attachment;filename=csat.xls");
Header ("Content-transfer-encoding:binary");
$objWriter->save ("Php://output");
?>
Second, directly using the header to export HTML, specify the style of Excel, that is, the contents of explode_excel.php, as follows:

<?php
Header ("Content-type:application/vnd.ms-excel");
Header ("Content-disposition:filename=csat.xls");
echo "<! DOCTYPE HTML Public '-//w3c//dtd XHTML 1.0 transitional//en ' Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ' >
<meta http-equiv= ' content-type ' content= ' text/html; Charset=utf-8 '/>
<title> Untitled Document </title>
<style>
td{
Text-align:center;
font-size:12px;
Font-family:arial, Helvetica, Sans-serif;
border: #1C7A80 1px solid;
Color: #152122;
width:100px;
}
table,tr{
Border-style:none;
}
. title{
Background: #7DDCF0;
Color: #FFFFFF;
Font-weight:bold;
}
</style>

<body>
<table width= ' border= ' 1 ' >
<tr>
&LT;TD class= ' title ' >Date</td>
&LT;TD class= ' title ' colspan= ' 5 ' style= ' width:500px;text-align:center; ' >csat score</td>
&LT;TD class= ' title ' >grand total</td>
&LT;TD class= ' title ' >CSAT</td>
</tr>
<tr>
<td>08/01/11</td>
<td>0</td>
<td>0</td>
<td> Test content!! </td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0%</td>
</tr>
<tr>
<td>08/01/11</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0%</td>
</tr>
<tr>
<td>08/01/11</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0%</td>
</tr>
</table>
</body>
";
?>

PHP two ways to export excel

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.