p hp使用phpExcelReader导入excel的方法 phpExcelReader是专门用来读取文件的。返回一个数组,包含表的所有内容。 该 class 使用的方法可以参考网站下载回来的压缩档中的 example.php。 有几点要需要注意: 1、reader.php 中:将 require_once 'Spreadsheet/E
php使用phpExcelReader导入excel的方法
phpExcelReader是专门用来读取文件的。返回一个数组,包含表格的所有内容。
该 class 使用的方法可以参考网站下载回来的压缩档中的 example.php。
有几点要需要注意:
1、reader.php 中:将 require_once 'Spreadsheet/Excel/Reader/OLERead.php';改为 require_once 'oleread.inc';
2、example.php 中:修改 $data->setOutputEncoding('CP1251');为 $data->setOutputEncoding('CP936');或者是$data->setOutputEncoding('gbk');
3、修改 nl2br(htmlentities($data->sheets[$sheet]['cells'][$row][$col]));为 $table_output[$sheet] .= nl2br(htmlspecialchars($data->sheets[$sheet]['cells'][$row][$col]));
不然中文会有问题。
繁体的话可以修改为CP950、日文是CP932,具体可参考codepage说明。
===========================================================
require_once 'excel/reader.php';
$data = new Spreadsheet_Excel_Reader();
$data->setOutputEncoding('gbk'); //设置编码
$data->read('徐州.xls'); //指定文件
for ( $i = 1; $i sheets[0]['numRows']; $i++) {
for ($j = 1; $j sheets[0]['numCols']; $j++) {
echo $data->sheets[0]['cells'][$i][$j]." | ";
}
echo "
";
}
-------------------------------------------------------------------------------------------------------
如何通过PHP将excel的数据导入MySQL中
在开发PHP程序时,很多时候我们会有将数据批量导入数据库的需求,如学生信息批量添加到数据库中,而事先用excel编排好,对excel实行操作,便是我们比较常用的选择方式。
在对excel的操作中,phpExcelReade便是很多人的共同选择。在具体实现中,我们可以以文件上传方式将excel文件上传到服务器中的某个位置,通过以下操作将excel中的数据导入到数据库后,在将上传的文件删除即可。
代码如下:
$dir=dirname(__FILE__); //获取当前脚本的绝对路径
$dir=str_replace("\","/",$dir)."/";
$filename='uploadFile.xls'; //可以定义一个上传后的文件名称
$result=move_uploaded_file($_FILES['upload'] ['tmp_name'],$dir.$filename);//假如上传到当前目录下
if($result) //如果上传文件成功,就执行导入excel操作
{
require_once 'phpExcelReader/Excel/reader.php';
$data = new Spreadsheet_Excel_Reader();
$data->setOutputEncoding('utf-8');//设置在页面中输出的编码方式,而不是utf8
// 该方法会自动判断上传的文件格式,不符合要求会显示错误提示信息(错误提示信息在该方法内部)。
$data->read("$filename"); //读取上传到当前目录下名叫$filename的文件
error_reporting(E_ALL ^ E_NOTICE);
//如果excel表带标题,则从$i=2开始,去掉excel表中的标题部分(要将$i
for ($i = 2; $i sheets[0]['numRows']; $i++)
{
$sql = "INSERT INTO user (stuid,class,name,sex,classNum,tel,addr,remark) VALUES('".
$data->sheets[0]['cells'][$i][1]."','". //学号
$data->sheets[0] ['cells'][$i][2]."','". //班级
$data->sheets[0]['cells'] [$i][3]."','". //姓名
$data->sheets[0]['cells'][$i] [4]."','". //性别
$data->sheets[0]['cells'][$i] [5]."','". //班内序号
$data->sheets[0]['cells'][$i] [6]."','". //联系电话
$data->sheets[0]['cells'][$i] [7]."','". //联系地址
$data->sheets[0]['cells'][$i][8]."')"; //附注
$db->query($sql);
$insert_info.= " $sql
n"; //可以用来显示数据插入的信息
}
$totalNums=$data->sheets[0]['numRows']-2;//求出导入的总数据条数(这里是减去2,才会得到去除标题后的总数据)
//echo "导入成功!";
unlink("$filename"); //删除上传的excel文件
}
else
{
$errmsg="上传失败";
}
=======================================================================
zendframework上传文件
此方法适用于1.6或更高版本。
1.先在view页面写html代码。
涓

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

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version
Chinese version, very easy to use