php - 如何使用 PHPExcel 从 excel 中读取数据

标签 php phpexcel

我正在尝试从 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/

相关文章:

php - 想要降级 phpMyAdmin,但不知道什么版本

php - Facebook OAuthException : "user hasn' t authorized the application to perform this action"

PHP 无法访问新创建的 zip 文件

php - 从html表生成excel文件后出现奇怪的符号

php - 如何用php生成一个大的Excel文件?

PHPExcel 创建/样式化/保存 PDF 文档

php - 组合字母数字时如何选择真值?

php - 从 codeigniter 2 中的类似函数中删除引号

php - 如何在mysql中创建一个表,其中表中的字段数等于php中excel文件中的列数?

即使使用单元格缓存,PHPExcel 内存仍然耗尽 - 其他解决方案