#今天接到一个新的需求 需要读取银行指定的xlsx文件模板 并按要求进行写入文件
/**
* 按银行的模板导出
* @author wapele
* @param array $data
* @return void
*/
public static function bank($data=[]){
$path = "./config/TransBat.xlsx";
$fileType = \PHPExcel_IOFactory::identify($path);
$objReader = \PHPExcel_IOFactory::createReader($fileType);
$objPHPExcel = $objReader->load($path);
$currentSheet = $objPHPExcel->getSheet(0); //第一个工作簿
$allRow = $currentSheet->getHighestRow(); //行数
$i = 3;
foreach ($data as $v) {
$x =0;
foreach ($v as $vv) {
$name = self::num2alpha($x).$i;
$objPHPExcel->getActiveSheet()->setCellValue($name,$vv);
$x++;
}
$i ++;
}
$file=date('YmdHis');
$objPHPExcel->setActiveSheetIndex(0);
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$file.'.xlsx"');
header('Cache-Control: max-age=0');
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, $fileType);
$objWriter->save('php://output');
exit();
}
/**
* 数字到字母列
* @author wapele
* @param int
* @param int
* @return string
*/
protected static function num2alpha($index, $start = 65)
{
$str = '';
if (floor($index / 26) > 0) {
$str .= self::num2alpha(floor($index / 26)-1);
}
return $str . chr($index % 26 + $start);
}