1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- <?php
- namespace app\common;
- class Excel
- {
- public function __construct()
- {
- include_once(CMF_ROOT . '/extend/Excel/PHPExcel.php');
- }
- public function import($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;
- } else {
- $data[$_row][] = $cellValue;
- }
- }
- }
- }
- return array_values($data);
- }
- /**
- * @param $expTitle
- * @param $expCellName [['a','A']]
- * @param $expTableData [['a'=>1]]
- * @param $textValue ['a']
- */
- public function export($expTitle, $expCellName, $expTableData, $textValue=[])
- {
- $xlsTitle = iconv('utf-8', 'gb2312', $expTitle); //文件名称
- $fileName = $expTitle . date('_YmdHis'); //or $xlsTitle 文件名称可根据自己情况设定
- $cellNum = count($expCellName);
- $dataNum = count($expTableData);
- $objPHPExcel = new \PHPExcel();
- $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'];
- 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]]);
- }
- }
- }
- header('pragma:public');
- header('Content-type:application/vnd.ms-excel;charset=utf-8;name="' . $xlsTitle . '.xls"');
- header("Content-Disposition:attachment;filename=$fileName.xls"); //attachment新窗口打印inline本窗口打印
- $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
- $objWriter->save('php://output');
- exit;
- }
- }
|