在网上找了excel读取的一些资料,个人觉得PHPExcel这还是挺好用的,相对比较全的工具。

  主要功能是读取上传的excel文件,插入或更新到数据库。

  iconv("gbk","utf8",$_FILES["file"]["tmp_name"]),mysql_query("SET NAMES 'utf8'"),编码转换防止中文在数据库中显示乱码;

以下是主要的源代码:

 header("Content-type:text/html;charset=utf-8");
require_once('PHPExcel/Classes/PHPExcel.php');
require_once('PHPExcel/Classes/PHPExcel/IOFactory.php'); iconv("gbk","utf8",$_FILES["file"]["tmp_name"]);//编码转换 $fileType=PHPExcel_IOFactory::identify($_FILES["file"]["tmp_name"]);//判断文件的类型
$objReader=PHPExcel_IOFactory::createReader($fileType);//创建对象
$objPHPExcel=$objReader->load($_FILES['file']['tmp_name']);//导入数据 $currentSheet = $objPHPExcel->getSheet(0); //第一个工作簿
$allRow = $currentSheet->getHighestRow(); //行数
$letters_arr = array(1=>'A',2=>'B',3=>'C',4=>'D',5=>'E',6=>'F',7=>'G',8=>'H',9=>'I',10=>'J',11=>'K',12=>'L',13=>'M', 14=>'N',15=>'O',16=>'P',17=>'Q',18=>'R',19=>'S',20=>'T',21=>'U',22=>'V',23=>'W',24=>'X',25=>'Y',26=>'Z'); $con=mysql_connect('localhost','root','root')
or die('数据库链接失败!<br />'.mysql_error()); $db=mysql_select_db('db_product',$con);
mysql_query("SET NAMES 'utf8'");
for($currentRow = 3;$currentRow<=$allRow;$currentRow++){
$rowValues='';
$id=(string)($currentSheet->getCell('C'.$currentRow)->getValue()); $select_id=mysql_query("SELECT * FROM t_shop WHERE productNum='$id'");
$result=mysql_fetch_array($select_id); if(!$result){//判断是否存在
foreach ($letters_arr as $key => $value) {
$xh=$currentSheet->getCell($value.$currentRow)->getValue();
if($key>14)break;
$rowValues=$rowValues.($key>1?',':'').'\''.$xh.'\''; } $sql="INSERT INTO t_shop (shopName,brand,productNum,spec,barCode,category,productName,orderNumber,deliveryNumber,returnNumber,returnBackNumber,salesPrice,costPrice,profit) VALUES (".$rowValues.")";
mysql_query($sql);
}
else{ $reValues=re_row($result,$letters_arr,$currentSheet,$currentRow);
$sql="UPDATE t_shop SET shopName='$reValues[0]',brand='$reValues[1]',spec='$reValues[2]',barCode='$reValues[3]',category='$reValues[4]',productName='$reValues[5]',orderNumber=$reValues[6],deliveryNumber=$reValues[7],returnNumber=$reValues[8],returnBackNumber=$reValues[9],salesPrice=$reValues[10],costPrice=$reValues[11],profit=$reValues[12] WHERE productNum='$id'";
mysql_query($sql);
echo mysql_error().$id.'###';
print_r($reValues);
echo '<br />';
} } mysql_close($con); function re_row($s,$a,$obj,$currentRow){
$b=$d='';
foreach ($a as $key => $value) {
$xh=$obj->getCell($value.$currentRow)->getValue();
if($key>14)break;
$d[]=$xh; } foreach ($d as $key => $value) {
if($key!=2 && $key<7){
$b[]= (string)$value;
}
elseif($key>6){
$b[]=$s[$key]+$value;
} }
return $b;
}

  

 
 

