www.2527.com_澳门新葡8455手机版_新京葡娱乐场网址_
做最好的网站

ThinkPHP中使用PHPEXCEL举行excel导出www.2527.com

2019-06-22 01:33 来源:未知

至于选拔PHPEXCEL,首先要求把下载好的文书更名称叫Excel(PHPExcel.php  PHPExcel文件)放入ThinkPHP包的Vendor文件夹中,算是导入了贰个新类。

记得在以前写了个导出excel结果完毕了导出excel可是导出来的excel的格式特别的羞耻无法举行美化的。最原始的导出excel的法门正是应用header()函数举办文件。其实phpexcel也是利用header()函数实行导出的。只可是是多了花旗国的进度而以。不罗嗦了贴出代码。

if(!function_exists('exportExcel')){
//导出excel文件
function exportExcel($datas, $save_method='http', $save_path=''){
set_time_limit;
error_reporting;
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
$excel_title = '箱子详细情形列表'.date('YmdHis',time;
$code = $datas['code'];
$totalBox = $datas['totalBox'];
$totalPackages = $datas['totalPackages'];
$totalWeight = $datas['totalWeight'];
$data = $datas['data'];
require_once ROOT_PATH.'/Classes/PHPExcel.php';
$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()
->setCreator("Maarten Balliauw")
->setLastModifiedBy("Maarten Balliauw")
->setTitle("Office 2007 XLSX Test Document")
->setSubject("Office 2007 XLSX Test Document")
->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
->setKeywords("office 2007 openxml php")
->setCategory("Test result file");
$row = array('A','B','C','D','E','F','G','H');
// 设置列宽
$objPHPExcel->getActiveSheet()->getColumnDimension->setWidth;
$objPHPExcel->getActiveSheet()->getColumnDimension->setWidth;
$objPHPExcel->getActiveSheet()->getColumnDimension->setWidth;
$objPHPExcel->getActiveSheet()->getColumnDimension->setWidth;
$objPHPExcel->getActiveSheet()->getColumnDimension->setWidth;
$objPHPExcel->getActiveSheet()->getColumnDimension->setWidth;
$objPHPExcel->getActiveSheet()->getColumnDimension->setWidth;
$objPHPExcel->getActiveSheet()->getColumnDimension->setWidth;
$styleThinBlackBorderOutline = array(
'borders' => array (
'outline' => array (
'style' => PHPExcel_Style_Border::BORDER_THIN,
'color' => array ('argb' => 'FF000000'), ),
),
);
for($i=0;$i<count;$i ){
//设置边框
for ($a = 1; $a < 4; $a ) {
$objPHPExcel->getActiveSheet()->getStyle($row[$i] . $a)->applyFromArray($styleThinBlackBorderOutline);
}
//设置居中
for($j=1;$j<500;$j ){
$objPHPExcel->getActiveSheet()->getStyle($row[$i].$j)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle($row[$i].$j)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
}
//设置背景观
$objPHPExcel->getActiveSheet->getStyle($row[$i].'1')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
$objPHPExcel->getActiveSheet->getStyle($row[$i].'1')->getFill()->getStartColor()->setARGB('00FFFF00');
$objPHPExcel->getActiveSheet->getStyle($row[$i].'2:'.$row[$i].'3')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
$objPHPExcel->getActiveSheet->getStyle($row[$i].'2:'.$row[$i].'3')->getFill()->getStartColor()->setARGB('00DBE5F1');
//设置换行
$objPHPExcel->getActiveSheet()->getStyle($row[$i].'2')->getAlignment()->setWrapText;
}
// 设置行高
$objPHPExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight;
$objPHPExcel->getActiveSheet()->getRowDimension->setRowHeight;
//定义表的前三行
$objPHPExcel->getActiveSheet()
->mergeCells
->mergeCells
->mergeCells
->mergeCells
->mergeCells
->mergeCells
->mergeCells
->mergeCells;
$objPHPExcel->setActiveSheetIndex
->setCellValue('A1', '客户代码 Code:')
->setCellValue('C1', $code)
->setCellValue('E1', '日期 Date:')
->setCellValue('G1', date('Y-m-d H:i:s',time
->setCellValue('A2', '总箱数:'.$totalBox."n".'Total box')
->setCellValue('A3', '箱号')
->setCellValue('B2', '转运单号'."n".'Tracking Number')
->setCellValue('C2', '订单号')
->setCellValue('D2', '总包裹数:'.$totalPackages."www.2527.com,n".'Total Packages')
->setCellValue('D3', '包裹重量
->setCellValue('E2', '总箱重:'.$totalWeight."n".'Total Weight')
->setCellValue('E3', '箱重
->setCellValue('F2', '总箱体积重'."n".'(长*宽*高cm/5000)')
->setCellValue('F3', '箱体积')
->setCellValue('G2', '头程'."n".'发货情势')
->setCellValue('H2', '跟踪号'."n".'Tracking Number');
//管理前面包车型的士逻辑
//当前所在的行数
$num = 4;
foreach($data as $k => $v){
//具体订单个数
$len = count;
$mergeLen = $num $len-4;
$objPHPExcel->getActiveSheet()
->mergeCells('E'.$num.':E'.$mergeLen)
->mergeCells('F'.$num.':F'.$mergeLen)
->mergeCells('G'.$num.':G'.$mergeLen)
->mergeCells('H'.$num.':H'.$mergeLen);
$objPHPExcel->setActiveSheetIndex()
->setCellValue('A'.$num, $v['boxId'])
->setCellValue('E'.$num, $v['weightss'])
->setCellValue('G'.$num, $v['carrier_company']);
for($i=0;$i<$len-3;$i ){
$objPHPExcel->setActiveSheetIndex()
->setCellValue('A'.$num, $v['boxId'])
->setCellValueExplicit('B'.$num, $v[$i]['tracknumber'],PHPExcel_Cell_DataType::TYPE_STRING)
->setCellValue('C'.$num, $v[$i]['ebay_id'])
->setCellValue('D'.$num, $v[$i]['weight']);
$num ;
}
}
$objPHPExcel->getActiveSheet()->setTitle($excel_title);
$objPHPExcel->setActiveSheetIndex;
$filename = $excel_title.'.xls';
if($save_method == 'file'){
//保存到文件
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$full_path = $save_path.$filename;
//当地利用,线上批注
//$full_path = iconv('utf-8', 'gbk//IGNORE', $full_path);
if(is_file($full_path)) unlink($full_path);
$objWriter->save($full_path);
return $full_path;
}else{
ob_end_clean();
//直接在浏览器输出
header('Content-Type: application/vnd.ms-excel');
header("Content-Disposition: attachment;filename={$filename}");
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
}
}
}

附带正是关于导出函数的采纳,具体代码如下所示:

html部分

 public function imorder()
      {     
        $list = $_SESSION['depart'];  //通过SESSION传递过来的已经在数据库中询问出来的数据集。
       $num=count($list);  // 对查询出来的数码开始展览总计。
        error_reporting(E_ALL);
        ini_set('display_errors', TRUE);
        ini_set('display_startup_errors', TRUE);
  
 
   Vendor('Excel.PHPExcel');
   Vendor('Excel.PHPExcel.IOFactory');
   Vendor('Excel.PHPExcel.Reader.Excel5');
        $objPHPExcel = new PHPExcel();
        $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
                                     ->setLastModifiedBy("Maarten Balliauw")
                                     ->setTitle("Office 2007 XLSX Test Document")
                                     ->setSubject("Office 2007 XLSX Test Document")
                                     ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
                                     ->setKeywords("office 2007 openxml php")
                                     ->setCategory("Test result file");  
        $objPHPExcel->setActiveSheetIndex(0)
                    ->setCellValue('A1', '学工号')   //明显导出的表中的每一列的名称。
                    ->setCellValue('B1', '姓名')
                    ->setCellValue('C1', '班级名称')
     ->setCellValue('D1', '消费日期')
     ->setCellValue('E1', '消费时间')
        ->setCellValue('F1', '消费年度')
     ->setCellValue('G1', '消费金额')
        ->setCellValue('H1', '余额')
     ->setCellValue('I1', '操作员');
  for ($i=0; $i < $num; $i ) {
   $n=$i 2;
        $objPHPExcel->setActiveSheetIndex(0)
                    ->setCellValue('A'.$n, $list[$i]['STUEMPNO'])   //每一列对应的数值。
                    ->setCellValue('B'.$n, $list[$i]['CUSTNAME'])
                    ->setCellValue('C'.$n, $list[$i]['CLASSNAME'])
     ->setCellValue('D'.$n, $list[$i]['XFDATE'])
     ->setCellValue('E'.$n, $list[$i]['XFTIME'])
     ->setCellValue('F'.$n, $list[$i]['NDNAME'])
     ->setCellValue('G'.$n, $list[$i]['JE'])
        ->setCellValue('H'.$n, $list[$i]['YE'])
     ->setCellValue('I'.$n, $list[$i]['FIRSTNAME']);
                 }
        $objPHPExcel->getActiveSheet()->setTitle('title');
        $objPHPExcel->setActiveSheetIndex(0);
        spl_autoload_register(array('Think','autoload'));
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="文件名.xls"');  //导出excel文件的名字是一度定义过的。
        header('Cache-Control: max-age=0');
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output');
        exit;
      }

 代码如下

 

    <div><a href="/esoxue.php/Article/download/"/>导出Excel</a></div>


实践代码部分

 代码如下

 

    public function download()

    {

    import("ORG.Excel.PHPExcel");

    $objPHPExcel = new PHPExcel();

    $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")

    ->setLastModifiedBy("Maarten Balliauw")

    ->setTitle("Office 2007 XLSX Test Document")

    ->setSubject("Office 2007 XLSX Test Document")

    ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")

    ->setKeywords("office 2007 openxml php")

    ->setCategory("Test result file");

    $list=M("Test");

    $rs=$list->select();

    $i=2;

    $objPHPExcel->setActiveSheetIndex(0)

                ->setCellValue('A1', '姓名')

                ->setCellValue('B1', '年龄')

                ->setCellValue('C1', '性别')

                ->setCellValue('D1', '身份ID编号')

                ->setCellValue('E1', '国籍')

                ->setCellValue('F1', '民族')

                ->setCellValue('G1', '详细地址');

    $objPHPExcel->setActiveSheetIndex(0);

    foreach($rs as $k=>$v){

    $objPHPExcel->setActiveSheetIndex(0)

    ->setCellValue('A'.$i, $v['name'])

                ->setCellValue('B'.$i, $v['old'])

                ->setCellValue('C'.$i, $v['sex'])

                ->setCellValue('D'.$i, $v['sid'])

                ->setCellValue('E'.$i, $v['guoji'])

                ->setCellValue('F'.$i, $v['minzu'])

                ->setCellValue('G'.$i, $v['address']);

    $i ;

    }

    $objPHPExcel->getActiveSheet()->setTitle('student');//设置sheet标签的名称

    $objPHPExcel->setActiveSheetIndex(0);

    ob_end_clean();  //清空缓存 

    header("Pragma: public");

    header("Expires: 0");

    header("Cache-Control:must-revalidate,post-check=0,pre-check=0");

    header("Content-Type:application/force-download");

    header("Content-Type:application/vnd.ms-execl");

    header("Content-Type:application/octet-stream");

    header("Content-Type:application/download");

    header('Content-Disposition:attachment;filename=11.xls');//设置文件的名称

    header("Content-Transfer-Encoding:binary");

    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');

    $objWriter->save('php://output');

    exit;

    }

效果图

 

www.2527.com 1

www.2527.com 2

TAG标签:
版权声明:本文由澳门新葡8455手机版发布于www.2527.com,转载请注明出处:ThinkPHP中使用PHPEXCEL举行excel导出www.2527.com