PHPExcel读取文件内容
(2022-10-10 00:38:22)
//声明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;