123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- <?php
- // 应用公共文件
- function ajax_return($code = 0, $msg = '', $data = [])
- {
- $res = ['code' => $code, 'msg' => $msg, 'data' => $data];
- $response = \think\Response::create($res, 'json');
- throw new \think\exception\HttpResponseException($response);
- }
- function url(string $url = '', array $vars = [], $suffix = true, $domain = true)
- {
- return \think\facade\Route::buildUrl($url, $vars)->suffix($suffix)->domain($domain);
- }
- function rand_str($len = 6)
- {
- $str = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
- $res = '';
- for ($i = 0; $i < $len; $i++) {
- $key = rand(0, strlen($str) - 1);
- $res .= $str[$key];
- }
- return $res;
- }
- function array_get($array, $key, $default = null)
- {
- return isset($array[$key]) ? $array[$key] : $default;
- }
- /**
- * 公共数据导出实现功能
- * @param string $expTitle
- * @param array $expCellName [['a','A']]
- * @param array $expTableData [['a'=>1]]
- * @param array $textValue ['a']
- */
- function export_exl($expTitle, $expCellName, $expTableData, $textValue = [])
- {
- $fileName = $expTitle . date('_YmdHis'); //or $xlsTitle 文件名称可根据自己情况设定
- $cellNum = count($expCellName);
- $dataNum = count($expTableData);
- $objPHPExcel = new \PHPExcel();
- $cellName = get_exl_column_letter(count($expCellName));
- for ($i = 0; $i < $cellNum; $i++) {
- $objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i] . '1', $expCellName[$i][1]);
- }
- // Miscellaneous glyphs, UTF-8
- for ($i = 0; $i < $dataNum; $i++) {
- for ($j = 0; $j < $cellNum; $j++) {
- if (in_array($expCellName[$j][0], $textValue)) {
- $objPHPExcel->getActiveSheet(0)->setCellValueExplicit($cellName[$j] . ($i + 2), $expTableData[$i][$expCellName[$j][0]], \PHPExcel_Cell_DataType::TYPE_STRING);
- } else {
- $objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j] . ($i + 2), $expTableData[$i][$expCellName[$j][0]]);
- }
- }
- }
- ob_end_clean();
- header('pragma:public');
- header('Content-type:application/vnd.ms-excel');
- header("Content-Disposition:attachment;filename=$fileName.xls"); //attachment新窗口打印inline本窗口打印
- $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
- $objWriter->save('php://output');
- exit;
- }
- function get_exl_column_letter($num = 1)
- {
- $res = [];
- for ($i = 0; $i < $num; $i++) {
- $y = ($i / 26);
- if ($y >= 1) {
- $y = intval($y);
- $res[] = chr($y + 64) . chr($i - $y * 26 + 65);
- } else {
- $res[] = chr($i + 65);
- }
- }
- return $res;
- }
- /**
- * @param string $file
- * @param array $cell
- * @param int $crop
- * @param int $sheet
- * @return array
- */
- function import_exl($file = '', $cell = [], $crop = 0, $sheet = 0)
- {
- $file = iconv("utf-8", "gb2312", $file); //转码
- if (empty($file) or !file_exists($file)) {
- die('file not exists!');
- }
- $objRead = new PHPExcel_Reader_Excel2007(); //建立reader对象
- if (!$objRead->canRead($file)) {
- $objRead = new PHPExcel_Reader_Excel5();
- if (!$objRead->canRead($file)) {
- die('No Excel!');
- }
- }
- $cellName = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM', 'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AW', 'AX', 'AY', 'AZ'];
- $obj = $objRead->load($file); //建立excel对象
- $currSheet = $obj->getSheet($sheet); //获取指定的sheet表
- $columnH = $currSheet->getHighestColumn(); //取得最大的列号
- $columnCnt = array_search($columnH, $cellName);
- $rowCnt = $currSheet->getHighestRow(); //获取总行数
- $data = [];
- for ($_row = 1; $_row <= $rowCnt; $_row++) { //读取内容
- if ($_row > $crop) {
- for ($_column = 0; $_column <= $columnCnt; $_column++) {
- $cellId = $cellName[$_column] . $_row;
- $cellValue = $currSheet->getCell($cellId)->getValue();
- //$cellValue = $currSheet->getCell($cellId)->getCalculatedValue(); #获取公式计算的值
- if ($cellValue instanceof PHPExcel_RichText) { //富文本转换字符串
- $cellValue = $cellValue->__toString();
- } else {
- $cellValue = (string)$cellValue;
- }
- if (!empty($cell[$_column])) {
- $data[$_row][$cell[$_column]] = $cellValue;
- }
- }
- }
- }
- return array_values($data);
- }
|