php读取excel(支持03,07)
需要用到PHPExcel这个类
附上代码
//phpExcel读取excel内容
header("Content-Type:textml;charset=utf-8");
//引用PHPExcel.php
require_once '../js/PHPExcel.php';
//文件路径
$filePath = "03711.xlsx";
//建立readerExcel对象
$PHPReader = new PHPExcel_Reader_Excel2007();
/**默认用excel2007读取excel,若格式不对,则用之前的版本进行读取*/
if(!$PHPReader->canRead($filePath)){
$PHPReader = new PHPExcel_Reader_Excel5();
if(!$PHPReader->canRead($filePath)){
exit('请使用03/07版本excel');
}
} //建立excel对象,此时你即可以读取文件excel文件内容
$PHPExcel = $PHPReader->load($filePath);
/**读取excel文件中的第一个工作表*/
$currentSheet = $PHPExcel->getSheet(0);
/**取得当前sheet名称*/
$currentSheetName=$currentSheet->getTitle();
/**取得sheet总数*/
$allSheet=$PHPExcel->getSheetCount();
/**取得最大的列号*/
$allColumn = $currentSheet->getHighestColumn();
/**转换为数字列数*/
$allColumn=PHPExcel_Cell::columnIndexFromString($allColumn);
/**取得一共有多少行*/
$allRow = $currentSheet->getHighestRow();
/*取得某一列的值 */
$value->getCellByColumnAndRow(0, 1)->getValue();
/* 这样就可以把excel的全部内容读取出来 */
/* 时间转换 */
$value=date("Y-m-d", PHPExcel_Shared_Date::ExcelToPHP($value) - 28800);
完整版的
$insert_flag = true; //是否写数据库标志
$errorinfo = array(); //错误信息数组
$success_num=;
$errorinfo1='不能为空';
$errorinfo2='结束时间不能小于开始时间';
$errorinfo3='所填年份在数据库中不存在';
$errorinfo4='写数据失败';
//文件路径
//$filePath = "../03711.xlsx";
//建立readerExcel对象
$PHPReader = new PHPExcel_Reader_Excel2007();
/**默认用excel2007读取excel,若格式不对,则用之前的版本进行读取*/
if(!$PHPReader->canRead($filepath)){
$PHPReader = new PHPExcel_Reader_Excel5();
if(!$PHPReader->canRead($filepath)){
exit('请使用03/07版本excel');
}
} //建立excel对象,此时你即可以读取文件excel文件内容
$PHPExcel = $PHPReader->load($filepath);
/**读取excel文件中的第一个工作表*/
$currentSheet = $PHPExcel->getSheet();
/**取得当前sheet名称*/
$currentSheetName=$currentSheet->getTitle();
/**取得sheet总数*/
$allSheet=$PHPExcel->getSheetCount();
/**取得最大的列号*/
$allColumn = $currentSheet->getHighestColumn();
/**转换为数字列数*/
$allColumn=PHPExcel_Cell::columnIndexFromString($allColumn);
/**取得一共有多少行*/
$allRow = $currentSheet->getHighestRow(); //echo "列:".$allColumn."行:".$allRow."<br/>"; /* 从第二行开始读取 */
for ($currentRow=;$currentRow<=$allRow;$currentRow++){
/*取得当前行每一列的值 ,列从0开始 */
//$year=$currentSheet->getCellByColumnAndRow(1, $currentRow)->getValue();
$year=$_GET['yearId'];
$startTime=$currentSheet->getCellByColumnAndRow(, $currentRow)->getValue();
$endTime=$currentSheet->getCellByColumnAndRow(, $currentRow)->getValue();
$remark=$currentSheet->getCellByColumnAndRow(, $currentRow)->getValue();
/*如果有一列的值为空,就跳出循环 */
if ($startTime==''||$endTime==''||$year==''){
$errorinfo[]=$errorinfo1.'|'.$currentRow;
continue;
}
if ($endTime<$startTime){
echo $startTime.'|'.$endTime;
$errorinfo[]=$errorinfo2.'|'.$currentRow;
continue;
}
$startTime=date("Y-m-d", PHPExcel_Shared_Date::ExcelToPHP($startTime) - );
$endTime=date("Y-m-d", PHPExcel_Shared_Date::ExcelToPHP($endTime) - );
$shorStartTime=str_replace('-', '.', substr($startTime, ));
$shorEndTime=str_replace('-', '.', substr($endTime, ));
// echo $year."|".$startTime."|".$endTime."|".$remark."<br/>";
if ($insert_flag){
$rs = $db -> select(, , 't_year', 'id,name', ' where id ="'. $year .'"' , 'id limit 1');
if (count($rs['id'])<){
$errorinfo[]=$errorinfo3.'|'.$currentRow;
continue;
}
if ($remark==''){
$sql='"';
}else{
$sql='('.$remark.')"';
}
$flag=$db -> insert(, , 't_period', 'year_id='.$rs['id'].',begtime="'.$startTime.'",endtime="'.$endTime.
'",remark="'.$remark.'",timename="'.$shorStartTime.'-'.$shorEndTime.$sql);
if ($flag!=){
$errorinfo[]=$errorinfo4.'|'.$currentRow;
}else{
$success_num = $success_num + ;
}
} } unlink($filepath);
if (count($errorinfo)>){
echo '<span style="color:#F00" >一共导入了' . $success_num . '条记录<br>';
echo "以下数据没有导入:<br>";
for ($i = ; $i < count($errorinfo); $i++) {
$temp_info = explode("|", $errorinfo[$i]);
echo "第" . $temp_info[] . "行错误信息:" . $temp_info[] . "<br>";
}
} else {
echo "数据导入成功<br>";
echo "一共导入了" . $success_num . "条记录<br></span>";
}
下载地址:http://pan.baidu.com/s/1i35ppBJ
完整版是api,另外一个是程序中用的。
php读取excel(支持03,07)的更多相关文章
- NPOI 导入导出excel 支持 03 07
因为微软的office成本太高了,所以开发项目的时候电脑上没安装office,而是安装了wps.但开发语言用的是C#,所以直接调用微软的office组件是很方便的,但一方面慢,一方面成本高,所以从网上 ...
- Java读取excel(兼容03和07格式)
读取excel,首先需要下载POI的jar,可以去官网下,也可以在这里下载 一.简单说明 excel2003和excel2007区别比较大,最直观的感受就是扩展名不一样,哈哈 不过,使用POI的API ...
- Java读取excel 支持xls 和 xlsx格式
1.工具类public class InExcelTool { //根据指定位置单独读取一个 public static String getContent(String file, int page ...
- php读取excel,以及php打包文件夹为zip文件
1.把文件下载到本地,放在在Apache环境下2.d.xlsx是某游戏的服务器名和玩家列表,本程序只适合此种xlsx文件结构,其他结构请修改index.php源码3.访问zip.php的功能是把生成的 ...
- java--poi读取excel图片和内容(支持03版本)
有的时候需要将excel中所包含的图片在导入的时候取出来存到服务器中, 详细实现代码如下: package com.liuf.util; import java.io.BufferedInputStr ...
- 原!操作 excel 03/07
参考 所用jar包: poi-3.11.jar poi-ooxml-3.11.jar poi-ooxml-schemas-3.11.jar /* * Project: fusion-may-open- ...
- poi读取excel模板,填充内容并导出,支持导出2007支持公式自动计算
/** * 版权所有(C) 2016 * @author www.xiongge.club * @date 2016-12-7 上午10:03:29 */ package xlsx; /** * @C ...
- NPOI读取Excel帮助类,支持xls与xlsx,实现公式解析,空行的处理
NPOI读取Excel(2003或者2010)返回DataTable.支持公式解析,空行处理. /// <summary>读取excel /// 默认第一行为表头 /// </sum ...
- JXL读取Excel(只支持xls版本)——(二)
注意:jxl是不支持xlsx后缀的excel的.因此建议用POI读取excel. Jar包 同一一样 Java代码 package JXL; import java.io.File; import j ...
随机推荐
- Linear Regression and Gradient Descent
随着所学算法的增多,加之使用次数的增多,不时对之前所学的算法有新的理解.这篇博文是在2018年4月17日再次编辑,将之前的3篇博文合并为一篇. 1.Problem and Loss Function ...
- 第一次工作->笔记:在phpstrom2019上搭建phpunit单元测试环境,php环境使用docker
前言:公司大佬让我开发一个工具,并合并到他的工具包中,使用的是github 说明:这里的php环境使用的是laradock.感兴趣的道友自行查找. 工具:php.phpstrom.phpunit.do ...
- vsCode python的函数跳转功能失效修复
经常遇到VsCode失效的问题,很有可能是插件的原因. 关闭插件提示功能 "python.pythonPath": "/usr/bin/python", 关闭所 ...
- Django 无法通过request.POST.get()获取数据的问题
原来是contentType为application/json时,Django不支持request.POST.get(),但可以通过request.body来获取string类型的参数: data = ...
- dataTable获取所有数据
"drawCallback": function(settings,e) { var api = new $.fn.dataTable.Api( settings ); resul ...
- Solr的学习使用之(三)IKAnalyzer中文分词器的配置
1.为什么要配置? 1.我们知道要使用Solr进行搜索,肯定要对词语进行分词,但是由于Solr的analysis包并没有带支持中文的包或者对中文的分词效果不好,需要自己添加中文分词器:目前呼声较高的是 ...
- https://blog.csdn.net/eguid_1/article/category/6270094
https://blog.csdn.net/eguid_1/article/category/6270094
- Zookeeper3.4.14集群搭建
环境: 3台centos7.4 1. 下载release:wget https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/zookeeper-3. ...
- 如何去除inline-block的默认间距
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title&g ...
- linux下载网页上的文件夹以及删除文件(stream)
wget -nd -r -l1 --no-parent http://www.cs.virginia.edu/stream/FTP/Code/ 注:-nd 不创建目录:-r 递归下载:-l1只下载当前 ...