thinkphp中使用phpexcel生成excel格式文件

2014-03-13 18:51 来源:www.chinab4c.com 作者:ecshop专家

vendor('PHPExcel.PHPExcel');
        $fileName = 'BOM-'.date('Y-m-d',time());
        $PHPExcel = new PHPExcel();
          $PHPExcel->createSheet();
        $subObject = $PHPExcel->getSheet(1);
        $subObject->setTitle('data');
        //填入表头
        $PHPExcel->getActiveSheet()->setCellValue('A1', '型号');
        $PHPExcel->getActiveSheet()->setCellValue('B1', '品牌');
        $PHPExcel->getActiveSheet()->setCellValue('C1', '数量');
        $PHPExcel->getActiveSheet()->setCellValue('D1', '用量');
        $PHPExcel->getActiveSheet()->setCellValue('E1', '包装');
        $PHPExcel->getActiveSheet()->setCellValue('F1', '价格');
        $PHPExcel->getActiveSheet()->setCellValue('G1', '交货时间');
        $PHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(20);
        //填入列表
        $k = 1;
        foreach ($b_item as $key => $v) {
            $k++;
            $PHPExcel->getActiveSheet()->setCellValue('A'.($k), $v['goods_name']);
            $PHPExcel->getActiveSheet()->setCellValue('B'.($k), $v['brand_name']);
            $PHPExcel->getActiveSheet()->setCellValue('C'.($k), $v['goods_number']);
            $PHPExcel->getActiveSheet()->setCellValue('D'.($k), $v['dosage']);
            $PHPExcel->getActiveSheet()->setCellValue('E'.($k), $v['package_info']);
            $PHPExcel->getActiveSheet()->setCellValue('F'.($k), $v['goods_price']);
            $PHPExcel->getActiveSheet()->setCellValue('G'.($k), $v['send_time']);
            $PHPExcel->getActiveSheet()->getRowDimension($k)->setRowHeight(20);
        }
       
       
        //设置单元格宽度
        /*$PHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(6);
        $PHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(15);
        $PHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(15);
        $PHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(15);
        $PHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(15);
        $PHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(15);
        $PHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(16);
        */
      
        //保存为2003格式
        $objWriter = new PHPExcel_Writer_Excel5($PHPExcel);
        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");
       
        //多浏览器下兼容中文标题
        $encoded_filename = urlencode($fileName);
        $ua = $_SERVER["HTTP_USER_AGENT"];
        if (preg_match("/MSIE/", $ua)) {
            header('Content-Disposition: attachment; filename="' . $encoded_filename . '.xls"');
        } else if (preg_match("/Firefox/", $ua)) {
            header('Content-Disposition: attachment; filename*="utf8\'\'' . $fileName . '.xls"');
        } else {
            header('Content-Disposition: attachment; filename="' . $fileName . '.xls"');
        }
       
        header("Content-Transfer-Encoding:binary");
        $objWriter->save('php://output');

     来源:http://www.chinab4c.com