<form action="{:U('Index/files')}" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"/>
<br/>
<input type="submit" name="submit" value="Submit"/>
</form>
 public function files(){
$filename = $_FILES['file']['name'];
$ext = preg_split("/\./", $filename);
$ext = strtolower($ext[1]);
/*
* 获取文件后缀名
*/
$allowed_types = array("xls", "xlsx");
$filePath = "./Public/excel/" . $_FILES["file"]["name"];
// print_r($filePath);die; if (!in_array($ext, $allowed_types)) {
echo "File type is wrong!";
die;
}
// else if (file_exists($filePath)) {
// echo "A file with this name already exists!";
// die;
// }
else {
move_uploaded_file($_FILES["file"]["tmp_name"], $filePath);
}
/**
* @param $val
* @return string
* 检查文件名是否符合要求,如果符合,就保存到指定路径。如果不符合,报错。
*/
// import("Org.Util.PHPExcel");
require_once "./ThinkPHP/Library/Org/Util/PHPExcel/Classes/PHPExcel.php";
//$filePath=$_FILES["file"]["tmp_name"];
//sleep(50);
$PHPExcel = new \PHPExcel();
//默认用excel2007读取excel,若格式不对,则用之前的版本进行读取
$PHPReader = new \PHPExcel_Reader_Excel2007();
if (!$PHPReader->canRead($filePath)) {
$PHPReader = new \PHPExcel_Reader_Excel5();
if (!$PHPReader->canRead($filePath)) {
return $this->error(null, "no file");
}
}
$PHPExcel = $PHPReader->load($filePath);
$sheetCount = $PHPExcel->getSheetCount();
$sheetNames = $PHPExcel->getSheetNames();
//var_dump($sheetNames);
//die;
/**读取excel文件中有多少个sheet*/
$objExcel = array();
for ($SheetID = 0; $SheetID < $sheetCount; $SheetID++) {
/**读取excel文件中的工作表*/
$name = $sheetNames[$SheetID];
$currentSheet = $PHPExcel->getSheetByName($name);
$name = iconv("utf-8", "gb2312", $name);
/**取得最大的列号*/
$allColumn = $currentSheet->getHighestColumn();
/**取得一共有多少行*/
$allRow = $currentSheet->getHighestRow();
for ($currentRow = 1; $currentRow <= $allRow; $currentRow++) {
/**从第A列开始输出*/
for ($currentColumn = 'A'; $currentColumn <= $allColumn; $currentColumn++) {
$val = $currentSheet->getCellByColumnAndRow(ord($currentColumn) - 65, $currentRow)->getValue();
$val = iconv("utf-8", "gb2312", $val);
$objExcel[$name][$currentRow - 1][ord($currentColumn) - 65] = $val;
//编码需要转换成gb2312
/*
$val = urlencode($val);
$objExcel[$name][$currentRow - 1][ord($currentColumn) - 65] = urldecode($val);
*/
}
}
} unlink($filePath);
/*
* $objExcel = json_encode($objExcel);
* $objExcel = urldecode($objExcel);
* 读取文件中的内容,因为json_encode()的参数必须是utf-8编码。为了保证汉字输出的结果,这里先将数组中的内容用urlencode进行编码,
* 在被json_encode()编码之后,再用urldecode解码。
*/
//return $objExcel;
// print_r($objExcel);
foreach ($objExcel as $k=>$v){
//取出里面的id拼接成新的一维数组
$result = array_reduce($v, 'array_merge', array());
//过滤掉里面的空值取出id
$id = array_filter($result); } }

  

最新文章

  1. 我为NET狂官方群福利贴:一些常用的工具:2016-08-01更新
  2. javascript面向对象系列第二篇——创建对象的5种模式
  3. FusionCharts简单教程(六)------加载外部Logo
  4. libprotobuf ERROR
  5. GetPrivateProfileStringA的文件名要小心写
  6. mount
  7. hdu3998 Sequence(最大流,LIS)
  8. XML限制、初步WEB服务
  9. HPU--1091 N!的位数
  10. react基础(2)
  11. 如何在vue中使用sass
  12. Think twice, code once.
  13. case when then
  14. 阿里云服务器 CentOS 安装Mysql 5.6
  15. 学习Spring Boot:(二十六)使用 RabbitMQ 消息队列
  16. webpack打包器简单入门
  17. [BZOJ2669] [cqoi2012]局部极小值
  18. android多lib库工程的自动批量构建--准备
  19. Python入门-内置函数二
  20. HDU 1078 FatMouse and Cheese ( DP, DFS)

热门文章

  1. 关于linux centos7 vmware 和windows7 文件共享笔记
  2. Python的历史及介绍
  3. Github pages博客搭建与域名绑定
  4. Dart编程实例 算术操作符
  5. Service7
  6. CSS适配,方案
  7. CSS:CSS 语法
  8. tomcat 相关异常
  9. POJ 1265 Area (Pick定理 &amp; 多边形面积)
  10. 【WLAN常用语】—VAP