php excel xls 导入

<?php
header("Content-Type:text/html;charset=utf-8");
require_once 'reader.php';

$mysqli = new mysqli('localhost', 'root', '', 'c');
if ($mysqli->errno) {
die('Connect Error' . $mysqli->error);
}
$mysqli->set_charset('UTF8');
$data = new Spreadsheet_Excel_Reader();
$data->setOutputEncoding('utf-8');

$data->read('huojia.xls');

for ($i = 2; $i <= $data->sheets[0]['numRows']; $i++) {
$sql = "INSERT INTO cangku_huojia(ck_id,huojia_name) VALUES('" . $data->sheets[0]['cells'][$i][2] . "','" . $data->sheets[0]['cells'][$i][3] . "');";
$mysqli->query($sql);
//echo $sql.'<br>';
}

/**
CREATE TABLE `cangku_huojia` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`ck_id` INT(11) DEFAULT NULL,
`huojia_name` VARCHAR(35) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8
*/


1.下载压缩包
地址https://sourceforge.net/projects/phpexcelreader/

2.解压
找到Excel文件夹里面有两个文件
reader.php oleread.inc

3.修改reader.php文件的第31行
原:// require_once 'Spreadsheet/Excel/Reader/OLERead.php';
改成:require_once 'oleread.php';

4.将 oleread.inc 文件后缀名修改成php
oleread.php

5.注:下面的huojia.xls 文件跟执行php同目录

eg2:
<?php
header("Content-Type:text/html;charset=utf-8");
require_once 'Excel/reader.php';

$data = new Spreadsheet_Excel_Reader();
$data->setOutputEncoding('utf-8');
$data->read('huojia.xls');

for ($i = 2; $i <= $data->sheets[0]['numRows']; $i ++) {
for ($j = 1; $j <= $data->sheets[0]['numCols']; $j++) {
echo $data->sheets[0]['cells'][$i][$j].",";
}
echo "<br />";
}
?>
posted @ 2017-08-05 14:49  Curitis  阅读(596)  评论(0编辑  收藏  举报