php csv以及excel文件格式读取数据的处理
public function upload()
{
$filesArr = [];
$validate = ['ext' => 'xls,xlsx,csv'];
$move = '../data/runtime/feetempletfiles';
$files = request()->file('files');
if (is_object($files)) {
$info = $files->validate($validate)->move($move);
if ($info) {
array_push($filesArr, $info);
} else {
$this->fileError = $files->getError();
return false;
}
} else {
if (empty($files)) {
return null;
} else {
foreach ($files as $file) {
$info = $file->validate($validate)->move($move);
if ($info) {
array_push($filesArr, $info);
} else {
$this->fileError = $file->getError();
return false;
}
}
}
}
return $filesArr;
}
public function getFileError()
{
return $this->fileError;
}
public function handleFiles($files)
{
$type = $this->judgeFileForm($files->getExtension());
if ($type == 'excel') {
$data = $this->disposeExcel($files);
} else {
$data = $this->disposeCsv($files);
}
return $data ? $data : false;
}
public function judgeFileForm(string $filesExtension)
{
$filesExtension = strtolower($filesExtension);
if (in_array($filesExtension, ['xls', 'xlsx'])) {
return 'excel';
} else {
return 'csv';
}
}
public function disposeExcel(object $fileInfo)
{
ini_set('memory_limit', '2048M');
vendor('phpoffice.phpexcel.Classes.PHPExcel.IOFactory');
$PHPReader = \PHPEXCEl_IOFactory::createReaderForFile($fileInfo->getPathname());
$objPHPExcel = $PHPReader->load($fileInfo->getPathname());
$sheetCount = $objPHPExcel->getSheetCount();
$sheetSelected = 0;
$objPHPExcel->setActiveSheetIndex($sheetSelected);
$rowCount = $objPHPExcel->getActiveSheet()->getHighestRow();
$columnCount = $objPHPExcel->getActiveSheet()->getHighestColumn();
$data = [];
for ($row = 1; $row <= $rowCount; $row++) {
for ($column = 'A'; $column <= $columnCount; $column++) {
$data[] = $objPHPExcel->getActiveSheet()->getCell($column . $row)->getValue();
}
}
if (empty($data)) {
return false;
} else {
$data = FreightTemplateTransfer::fileInProcessData($data);
return $data;
}
}
public function disposeCsv(object $fileInfo)
{
ini_set('memory_limit', '2048M');
$fopen = fopen($fileInfo->getPathname(), 'r');
$checkEncoding = '';
while ($datum = fgetcsv($fopen)) {
$checkEncoding = empty($checkEncoding) ? $this->checkEncoding($datum[0]) : $checkEncoding;
foreach ($datum as $key => $v) {
$data[] = mb_convert_encoding($v, "UTF-8", $checkEncoding);
}
}
fclose($fopen);
if (empty($data)) {
return false;
} else {
$data = FreightTemplateTransfer::fileInProcessData($data);
return $data;
}
}
public function fgetcsvReg(&$handle, $length = null, $d = ',', $e = '"')
{
$d = preg_quote($d);
$e = preg_quote($e);
$_line = "";
$eof = false;
while ($eof != true) {
$_line .= (empty ($length) ? fgets($handle) : fgets($handle, $length));
$itemcnt = preg_match_all('/' . $e . '/', $_line, $dummy);
if ($itemcnt % 2 == 0)
$eof = true;
}
$_csv_line = preg_replace('/(?: |[ ])?$/', $d, trim($_line));
$_csv_pattern = '/(' . $e . '[^' . $e . ']*(?:' . $e . $e . '[^' . $e . ']*)*' . $e . '|[^' . $d . ']*)' . $d . '/';
preg_match_all($_csv_pattern, $_csv_line, $_csv_matches);
$_csv_data = $_csv_matches[1];
for ($_csv_i = 0; $_csv_i < count($_csv_data); $_csv_i++) {
$_csv_data[$_csv_i] = preg_replace('/^' . $e . '(.*)' . $e . '$/s', '$1', $_csv_data[$_csv_i]);
$_csv_data[$_csv_i] = str_replace($e . $e, $e, $_csv_data[$_csv_i]);
}
return empty ($_line) ? false : $_csv_data;
}
public function checkEncoding($str = "")
{
$encodings = [
'ASCII',
'UTF-8',
'GBK'
];
foreach ($encodings as $encoding) {
if ($str === mb_convert_encoding(mb_convert_encoding($str, "UTF-32", $encoding), $encoding, "UTF-32")) {
return $encoding;
}
}
return false;
}