Summary of how to operate excel files in php_PHP tutorial
1. PHP, without COM, generate excel file
< ?
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
$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
//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.
// ----- 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
?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
