如何使用 PHPExcel从单个 excel 文件中读取多个工作表。[使用 Codeigniter 3.1.11]
来自单个 excel 文件
1的多个工作表:https ://i.stack.imgur.com/KyPQz.png
源代码 // 批量上传函数 bulk_upload() { $output = array();
$file_upload = $_FILES['bulk_karvy_file']['tmp_name'];
if (isset($file_upload) && !empty($file_upload)){
//You can add directly the Composer Autoloder in your controller:
require FCPATH . 'vendor/autoload.php';
try{
$db_spreadsheet = PHPExcel_IOFactory::load($file_upload);
}
catch (Exception $e){
die('Error loading file "' . pathinfo($file_upload, PATHINFO_BASENAME) . '": '.@$e->getMessage());
}
// sheet list
$sheetListArray = ["Ann B","Ann C","Ann E","Ann F"];
$sheetData = [];
$sheetCount = $db_spreadsheet->getSheetCount();
if(is_null($sheetCount))
{}
else{
// Read Excel Sheet
for ($i = 0; $i < $sheetCount; $i++)
{
$sheet = $db_spreadsheet->getSheet($i);
$sheetN = $sheet->getTitle();
if(in_array($sheetN,$sheetListArray))
{
$sheetData[] = array(
'data' => $sheet->toArray(null, true, true, true),
'file' => $sheetN,
'total' => count($sheet->toArray(null, true, true, true))
);
}
}
// Read all Sheets from your Excel file, you have to use foreach loop
foreach($sheetData as $val)
{
$N = @$val['total'];
for($i=2; $row <= $N; $i++)
{
$db_ch= 64;
$sl_no = trim($val['data'][$i][chr(++$db_ch)]);
if(empty($sl_no)){break;}
$ref_no = trim($val['data'][$i][chr(++$db_ch)]);
$remark = trim($val['data'][$i][chr(++$db_ch)]);
$dep_slip_no = trim($val['data'][$i][chr(++$db_ch)]);
$inst_no = str_pad(trim($val['data'][$i][chr(++$db_ch)]),6, '0', STR_PAD_LEFT);
$amount = trim($val['data'][$i][chr(++$db_ch)]);
$drawee_name = trim($val['data'][$i][chr(++$db_ch)]);
$sch_code = str_pad(trim($val['data'][$i][chr(++$db_ch)]),3, '0', STR_PAD_LEFT);
$ufc_code = trim($val['data'][$i][chr(++$db_ch)]);
$acc_no = trim($val['data'][$i][chr(++$db_ch)]);
$ihno = trim($val['data'][$i][chr(++$db_ch)]);
$trno = trim($val['data'][$i][chr(++$db_ch)]);
$tr_dt = str_replace('/', '-',trim($val['data'][$i][chr(++$db_ch)]));
$value_date = str_replace('/', '-',trim($val['data'][$i][chr(++$db_ch)]));
$branch = trim($val['data'][$i][chr(++$db_ch)]);
$file_name = trim($val['data'][$i][chr(++$db_ch)]);
$category = trim($val['data'][$i][chr(++$db_ch)]);
$upload_type = trim($val['data'][$i][chr(++$db_ch)]);
$fl_name = @$val['file'];
$fl_name_id = @$db_fn->id;
$created_at = date("Y-m-d H:i:s");
$updated_at = date("Y-m-d H:i:s"); [chr(++$db_ch)]);
}
}
} // end of else
}
echo json_encode($sheetData);
}