douyicao2199 2015-11-25 07:38
浏览 31
已采纳

Php - 阅读Excel并存储到Db

error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
$inputFileName ="../brief/phaseupload/" . $filename;
//  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 = 3; $row <= $highestRow; $row++){
//  Read a row of data into an array
 $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE);
print_r($rowData);

I use above code to read the excel file using PHPExcel. I managed to read the excel value and print it. Now the problem is I need to store the values in the database. How do I get the value, in this case from $rowData?
Below is the sample output:

Array ( [0] => Array ( [0] => 854273_19 [1] => Beds [2] => 61 [3] => Autumn Winter 2012 [4] => Divans [5] => Fabric [6] => Storage Bedding [7] => Single Divan [8] => Not Required [9] => Fabric [10] => White [11] => Not Required [12] => Not Required [13] => [14] => Not Required [15] => Divan With Mattress [16] => Not Required [17] => Not Required [18] => Not Required ) ) 0
  • 写回答

1条回答 默认 最新

  • duan0427 2015-11-25 07:48
    关注

    Seeing that you already have the number of rows on your array, you can proceed with this, inside the loop.

    $value1 = $rowData[$row]["A"];
    $value2 = $rowData[$row]["B"];
       //and so on...
    

    Make sure you clean the strings as you will be inserting to the db.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起