国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

PHP實(shí)現(xiàn)生成Excel文件并導(dǎo)出的示例詳解

瀏覽:205日期:2022-06-06 18:41:18

在現(xiàn)在的項(xiàng)目里,不管是電商項(xiàng)目還是別的項(xiàng)目,在管理端都會(huì)有導(dǎo)出的功能,比方說(shuō)訂單表導(dǎo)出,用戶表導(dǎo)出,業(yè)績(jī)表導(dǎo)出。這些都需要提前生成excel表,然后在導(dǎo)出,實(shí)際上是在代碼里生成一張excel表,然后通過(guò)下載api進(jìn)行導(dǎo)出的。好了 先給大家講一下示例

利用php導(dǎo)出excel我們大多會(huì)直接生成.xls文件,這種方便快捷。

首先我們先在項(xiàng)目中引入幾個(gè)類

use \PhpOffice\PhpSpreadsheet\Spreadsheet;use \PhpOffice\PhpSpreadsheet\IOFactory;use \PhpOffice\PhpSpreadsheet\Cell\Coordinate;use PhpOffice\PhpSpreadsheet\Style\Border;use PhpOffice\PhpSpreadsheet\Style\Alignment;

然后我們?cè)谶M(jìn)行封裝一個(gè)生成并導(dǎo)出excel表的方法,這里我們用訂單表做示例

//訂單信息導(dǎo)出excel    public function order_outputProjectExcel($info){$newExcel = new Spreadsheet();//創(chuàng)建一個(gè)新的excel文檔$objSheet = $newExcel->getActiveSheet();//獲取當(dāng)前操作sheet的對(duì)象$date = date("Ymd",time());$name = "訂單信息表";$objSheet->setTitle($name);//設(shè)置當(dāng)前sheet的標(biāo)題 //樣式設(shè)置 - 合并和拆分$objSheet->mergeCells("A1:P1"); //合并單元格//$sheet -> unmergeCells("C3:G3"); //拆分單元格$objSheet->setCellValue("A1",$name); //設(shè)置第一欄的中文標(biāo)題$objSheet->setCellValue("A2", "編號(hào)")    ->setCellValue("B2", "商品圖片")    ->setCellValue("C2", "商品名稱")    ->setCellValue("D2", "會(huì)員編號(hào)")    ->setCellValue("E2", "直屬")    ->setCellValue("F2", "非直屬")    ->setCellValue("G2", "訂單號(hào)")    ->setCellValue("H2", "商品單價(jià)")    ->setCellValue("I2", "實(shí)付金額")    ->setCellValue("J2", "商品傭金")    ->setCellValue("K2", "會(huì)員傭金")    ->setCellValue("L2", "直屬傭金")    ->setCellValue("M2", "非直屬傭金")    ->setCellValue("N2", "支付時(shí)間")    ->setCellValue("O2", "支付渠道")    ->setCellValue("P2", "訂單狀態(tài)"); //寫入數(shù)據(jù)$dataCount = count($info);$k = 2;if($dataCount == 0){    exit;}else{    for ($i=0;$i<$dataCount;$i++){$k = $k + 1;$order=$i+1;$objSheet->setCellValue("A" . $k, $info[$i]["id"])    ->setCellValue("B" . $k, $info[$i]["goods_image"])    ->setCellValue("C" . $k, $info[$i]["goods_name"])    ->setCellValue("D" . $k, $info[$i]["user_num"])    ->setCellValue("E" . $k, $info[$i]["user_upteam"])    ->setCellValue("F" . $k, $info[$i]["user_un_upteam"])    ->setCellValue("G" . $k, $info[$i]["order_id"])    ->setCellValue("H" . $k, $info[$i]["goods_price"])    ->setCellValue("I" . $k, $info[$i]["payment"])    ->setCellValue("J" . $k, $info[$i]["goods_yongjin"])    ->setCellValue("K" . $k, $info[$i]["user_yongjin"])    ->setCellValue("L" . $k, $info[$i]["user_up_yongjin"])    ->setCellValue("M" . $k, $info[$i]["user_un_upyongjin"])    ->setCellValue("N" . $k, $info[$i]["paymenttime"])    ->setCellValue("O" . $k, $info[$i]["pay_way_ch"])    ->setCellValue("P" . $k, $info[$i]["status_ch"]);    }} //設(shè)定樣式//所有sheet的表頭樣式 加粗$font = [    "font" => ["bold" => true,"size" => 14,    ],];$objSheet->getStyle("A1:P1")->applyFromArray($font); //樣式設(shè)置 - 水平、垂直居中$styleArray = [    "alignment" => ["horizontal" => Alignment::HORIZONTAL_CENTER,"vertical" => Alignment::VERTICAL_CENTER    ],];$objSheet->getStyle("A1:P2")->applyFromArray($styleArray); //所有sheet的內(nèi)容樣式-加黑色邊框$borders = [    "borders" => ["outline" => [    "borderStyle" => Border::BORDER_THIN,    "color" => ["argb" => "000000"],],"inside" => [    "borderStyle" => Border::BORDER_THIN,]    ],];$objSheet->getStyle("A1:P".$k)->applyFromArray($borders); //設(shè)置寬度$cell = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P"];foreach($cell as $k=>$v){    $objSheet->getColumnDimension($v)->setWidth(20); //    $objSheet->getColumnDimension($v)->setAutoSize(true);} $this->downloadExcel($newExcel,$name,"Xlsx");    }                    //下載    private function downloadExcel($newExcel,$filename,$format)    {ob_end_clean();ob_start();// $format只能為 Xlsx 或 Xlsif ($format == "Xlsx") {    header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");} elseif ($format == "Xls") {    header("Content-Type: application/vnd.ms-excel");}//  strtolower($format)header("Content-Disposition: attachment;filename="    . $filename . "." . strtolower($format));header("Cache-Control: max-age=0");$objWriter = IOFactory::createWriter($newExcel, $format);$objWriter->save("php://output");//通過(guò)php保存在本地的時(shí)候需要用到// $objWriter->save($dir."/demo.xlsx"); //以下為需要用到IE時(shí)候設(shè)置// If you"re serving to IE 9, then the following may be needed//header("Cache-Control: max-age=1");// If you"re serving to IE over SSL, then the following may be needed//header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past//header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified//header("Cache-Control: cache, must-revalidate"); // HTTP/1.1//header("Pragma: public"); // HTTP/1.0exit;    }

