用php把excel数据导入数据库
PHPExcel是一个PHP类库,用来帮助我们简单、高效实现从Excel读取Excel的数据和导出数据到Excel。
先下载PHPExcel类库·
读取文件源码:
<?php
header("content-type:text/html;charset=utf8");
include './Classes/PHPExcel/IOFactory.php';//引入PHPExcel类
$inputFileName = './test.xls';//读取的excel文件
date_default_timezone_set('PRC');
// 读取excel文件
try {
$inputFileType = PHPExcel_IOFactory::identify($inputFileName);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($inputFileName);
} catch(Exception $e) {
die('加载文件发生错误:"'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
}
$sheet = $objPHPExcel->getSheet(0);
$data=$sheet->toArray();//该方法读取不到图片 图片需单独处理
$imageFilePath='./images/'.date('Y-m-d').'/';//图片在本地存储的路径
if (! file_exists ( $imageFilePath )) {
mkdir("$imageFilePath", 0777, true);
}
//处理图片
foreach($sheet->getDrawingCollection() as $img) {
list($startColumn,$startRow)= PHPExcel_Cell::coordinateFromString($img->getCoordinates());//获取图片所在行和列
$imageFileName = $img->getCoordinates() . mt_rand(100, 999);
switch($img->getMimeType()) {
case 'image/jpg':
$imageFileName.='.jpg';
imagejpeg($img->getImageResource(),$imageFilePath.$imageFileName);
break;
case 'image/gif':
$imageFileName.='.gif';
imagegif($img->getImageResource(),$imageFilePath.$imageFileName);
break;
case 'image/png':
$imageFileName.='.png';
imagepng($img->getImageResource(),$imageFilePath.$imageFileName);
break;
}
$startColumn = ABC2decimal($startColumn);//由于图片所在位置的列号为字母,转化为数字
$data[$startRow-1][$startColumn]=$imageFilePath.$imageFileName;//把图片插入到数组中
}
print_r($data);die;
function ABC2decimal($abc){
$ten = 0;
$len = strlen($abc);
for($i=1;$i<=$len;$i++){
$char = substr($abc,0-$i,1);//反向获取单个字符
$int = ord($char);
$ten += ($int-65)*pow(26,$i-1);
}
return $ten;
}
以上代码只是处理图片,得到图片路径插入到数组中,如需数据入库,可循环insert,自行处理,打印结果如下:

用php把excel数据导入数据库的更多相关文章
- 使用python将excel数据导入数据库
使用python将excel数据导入数据库 因为需要对数据处理,将excel数据导入到数据库,记录一下过程. 使用到的库:xlrd 和 pymysql (如果需要写到excel可以使用xlwt) 直接 ...
- [Asp.net]常见数据导入Excel,Excel数据导入数据库解决方案,总有一款适合你!
引言 项目中常用到将数据导入Excel,将Excel中的数据导入数据库的功能,曾经也查找过相关的内容,将曾经用过的方案总结一下. 方案一 NPOI NPOI 是 POI 项目的 .NET 版本.POI ...
- 如何把excel数据导入数据库
这里介绍2种把excel数据导入oracle数据库的方法. 1. 在excel中生成sql语句. 1)在数据列的右侧,第一行的任何位置输入="insert into table(xx,yyy ...
- C#将Excel数据导入数据库(MySQL或Sql Server)
最近一直很忙,很久没写博客了.今天给大家讲解一下如何用C#将Excel数据导入Excel,同时在文章最后附上如何用sqlserver和mysql工具导入数据. 导入过程大致分为两步: 1.将excel ...
- Excel数据导入数据库
maven依赖 <!--excel相关依赖--> <dependency> <groupId>org.apache.poi</groupId> < ...
- 转:[Asp.net]常见数据导入Excel,Excel数据导入数据库解决方案,总有一款适合你!
引言 项目中常用到将数据导入Excel,将Excel中的数据导入数据库的功能,曾经也查找过相关的内容,将曾经用过的方案总结一下. 方案一 NPOI NPOI 是 POI 项目的 .NET 版本.POI ...
- PHPExcel将Excel数据导入数据库
<?php //PHPExcel读取导入Excel数据到数据库(2003,2007通用)使用方法: //先用excel2array()方法将excel表中的数据存储到数组,在从遍历二维数组将数据 ...
- 将Excel数据导入数据库
Excel如下,这页工作表名叫“线路” 数据库表如下 using System; using System.Collections.Generic; using System.Linq; using ...
- ASP.NET Excel数据导入数据库
<identity impersonate="true"/> 是指模拟IIS身份验证 導入錯誤時可刪除 protected void btnImport_Click(o ...
- 利用poi,jxl将Excel数据导入数据库
需求:‘需要将本地的Excel中的数据经过验证之后导入数据库,在导入数据库之前在页面上展示出来 思路:将Excel导入存到session里面 去判断有没有不合法数据 如果有阻止提交 工具类: imp ...
随机推荐
- 手机网站支付如何接入支付宝简易版支付功能PHP版
接入支付宝准备工作:(关于账号可以是个体商户也可以是企业账号但必须有营业执照) 1.登录蚂蚁金服开放平台 2.创建应用,应用分类网页应用和移动应用.应用提交审核审核通过后得到Appid才能调用相应的 ...
- getContextPath、getServletPath、getRequestURI、getRealPath、getRequestURL、getPathInfo();的区别
<% out.println("getContextPath: "+request.getContextPath()+"<br/>"); ou ...
- Unity 用脚本给EventTrigger添加各种事件
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Eve ...
- Entity的约束
在DBContext的OnModelCreating()方法中调用上面的那个类 1.Infrastruture的Database文件夹建立Entityconfiguretions的文件夹 2.MyCo ...
- intellij IDE 破解 简单 License server 法
http://idea.iteblog.com/key.php
- spring利用xml配置定时任务
在开发中会经常遇到做定时任务的需求,例如日志定时清理与处理,数据信息定时同步等需求. 1.在spring中利用xml配置定时任务,如下 <!-- ftpiptv信息同步接口定时任务配置--> ...
- bootstrap-selectpicker 插件事件
$('#id').on('show.bs.select', function (e) { //绑定下拉显示列表触发事件 }); $('#id').on('hidden.bs.select', func ...
- 让theano在windows下能进行GPU并行的配置步骤
最近在implement DeepLearning.net上面的程序.对于开源的python,最头疼的就是各种package和各种configuration. 而且还是在windows下. 想要让th ...
- Error:Execution failed for task ':app:compileDebugJavaWithJavac'
百度一下呗 查找了各种解决方案,都不对症. 最后发现,造成这种异常的原因有很多.具体的还是要去终端编译,查看到底是什么地方出错了,然后具体问题具体分析. 终端进入项目的根目录,然后输入命令 ./gra ...
- Delphi 类类型和对象