php 如何使用PHPExcel从excel读取数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26322129/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to read data from excel using with PHPExcel
提问by Manan
I am trying to import data from excel sheet(.xlsx). I have found PHPExcelfor 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.
谢谢。
回答by Brainy Prb
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
回答by Tserkov
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);
回答by Antoan Milkov
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.php 的路径替换$dataFile
为您的 excel 文件
同时调整要导入的单元格的范围