【发布时间】:2019-08-06 23:42:47
【问题描述】:
【问题讨论】:
【问题讨论】:
//首先,从(https://github.com/PHPOffice/PHPExcel)下载PHPExcel,保存到项目目录下。它已被弃用,但页面上有指向它当前版本的链接。以下代码适用于旧版本,但在经过测试和工作时可能会帮助您入门
<?php
require_once('PHPExcel.php');
include 'PHPExcel/IOFactory.php';
$inputFileName = 'test_data.xlsx'; // Your Excel spreadsheet
if(file_exists($inputFileName)){
// Read your Excel workbook
try {
$inputFileType = PHPExcel_IOFactory::identify($inputFileName);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($inputFileName);
} catch (Exception $e) {
die('Error loading file "' . pathinfo($inputFileName, PATHINFO_BASENAME) . '": ' . $e->getMessage());
}
// Get worksheet dimensions
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow();
$highestColumn = $sheet->getHighestColumn();
// Loop through each row of the worksheet in turn
for ($row = 2; $row <= $highestRow; $row++) {
// Read a row of data into an array
$rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE);
foreach($rowData[0] as $k=>$v){
echo $k, $v.'<br>';
}
}
}
?>
【讨论】: