使用的第三方插件 Spreadsheet_Excel_Reader
- 下载插件
插件下载链接 下载解压后入下图所示 只要将Excel文件夹下的内容搬到自己的项目中 2.具体使用
require_once(REALPATH . 'include/Class/excel/reader.php');
/**
* 读取xls文件内容 返回单元格集合
* @return array
*/
public function getFileInfoAction () {
$url = $this->request->getPost('url');
if (empty($url)) {
return [
'code' => BaseCode::LOSE_PARAM,
'message' => BaseCode::getErrorMessage(BaseCode::LOSE_PARAM)
];
}
// 拿到文件后缀
$result = explode('?', $url)[0];
$result = explode('.', $result);
$len = count($result);
$suffix = $result[$len - 1]; // 拿到图片后缀
$data = file_get_contents($url);
$credentialsFileName = date('YmdHis') . "." . $suffix;
$path = "/data/tmp/{$credentialsFileName}";
file_put_contents($path, $data);
$reader = new Spreadsheet_Excel_Reader();
$reader->setOutputEncoding('UTF-8'); // 中文乱码
$reader->read($path);
$winner_list = array();
for ($i = 1; $i <= $reader->sheets[0]['numRows']; $i++) {
for ($j = 1; $j <= $reader->sheets[0]['numCols']; $j++) {
array_push($winner_list, $reader->sheets[0]['cells'][$i][$j]);
}
}
// 删除服务器文件
shell_exec('rm -rf ' . $path);
return [
'code' => BaseCode::SUCCESS,
'message' => BaseCode::getErrorMessage(BaseCode::SUCCESS),
'body' => [
'data' => $winner_list
]
];
}
3.缺陷 不能读取xlsx文件
|