How to import and export Excel files with PHP_PHP tutorial
Let me introduce to you two examples of operating excel in PHP. One is to use PHP-ExcelReader to import excel and output it, and the other is to directly input excel and export it. Let's see the examples below.
With the help of the PHP-ExcelReader open source class, we can easily import Excel file data. The sample code is as follows:
PHP-ExcelReader download address: http://sourceforge.net/projects/phpexcelreader/
Example. Import Excel file
The code is as follows | Copy code | ||||||||
for ($i = 1; $i <= $data->sheets[0]['numRows']; $i++) { for ($j = 1; $j <= $data->sheets[0]['numCols']; $j++) {
|
The code is as follows | Copy code |
require_once 'reader.php'; // ExcelFile($filename, $encoding); $data = new Spreadsheet_Excel_Reader(); // Set output Encoding. $data->setOutputEncoding('gbk'); //"data.xls" refers to the excel file to be imported into mysql $data->read('data.xls'); @ $db = mysql_connect('localhost', 'root', '123456') or die("Could not connect to database.");//Connect to the database mysql_query("set names 'gbk'");//Output Chinese mysql_select_db('mydb'); //Select database error_reporting(E_ALL ^ E_NOTICE); for ($i = 1; $i <= $data->sheets[0]['numRows']; $i++) { //The following commented for loop prints excel table data /* for ($j = 1; $j <= $data->sheets[0]['numCols']; $j++) { echo """.$data->sheets[0]['cells'][$i][$j]."","; } echo "n"; //PHP open source code */ //The following code inserts excel table data [3 fields] into mysql. Rewrite the following code according to the number of fields in your excel table! $sql = "INSERT INTO test VALUES('". $data->sheets[0]['cells'][$i][1]."','". $data->sheets[0]['cells'][$i][2]."','". $data->sheets[0]['cells'][$i][3]."')"; echo $sql.' '; $res = mysql_query($sql); } ?> |
Example. Export excel file
For example, if I need a program to export Excel from PHP, I only need to export the relevant data to the Excel table. Such a simple operation does not require the use of those class libraries. Just use the header method directly: header("Content-type:application/vnd.ms-excel");
Look at the code:
The code is as follows
|
Copy code
|
||||
header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:attachment;filename=Export_test.xls");
|
$head="Number".$tab."Remarks".$br;
echo "string1";

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



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.

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

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

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

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

This chapter deals with the information about the authentication process available in CakePHP.
