|
@@ -0,0 +1,232 @@
|
|
|
+<?php
|
|
|
+class Model_excelnew extends Lin_Model {
|
|
|
+ private $objSheet ;
|
|
|
+ function __construct(){
|
|
|
+ parent::__construct();
|
|
|
+ }
|
|
|
+
|
|
|
+ public function doHeadMerg($titlename,$filename,$header,$list_data){
|
|
|
+ libxml_use_internal_errors(true);
|
|
|
+ require_once "./data/excel/PHPExcel/IOFactory.php";
|
|
|
+ $objPHPExcel = new PHPExcel();
|
|
|
+ $objSheet = $objPHPExcel->getActiveSheet();
|
|
|
+ $objSheet->getDefaultStyle()->getNumberFormat()->setFormatCode('@');
|
|
|
+ $columns = range('A', 'Z'); // 支持最多26列:ml-citation{ref="7" data="citationList"}
|
|
|
+ $columnIndex = 0;
|
|
|
+ $end_column_title = "A";
|
|
|
+ // 设置表头内容
|
|
|
+ $header_title_name = [];
|
|
|
+ foreach ($header as $fieldName) {
|
|
|
+ $end_column_title = $column = $columns[$columnIndex];
|
|
|
+ $objSheet->setCellValue($column . '1', $fieldName['title']);
|
|
|
+ $columnIndex++;
|
|
|
+ $header_title_name[] = $end_column_title;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ // 合并主标题
|
|
|
+ $objSheet->mergeCells('A1:'.$end_column_title.'1');
|
|
|
+ $objSheet->setCellValue('A1', $titlename);
|
|
|
+
|
|
|
+ // 主标题样式
|
|
|
+ $mainHeaderStyle = [
|
|
|
+ 'font' => ['bold' => true, 'size' => 16],
|
|
|
+ 'alignment' => ['horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER]
|
|
|
+ ];
|
|
|
+ $objSheet->getStyle('A1:D1')->applyFromArray($mainHeaderStyle);
|
|
|
+
|
|
|
+ // 子表头
|
|
|
+ $objSheet->fromArray($header_title_name, null, 'A2');
|
|
|
+
|
|
|
+ // 子表头样式
|
|
|
+ $subHeaderStyle = [
|
|
|
+ 'font' => ['bold' => true, 'color' => ['rgb' => '4F81BD']],
|
|
|
+ 'fill' => [
|
|
|
+ 'type' => PHPExcel_Style_Fill::FILL_SOLID,
|
|
|
+ 'color' => ['rgb' => 'DCE6F1']
|
|
|
+ ]
|
|
|
+ ];
|
|
|
+ $objSheet->getStyle('A2:'.$end_column_title.'2')->applyFromArray($subHeaderStyle);
|
|
|
+
|
|
|
+
|
|
|
+ // 设置所有列自适应宽度
|
|
|
+ foreach ($header_title_name as $k=>$col) {
|
|
|
+ if(empty($header[$k])){
|
|
|
+ $objSheet->getColumnDimension($col)->setAutoSize(true); // 自动调整:ml-citation{ref="1,5" data="citationList"}
|
|
|
+ }else{
|
|
|
+ if($header[$k]['width'] == 0){
|
|
|
+ $objSheet->getColumnDimension($col)->setAutoSize(true); // 自动调整:ml-citation{ref="1,5" data="citationList"}
|
|
|
+ }else{
|
|
|
+ $objSheet->getColumnDimension($col)->setWidth($header[$k]['width']); // 自动调整:ml-citation{ref="1,5" data="citationList"}
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ // // 设置列宽
|
|
|
+ // $objSheet->getColumnDimension('A')->setWidth(12); // 固定宽度
|
|
|
+ // $objSheet->getColumnDimension('B')->setAutoSize(true); // 自动宽度
|
|
|
+ // $objSheet->getColumnDimension('C')->setWidth(20);
|
|
|
+ // $objSheet->getColumnDimension('D')->setWidth(18);
|
|
|
+ // 从第二行开始写入数据
|
|
|
+ $rowIndex = 3;
|
|
|
+ $objSheet->fromArray($list_data, null, 'A3');
|
|
|
+
|
|
|
+ // // 强制设置手机号为文本格式(避免科学计数法)
|
|
|
+ // $objSheet->getStyle('A3:A'.$rowIndex)
|
|
|
+ // ->getNumberFormat()
|
|
|
+ // ->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
|
|
|
+
|
|
|
+ header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
|
|
|
+ header('Content-Disposition: attachment;filename="'.$filename.'.xlsx"');
|
|
|
+ $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
|
|
|
+ $objWriter->save('php://output');
|
|
|
+ }
|
|
|
+ public function doExcel($header,$list){
|
|
|
+
|
|
|
+ libxml_use_internal_errors(true);
|
|
|
+ require_once "./data/excel/PHPExcel/IOFactory.php";
|
|
|
+ $objPHPExcel = new PHPExcel();
|
|
|
+ $objSheet = $objPHPExcel->getActiveSheet();
|
|
|
+ $columns = range('A', 'Z'); // 支持最多26列:ml-citation{ref="7" data="citationList"}
|
|
|
+ $columnIndex = 0;
|
|
|
+
|
|
|
+ // 设置表头内容
|
|
|
+ foreach ($header as $fieldName) {
|
|
|
+ $column = $columns[$columnIndex];
|
|
|
+ $objSheet->setCellValue($column . '1', $fieldName);//自定义宽度
|
|
|
+ $columnIndex++;
|
|
|
+ }
|
|
|
+ //自定义宽度
|
|
|
+ foreach ($header as $fieldName) {
|
|
|
+ $column = $columns[$columnIndex];
|
|
|
+ $objSheet->setCellValue($column . '1', $fieldName);//自定义宽度
|
|
|
+ $columnIndex++;
|
|
|
+ }
|
|
|
+ //$objSheet->getColumnDimension('D')->setAutoSize(true);
|
|
|
+
|
|
|
+ // 设置HTTP头直接下载文件
|
|
|
+ header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
|
|
|
+ header('Content-Disposition: attachment;filename="user_export.xlsx"');
|
|
|
+ header('Cache-Control: max-age=0');
|
|
|
+
|
|
|
+ // 生成Excel文件并输出到浏览器
|
|
|
+ $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
|
|
|
+ $objWriter->save('php://output');
|
|
|
+ exit;
|
|
|
+
|
|
|
+ }
|
|
|
+ private function setHeader($header){
|
|
|
+
|
|
|
+ }
|
|
|
+ // public function setExcel($header,$list){
|
|
|
+ // libxml_use_internal_errors(true);
|
|
|
+ // require_once "./data/excel/PHPExcel/IOFactory.php";
|
|
|
+ // $objPHPExcel = new PHPExcel();
|
|
|
+ // $objPHPExcel->getProperties()->setTitle("export")->setDescription("none");
|
|
|
+ // $objPHPExcel->setActiveSheetIndex(0);// 创建PHPExcel对象
|
|
|
+ // $objPHPExcel = new PHPExcel();
|
|
|
+
|
|
|
+ // // 设置文档属性
|
|
|
+ // $objPHPExcel->getProperties()
|
|
|
+ // ->setCreator("Admin") // 创建者
|
|
|
+ // ->setLastModifiedBy("Admin") // 最后修改者
|
|
|
+ // ->setTitle("用户数据导出") // 标题
|
|
|
+ // ->setSubject("用户信息"); // 主题
|
|
|
+
|
|
|
+ // // 操作第一个工作表
|
|
|
+ // $objSheet = $objPHPExcel->setActiveSheetIndex(0);
|
|
|
+ // $objSheet->setTitle('用户列表'); // 工作表名称
|
|
|
+
|
|
|
+ // // 设置表头
|
|
|
+ // $objSheet->setCellValue('A1', '用户ID')
|
|
|
+ // ->setCellValue('B1', '姓名')
|
|
|
+ // ->setCellValue('C1', '手机号')
|
|
|
+ // ->setCellValue('D1', '注册时间');
|
|
|
+
|
|
|
+ // // 模拟数据填充(示例数据)
|
|
|
+ // $data = [
|
|
|
+ // [1001, '张三', '13800138000', '2025-01-01'],
|
|
|
+ // [1002, '李四', '13912345678', '2025-02-15']
|
|
|
+ // ];
|
|
|
+
|
|
|
+ // // 从第二行开始写入数据
|
|
|
+ // $rowIndex = 2;
|
|
|
+ // foreach ($data as $row) {
|
|
|
+ // $objSheet->setCellValue('A'.$rowIndex, $row[0])
|
|
|
+ // ->setCellValue('B'.$rowIndex, $row[1])
|
|
|
+ // ->setCellValue('C'.$rowIndex, $row[2])
|
|
|
+ // ->setCellValue('D'.$rowIndex, $row[3]);
|
|
|
+ // $rowIndex++;
|
|
|
+ // }
|
|
|
+
|
|
|
+ // // 强制设置手机号为文本格式(避免科学计数法)
|
|
|
+ // $objSheet->getStyle('C2:C'.$rowIndex)
|
|
|
+ // ->getNumberFormat()
|
|
|
+ // ->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
|
|
|
+
|
|
|
+ // // 设置HTTP头直接下载文件
|
|
|
+ // header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
|
|
|
+ // header('Content-Disposition: attachment;filename="user_export.xlsx"');
|
|
|
+ // header('Cache-Control: max-age=0');
|
|
|
+
|
|
|
+ // // 生成Excel文件并输出到浏览器
|
|
|
+ // $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
|
|
|
+ // $objWriter->save('php://output');
|
|
|
+ // exit;
|
|
|
+ // }
|
|
|
+
|
|
|
+ public function demo(){
|
|
|
+ // 创建PHPExcel对象
|
|
|
+$objPHPExcel = new PHPExcel();
|
|
|
+
|
|
|
+// 设置文档属性
|
|
|
+$objPHPExcel->getProperties()
|
|
|
+ ->setCreator("Admin") // 创建者
|
|
|
+ ->setLastModifiedBy("Admin") // 最后修改者
|
|
|
+ ->setTitle("用户数据导出") // 标题
|
|
|
+ ->setSubject("用户信息"); // 主题
|
|
|
+
|
|
|
+// 操作第一个工作表
|
|
|
+$objSheet = $objPHPExcel->setActiveSheetIndex(0);
|
|
|
+$objSheet->setTitle('用户列表'); // 工作表名称
|
|
|
+
|
|
|
+// 设置表头
|
|
|
+$objSheet->setCellValue('A1', '用户ID')
|
|
|
+ ->setCellValue('B1', '姓名')
|
|
|
+ ->setCellValue('C1', '手机号')
|
|
|
+ ->setCellValue('D1', '注册时间');
|
|
|
+
|
|
|
+// 模拟数据填充(示例数据)
|
|
|
+$data = [
|
|
|
+ [1001, '张三', '13800138000', '2025-01-01'],
|
|
|
+ [1002, '李四', '13912345678', '2025-02-15']
|
|
|
+];
|
|
|
+
|
|
|
+// 从第二行开始写入数据
|
|
|
+$rowIndex = 2;
|
|
|
+foreach ($data as $row) {
|
|
|
+ $objSheet->setCellValue('A'.$rowIndex, $row[0])
|
|
|
+ ->setCellValue('B'.$rowIndex, $row[1])
|
|
|
+ ->setCellValue('C'.$rowIndex, $row[2])
|
|
|
+ ->setCellValue('D'.$rowIndex, $row[3]);
|
|
|
+ $rowIndex++;
|
|
|
+}
|
|
|
+
|
|
|
+// 强制设置手机号为文本格式(避免科学计数法)
|
|
|
+ $objSheet->getStyle('C2:C'.$rowIndex)
|
|
|
+ ->getNumberFormat()
|
|
|
+ ->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
|
|
|
+
|
|
|
+ // 设置HTTP头直接下载文件
|
|
|
+ header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
|
|
|
+ header('Content-Disposition: attachment;filename="user_export.xlsx"');
|
|
|
+ header('Cache-Control: max-age=0');
|
|
|
+
|
|
|
+ // 生成Excel文件并输出到浏览器
|
|
|
+ $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
|
|
|
+ $objWriter->save('php://output');
|
|
|
+ exit;
|
|
|
+ }
|
|
|
+}
|