问题描述
我正在尝试从excel表格(.xlsx)导入数据.我已经找到 PHPExcel 用于导入数据,但是下载文档和源代码后,我很困惑哪个文件对我很重要.我还尝试在该站点上查找文档,但没有找到方法.
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工作表数据并将数据插入到我的数据库表中.
About my task: Read excel sheet data from selected sheet and insert data to my database table.
因此,如果您能指导我如何使用它,我真的很感激.
So I really thankful If You will guide me how to use it.
谢谢.
推荐答案
您可以使用PHPExcel库读取Excel文件并将数据插入数据库中.
You can use the PHPExcel library to read an Excel file and insert the data into a database.
示例代码如下.
// 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
这篇关于如何使用PHPExcel从Excel读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!