我有一个 php 应用程序,我将数据从 excel 工作表导入到数据库中。为此,我正在使用 phpExcel。
我为此实现了一个简单的导入功能。
现在,我的问题是当我以特定格式将数据从 Excel 工作表导入数据库时。喜欢,
表名:-Mytable
编号 |姓名 |姓 |电邮 |日期 |
和我用来导入的excel文件有相同的格式。但是格式可以改变,比如,
编号 |姓 |姓名 |日期 |邮箱 |
如何将 excel 文件中的列映射到数据库表中,以便将正确的数据输入到数据表中。
最佳答案
首先读取标题,并遍历每个标题条目以获取列名,然后当您循环行时,您可以构建一个适当映射的该行的 StdClass 对象的关联数组
$headers = $objPHPExcel->getActiveSheet()->toArray('A1');
$highestRow = $sheet->getHighestRow();
// Loop through each row of the worksheet in turn
for ($row = 1; $row <= $highestRow; $row++){
// Read a row of data into an array
$rowData = $sheet->rangeToArray('A' . $row);
// Map the row data array to the headers array
$dataMapArray = array_combine(
$header,
$rowData
);
// If you prefer objects, then convert that associative array to an object
$dataMapObject = (object) $dataMapArray;
}
关于php - 使用 PHPExcel 导入时将表列与数据映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33472091/