search
Homephp教程php手册PHP将Excel导入数据库及数据库数据导出至Excel的方法

PHP将Excel导入数据库及数据库数据导出至Excel的方法

         本文实例讲述了PHP将Excel导入数据库及数据库数据导出至Excel的方法。分享给大家供大家参考。具体实现方法如下:

         一.导入

          导入需要使用能读取Excel的组件,网上也有比较好的组件,这里分享我使用的:下载 提取码:vxyn。(注意两个文件有引用关系)

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

//传入要导入的Excel的文件名

function import_to_DB($filename) {

require_once'reader.php';

$data = new Spreadsheet_Excel_Reader();

//创建读取Excel的对象

$data->setOutputEncoding('utf-8');

//设置读取Excel内容后输出的字符编码

$data->read("data/Excel/{$filename}.xls");

$db = mysql_connect('localhost', '用户名', '密码') or die("Could not connect to database.");

//连接数据库

mysql_query("set names 'uft8'");

//输出中文

mysql_select_db('数据库名');

//选择数据库

error_reporting(E_ALL ^ E_NOTICE);

for ($i = 1; $i sheets[0]['numRows']; $i++) {

echo $data->sheets[0]['cells'][$i][列数];

//这里可以把每一行相应列的值插到数据库中,如:

/*

$sql="insert "表名" values(对应项...)";

mysql_query($sql);

可加上错误判断

*/

}

?>

         总之,能够读出表格中每一行中的相应列$data->sheets[0][行][列]的值,插入操作就好办了。

          二.导出

         导出可以利用MIME协议轻松导出表格文件,不用依赖任何组件。按如下格式设置header即可导出Excel,同时浏览器进行下载

?

1

2

3

4

header('Content-type: text/html; charset=utf-8');

header("Content-type:application/vnd.ms-excel;charset=UTF-8"); //application/vnd.ms-excel指定输出Excel格式

header("Content-Disposition:filename=表格文件名.xls");

//输出的表格名称

        完整代码如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

header('Content-type: text/html; charset=utf-8');

header("Content-type:application/vnd.ms-excel;charset=UTF-8");

header("Content-Disposition:filename=表格文件名.xls");

$conn = mysql_connect("localhost","root","数据库密码") or die("不能连接数据库");

mysql_select_db("数据库名", $conn);

mysql_query("set names 'UTF-8'");

$sql="select * from 表名 where 条件";

$result=mysql_query($sql);

echo "表头1\t表头2\t表头3\n";

while($row=mysql_fetch_array($result)){

echo $row[0]."\t".$row[1]."\t".$row[2]."\n";

}

?>

        这里其实\t就是换格,\n就是换行。在一个网页中设置这个php文件的链接,当点击时浏览器会自动把传过来的流保存为Excel文件。

         希望本文所述对大家的php程序设计有所帮助。

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor