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

时间:2022-10-19 20:51:08

I am trying to import data from excel sheet(.xlsx). I have found PHPExcel for import data but after downloading document and source code I am confused which file is important for me. I also tried to find out document on that site but not found the way.

我正在尝试从excel表(.xlsx)导入数据。我已经找到了PHPExcel的导入数据,但是下载了文档和源代码之后,我不知道哪个文件对我来说是重要的。我还试图找到那个网站的文件,但没有找到方法。

About my task: Read excel sheet data from selected sheet and insert data to my database table.

关于我的任务:从选定的表中读取excel表数据并将数据插入到数据库表中。

So I really thankful If You will guide me how to use it.

所以我非常感谢你能指导我如何使用它。

Thanks.

谢谢。

3 个解决方案

#1


12  

You can use the PHPExcel library to read an Excel file and insert the data into a database.

您可以使用PHPExcel库读取Excel文件并将数据插入数据库。

Sample code is below.

下面的示例代码。

//  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

#2


8  

From a quick look over the documentation, use the IOFactory to automatically determine the file format.

通过快速查看文档,可以使用IOFactory自动确定文件格式。

include 'path/to/PHPExcel/IOFactory.php';

// Let IOFactory determine the spreadsheet format
$document = PHPExcel_IOFactory::load('path/to/spreadsheet.xls');

// Get the active sheet as an array
$activeSheetData = $document->getActiveSheet()->toArray(null, true, true, true);

var_dump($activeSheetData);

#3


0  

Try this so you can jump start the development process:

试试这个,你就可以开始开发过程了:

include '.... /PHPexcel/Classes/PHPExcel.php';
    $dataFfile = "C:/tmp/test_data.xls";
    $objPHPExcel = PHPExcel_IOFactory::load($dataFfile);
    $sheet = $objPHPExcel->getActiveSheet();
    $data = $sheet->rangeToArray('A2:AB5523');
    echo "Rows available: " . count($data) . "\n";
    foreach ($data as $row) {
        print_r($row);
    }

Replace the include path with your path to the PHPExcel.php
Replace $dataFile with your excel file
Also adjust the range of the cells that you want to import

用到PHPExcel的路径替换包含路径。用您的excel文件替换$dataFile,也可以调整您想要导入的单元格的范围。

#1


12  

You can use the PHPExcel library to read an Excel file and insert the data into a database.

您可以使用PHPExcel库读取Excel文件并将数据插入数据库。

Sample code is below.

下面的示例代码。

//  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

#2


8  

From a quick look over the documentation, use the IOFactory to automatically determine the file format.

通过快速查看文档,可以使用IOFactory自动确定文件格式。

include 'path/to/PHPExcel/IOFactory.php';

// Let IOFactory determine the spreadsheet format
$document = PHPExcel_IOFactory::load('path/to/spreadsheet.xls');

// Get the active sheet as an array
$activeSheetData = $document->getActiveSheet()->toArray(null, true, true, true);

var_dump($activeSheetData);

#3


0  

Try this so you can jump start the development process:

试试这个,你就可以开始开发过程了:

include '.... /PHPexcel/Classes/PHPExcel.php';
    $dataFfile = "C:/tmp/test_data.xls";
    $objPHPExcel = PHPExcel_IOFactory::load($dataFfile);
    $sheet = $objPHPExcel->getActiveSheet();
    $data = $sheet->rangeToArray('A2:AB5523');
    echo "Rows available: " . count($data) . "\n";
    foreach ($data as $row) {
        print_r($row);
    }

Replace the include path with your path to the PHPExcel.php
Replace $dataFile with your excel file
Also adjust the range of the cells that you want to import

用到PHPExcel的路径替换包含路径。用您的excel文件替换$dataFile,也可以调整您想要导入的单元格的范围。