PHPExcel 读取文件内容
【摘要】
//声明header头部header("Content-type:text/html;charset=utf-8");//引入类库//include "Classes/PHPExcel/IOFactory.php";include __DIR__ . "/PHPExcel/Classes/PHPExcel/IOFactory.php";...
//声明header头部
header("Content-type:text/html;charset=utf-8");
//引入类库
//include "Classes/PHPExcel/IOFactory.php";
include __DIR__ . "/PHPExcel/Classes/PHPExcel/IOFactory.php";
date_default_timezone_set('PRC');
//TODO 导入数组开关
$data = array();
//elsx文件路径
$file = __DIR__ . '/20220510.xlsx';
//$file = __DIR__ . '/sx_0903.csv';
$allData = read_execl($file);
// 读取excel文件
function read_execl($inputFileName)
{
try {
$inputFileType = PHPExcel_IOFactory::identify($inputFileName);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($inputFileName);
} catch (Exception $e) {
echo "读取失败";
}
// 确定要读取的sheet,从0开始,0即第一个表,1即第二个表,...
$sheet = $objPHPExcel->getSheet(0);// 0即第一个表
$highestRow = $sheet->getHighestRow();// 取得总行数
$highestColumn = $sheet->getHighestColumn(); // 取得总列数
// 获取excel文件的数据,$row=2代表从第二行开始获取数据
for ($row = 2; $row <= $highestRow; $row++) {
// // rowData是一行的数据,遍历出来就是一列数据,字母A代表遍历所有列的数据,字母B开始就是单独遍历那一列的数据,我这里填写的C就是获取C列的数据
// $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE);
// //$rowData是一个嵌套数组,需要这样输出
// echo $rowData['0']['0'];
if (empty($sheet->getCell('A' . $row)->getValue())) {
break;
}
$data[] = array(
'content' => $sheet->getCell('A' . $row)->getValue(),
'city' => $sheet->getCell('B' . $row)->getValue(),
'hid' => $sheet->getCell('C' . $row)->getValue(),
'create_time' => $sheet->getCell('D' . $row)->getValue(),
);
}
return $data;
}
文章来源: blog.csdn.net,作者:lxw1844912514,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/lxw1844912514/article/details/124693153
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)