/**
- * Export data to Excel to solve garbled problems
- * Edit bbs.it-home.org
- */
- function Xlsbof () {
- Echo Pack ("Ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
- Return
- }
function xlseof () {
- Echo Pack ("ss", 0x0A, 0x00);
- Return
- }
function Xlswritenumber ($Row, $Col, $Value) {
- Echo Pack ("Sssss", 0x203, $Row, $Col, 0x0);
- Echo Pack ("D", $Value);
- Return
- }
function Xlswritelabel ($Row, $Col, $Value) {
- $Value = Iconv ("UTF-8", "gb2312", $Value); Add this statement to resolve the export Excel file garbled problem 20110629
- $L = strlen ($Value);
- Echo Pack ("Ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L);
- Echo $Value;
- Return
- }
Include "connection.php";
- $sql = "Select Ledger_name,ledger_sex, Ledger_age, Ledger_addfrom ps_ledger_11";
- $query = mysql_query ($sql);
File header
- 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/octet-stream");
- Header ("Content-type:application/download");
- Header ("Content-type:application/vnd.ms-excel; Charset=utf-8 ");
- Header ("Content-disposition:attachment;filename= Police Office Auxiliary Police statistics. xls");
- Header ("Content-disposition:inline;filename=\". $filename. ". Xls\" ");
- Iconv ("Utf-8", "gb2312", $filename);//Troubleshoot file-caused garbled ". Xls\" ");
- Header ("Content-transfer-encoding:binary");
Adding data to a table
- Xlsbof ();
- Xlswritelabel (1, 0, "column name");
- Xlswritelabel (1, 1, "column name");
- Xlswritelabel (1, 2, "column name");
- Xlswritelabel (1, 3, "column name");
- Xlswritelabel (1, 4, "column name");
- $xlsRow = 1;
- while ($array = Mysql_fetch_array ($query)) {
- + + $i;
- Xlswritenumber ($xlsRow, 0, "$i");
- Xlswritenumber ($xlsRow, 0, "$array [0]");
- Xlswritelabel ($xlsRow, 1, "$array [1]");
- Xlswritelabel ($xlsRow, 2, "$array [2]");
- Xlswritelabel ($xlsRow, 3, "$array [3]");
- Xlswritelabel ($xlsRow, 4, "$array [4]");
- $xlsRow + +;
- }
- Xlseof ();
- Exit ();
- Test can be used, the column name is not displayed, to be resolved
- ?>
Copy Code |