我有一个php应用程序,我想从excel读取数据,插入数据库,然后为特定用户生成pdf报告.
我搜索了很多但没有具体说明这两件事.
解决方法:
使用PHPExcel库读取Excel文件并将数据传输到数据库中
// Include PHPExcel_IOFactory
include 'PHPExcel/IOFactory.php';
$inputFileName = './sampleData/example1.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 your database of choice here
}
更多的东西变得非常依赖于您的数据库,以及您希望如何在其中构建数据