到這一步其實(shí)就已經(jīng)成功了95%了。剩下的5%只需要你調(diào)用上面的方法傳入正確的參數(shù)即可

model("Apimodel")->order_outputProjectExcel($info);

這里的$info是你的訂單詳情。必須要做時(shí)間篩選,如果不做時(shí)間篩選的話 數(shù)據(jù)量一多,你的系統(tǒng)會(huì)崩的。

這里的for循環(huán)就是你傳入的 $info。 如果說(shuō)你的 $info 是空的 那么導(dǎo)出的excel打開的時(shí)候還會(huì)出現(xiàn)

for ($i=0;$i<$dataCount;$i++){    $k = $k + 1;    $order=$i+1;    $objSheet->setCellValue("A" . $k, $info[$i]["id"])->setCellValue("B" . $k, $info[$i]["goods_image"])->setCellValue("C" . $k, $info[$i]["goods_name"])->setCellValue("D" . $k, $info[$i]["user_num"])->setCellValue("E" . $k, $info[$i]["user_upteam"])->setCellValue("F" . $k, $info[$i]["user_un_upteam"])->setCellValue("G" . $k, $info[$i]["order_id"])->setCellValue("H" . $k, $info[$i]["goods_price"])->setCellValue("I" . $k, $info[$i]["payment"])->setCellValue("J" . $k, $info[$i]["goods_yongjin"])->setCellValue("K" . $k, $info[$i]["user_yongjin"])->setCellValue("L" . $k, $info[$i]["user_up_yongjin"])->setCellValue("M" . $k, $info[$i]["user_un_upyongjin"])->setCellValue("N" . $k, $info[$i]["paymenttime"])->setCellValue("O" . $k, $info[$i]["pay_way_ch"])->setCellValue("P" . $k, $info[$i]["status_ch"]);  }

最后就是這樣的效果

到此這篇關(guān)于PHP實(shí)現(xiàn)生成Excel文件并導(dǎo)出的示例詳解的文章就介紹到這了,更多相關(guān)PHP生成導(dǎo)出Excel內(nèi)容請(qǐng)搜索以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持!

標(biāo)簽: PHP
主站蜘蛛池模板: 永定县| 泾川县| 靖远县| 元朗区| 云梦县| 隆安县| 毕节市| 晋州市| 蒙城县| 石台县| 衢州市| 元江| 枣强县| 松阳县| 嘉义市| 轮台县| 阿图什市| 吴川市| 岳阳市| 沁水县| 沙湾县| 抚州市| 荆州市| 马关县| 兴国县| 蕲春县| 淄博市| 中牟县| 乐山市| 元朗区| 黎城县| 秦皇岛市| 彰化县| 西宁市| 十堰市| 咸阳市| 随州市| 文登市| 麟游县| 准格尔旗| 六枝特区|