PHPExcel——读取excel的更多相关文章

  1. PHPExcel读取excel文件示例

    PHPExcel读取excel文件示例PHPExcel最新版官方下载网址:http://phpexcel.codeplex.com/PHPExcel是一个非常方便生成Excel格式文件的类,官方下载包 ...

  2. (实用篇)PHPExcel读取Excel文件的实现代码

    用PHPExcel读取Excel 2007 或者Excel2003文件,需要的朋友,可以参考下. 涉及知识点:  php对excel文件进行循环读取 php对字符进行ascii编码转化,将字符转为十进 ...

  3. 利用PHPExcel读取Excel的数据和导出数据到Excel

    PHPExcel是一个PHP类库,用来帮助我们简单.高效实现从Excel读取Excel的数据和导出数据到Excel.也是我们日常开发中,经常会遇到的使用场景.比如有个客户信息表,要批量导出发给同事,我 ...

  4. PHPExcel读取Excel文件的实现代码

    <?php require_once 'PHPExcel.php'; /**对excel里的日期进行格式转化*/ function GetData($val){ $jd = GregorianT ...

  5. PHPExcel读取excel的多个sheet存入数据库

    //批量导入文章 excel读取 public function importdata ( $filename, $tmp_name ) { //设置超时时间 set_time_limit(0); $ ...

  6. PHP使用phpexcel读取excel文件

    PHP读取excel文件 require("Classes/PHPExcel.php"); require("Classes/PHPExcel/IOFactory.php ...

  7. thinkphp用phpexcel读取excel,并修改列中的值,再导出excel,带往excel里写入图片

    <?php class GetpriceAction extends AdministratorAction { // 文件保存路径 protected $savepath; // 允许上传的文 ...

  8. 利用PHPExcel读取excel文件

    $filePath = "7788.xls"; $PHPExcel = new PHPExcel(); $PHPReader = new PHPExcel_Reader_Excel ...

  9. ThinkPHP3.2.3 PHPExcel读取excel插入数据库

    版本 ThinkPHP3.2.3 下载PHPExcel 将这两个文件放到并更改名字 excel文件: 数据库表: CREATE TABLE `sh_name` ( `name` varchar(255 ...

  10. php phpexcel 读取excel文件数据

    public function readExcel(){ $allPath = '/home/examine\video/list.xls'; \think\Loader::import('exten ...

随机推荐

  1. R语言读取本地文件注意事项

    R里面应该用/,而不是\ ,或者用两个\\   R区分大小写,所以应该用C:,而不是c:

  2. 广播接收者Receiver

    一,动态创建网络状态监控 思路: 1:需要注册一个广播接收者,registerReceiver()需要两个参数 public Intent registerReceiver( BroadcastRec ...

  3. Redis学习笔记7--Redis管道(pipeline)

    redis是一个cs模式的tcp server,使用和http类似的请求响应协议.一个client可以通过一个socket连接发起多个请求命令.每个请求命令发出后client通常会阻塞并等待redis ...

  4. 网址前面的icon

    shortcut icon和icon代码之间究竟有何区别呢.下面介绍一下   语句一:<link rel="shortcut icon" href="favicon ...

  5. python嵌套函数、闭包与decorator

    1 一段代码的执行结果不光取决与代码中的符号,更多地是取决于代码中符号的意义,而运行时的意义是由名字空间决定的.名字空间是在运行时由python虚拟机动态维护的,但是有时候我们希望能将名字空间静态化. ...

  6. C#夯实基础系列之const与readonly

    一.const与readonly的争议       你一定写过const,也一定用过readonly,但说起两者的区别,并说出何时用const,何时用readonly,你是否能清晰有条理地说出个一二三 ...

  7. 【MySQL】常规操作

    2016.4.10 1.MySQL查看系统当前默认自增列种子值和步长值(全局) 1 show GLOBAL VARIABLES like 'auto_incre%'; 2.MySQL查看具体某一张表的 ...

  8. python——连接Oracle数据库

    前言 Python自带的模块中有很多操纵文件的.我们可以把文件的数据读出来,经过处理还可以将数据写入文件中.但是对于数据的管理和分析来说,数据库还是专业一些.如果Python能和数据库结合在一起,那么 ...

  9. js框架设计1.2对象扩展笔记

    需要一个新的功能添加到我们的命名空间上.这方法在JS中被叫做extend或者mixin,若是遍历属性用一下1.1代码,则会遍历不出原型方法,所以1.2介绍的是mass Framework里的mix方法 ...

  10. windows7 启用管理员账户

    在虚拟机中安装了windows7,方便使用qq,使用百度云,office等常用的软件.虚拟机使用的oracle的VirtualBox,个人使用体验不错,最棒的功能是能将物理机中的磁盘映射到虚拟机中网络 ...