我正在尝试从 Excel 工作表 (.xlsx) 导入数据。我找到了 PHPExcel用于导入数据,但在下载文档和源代码后,我很困惑哪个文件对我很重要。我也试图在那个网站上找到文档,但没有找到方法。
关于我的任务:从选定的工作表中读取 excel 工作表数据并将数据插入到我的数据库表中。
所以我真的很感谢你能指导我如何使用它。
谢谢。
最佳答案
您可以使用 PHPExcel 库读取 Excel 文件并将数据插入数据库。
示例代码如下。
// Include PHPExcel_IOFactory
include 'PHPExcel/IOFactory.php';
$inputFileName = 'sample.xls';
// 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 = 1; $row <= $highestRow; $row++){
// Read a row of data into an array
$rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row,
NULL,
TRUE,
FALSE);
// Insert row data array into database here using your own structure
关于php - 如何使用 PHPExcel 从 excel 中读取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26322129/