我正在尝试从excel工作表(.xlsx)导入数据。我已经找到了导入数据的PHPExcel,但是在下载文档和源代码之后,我很困惑哪一个文件对我来说很重要。我也试图在那个网站上找出文件,但没有找到方法。
关于我的任务的:从选定的工作表读取excel工作表数据,并将数据插入到数据库表中。
所以如果你能指导我如何使用它,我真的很感激。
谢谢。
发布于 2014-10-12 05:32:42
您可以使用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发布于 2014-10-12 05:37:21
通过快速查看文档,使用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);发布于 2014-10-12 05:33:01
尝试这个方法,这样您就可以快速启动开发过程:
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);
}用指向PHPExcel.php的路径替换包含路径
用excel文件替换$dataFile
还可以调整要导入的单元格的范围。
https://stackoverflow.com/questions/26322129
复制相似问题