Summary of how to operate excel files in php_PHP tutorial

WBOY
Release: 2016-07-21 15:41:56
Original
1075 people have browsed it

1. PHP, without COM, generate excel file

Copy code The code is as follows:

< ?
header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:filename=test.xls");
echo "test1t";

echo "test1t";
echo "test2tn";
echo "test1t";
echo "test2tn";
echo "test1t";
echo " test2tn";
echo "test1t";
echo "test2tn";
echo "test1t";
echo "test2tn";
?>

Run the above code in the php environment, and you can see the browser asking the user whether to download the excel document, click Save, and there will be an extra excel file on the hard disk. Open it with excel and you will see the final result. How good is it? .
In fact, when making real applications, you can take the data out of the database, and then echo it out by adding t at the end of each column of data and n at the end of each row of data. Use header( at the beginning of php "Content-type:application/vnd.ms-excel"); indicates that the output is an excel file. Use header("Content-Disposition:filename=test.xls"); to indicate that the output file name is text.xls. That's OK.
We can also modify the header to allow it to output files in more formats, so that PHP will be more convenient in processing various types of files.
2. Use PHP to convert the mysql data table into excel file format
Copy the code The code is as follows:

$DB_Server = "localhost";
$DB_Username = "mydowns";
$DB_Password = "";
$DB_DBName = "mydowns";
$DB_TBLName = "user";
$Connect = @mysql_connect($DB_Server, $DB_Username, $DB_Password)
or die("Couldn@#t connect.");
$Db = @mysql_select_db( $DB_DBName, $Connect)
or die("Couldn@#t select database.");
$file_type = "vnd.ms-excel";
$file_ending = "xls";
header("Content-Type: application/$file_type");
header("Content-Disposition: attachment; filename=mydowns.$file_ending");
header("Pragma: no-cache");
header("Expires: 0");
$now_date = date(@#Y-m-d H:i@#);
$title = "Database name: $DB_DBName, data table: $DB_TBLName, backup date :$now_date";
$sql = "Select * from $DB_TBLName";
$ALT_Db = @mysql_select_db($DB_DBName, $Connect)
or die("Couldn@#t select database");
$result = @mysql_query($sql,$Connect)
or die(mysql_error());
echo("$titlen");
$sep = "t";
for ($i = 0; $i < mysql_num_fields($result); $i++) {
echo mysql_field_name($result,$i) . "t";
}
print("n" );
$i = 0;
while($row = mysql_fetch_row($result))
{
$schema_insert = "";
for($j=0; $j< mysql_num_fields($result);$j++)
{
if(!isset($row[$j]))
$schema_insert .= "NULL".$sep;
elseif ($row [$j] != "")
$schema_insert .= "$row[$j]".$sep;
else
$schema_insert .= "".$sep;
}
$schema_insert = str_replace($sep."$", "", $schema_insert);
$schema_insert .= "t";
print(trim($schema_insert));
print "n ";
$i++;
}
return (true);
?>

3. An example of PHP operating excel (using COM objects Generate excel)
This is for those who just like to simply process excel
Copy the code The code is as follows:

//Define an excel file
$workbook = "C:/My Documents/test.xls";
$sheet = "Sheet1";
/ /Generate a COM object $ex
$ex = new COM("Excel.sheet") or Die ("Cannot connect! ! ! ");
//Open an excel file
$book = $ex->application->Workbooks->Open($workbook) or Die ("Cannot open!!!");
$sheets = $book->Worksheets($sheet);
$sheets->activate;
//Get a cell
$cell = $sheets->Cells(5, 5);
$cell->activate;
//Assign value to the cell
$cell->value = 999;
//Save as another file newtest.xls
$ex->Application->ActiveWorkbook->SaveAs("newtest.xls");
//Close excel. If you want to see the effect, comment out the following two lines and let the user manually close excel.
$ex->Application->ActiveWorkbook->Close("False");
unset ($ex);
?>

4. PHP generates EXCEL stuff
Excel files can be generated through PHP.
----------------------------
Excel Functions
---------- ------------------
Save the following code as excel.php, then include it in the page
and then call
1. Call xlsBOF()
2. Write some content to xlswritenunber() or xlswritelabel().
3. Then call Call xlsEOF()
You can also use the fwrite function to write directly to the server instead of using echo just displayed on the browser.
Copy code The code is as follows:

// ----- begin of function library -----
// Excel begin of file header
function xlsBOF() {
echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
return ;
}
// Excel end of file footer
function xlsEOF() {
echo pack("ss", 0x0A, 0x00);
return;
}
// Function to write a Number (double) into Row, Col
function xlsWriteNumber($Row, $Col, $Value) {
echo pack("sssss", 0x203, 14, $Row, $Col, 0x0);
echo pack("d", $Value);
return;
}
// Function to write a label (text) into Row, Col
function xlsWriteLabel($ Row, $Col, $Value ) {
$L = strlen($Value);
echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L);
echo $Value;
return;
}
// ----- end of function library -----
?>
//
/ / To display the contents directly in a MIME compatible browser
// add the following lines on TOP of your PHP file:
header ("Expires: Mon, 26 Jul 1997 05:00 :00 GMT");
header ("Last-Modified: " . gmdate("D,d M YH:i:s") . " GMT");
header ("Cache-Control: no- cache, must-revalidate");
header ("Pragma: no-cache");
header (@#Content-type: application/x-msexcel@#);
header ("Content- Disposition: attachment; filename=EmplList.xls" );
header ("Content-Description: PHP/INTERBASE Generated Data" );
//
// the next lines demonstrate the generation of the Excel stream
//
xlsBOF(); // begin Excel stream
xlsWriteLabel(0,0,"This is a label"); // write a label in A1, use for dates too
xlsWriteNumber (0,1,9999); // write a number B1
xlsEOF(); // close the stream
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/321054.htmlTechArticle1. PHP, without COM, generate excel file copy code as follows: ? header("Content-type:application /vnd.ms-excel"); header("Content-Disposition:filename=test.xls"); echo "test1...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!