Home > php教程 > PHP源码 > Using the PHPExcel library

Using the PHPExcel library

大家讲道理
Release: 2016-11-09 14:46:48
Original
1311 people have browsed it

/**
 * excel 写入,基于PHPExcel库
 * @param array $data
 * @param $file
 * @return bool
 * @throws PHPExcel_Exception
 *
 */
function excel_insert(array $data,$file){
 
//    $data = [
//        '库房'=>[
//            ['库房编号','库房名词',1],
//            ['库房编号','库房名词',1],
//            ['库房编号','库房名词',1],
//            ['库房编号','库房名词',1],
//            ['库房编号','库房名词',1],
//        ],
//        '库房2'=>[
//            ['库房编号','库房名词',1],
//            ['库房编号','库房名词',1],
//            ['库房编号','库房名词',1],
//            ['库房编号','库房名词',1],
//            ['库房编号','库房名词',1],
//        ],
//    ];
//    excel_insert($data,'s.xlsx');
 
    if(!$data||!$file){
        return false;
    }
 
    $sheet_id = 0;
    //创建excel操作对象
    $objPHPExcel = new PHPExcel();
    //获得文件属性对象,给下文提供设置资源
    $objPHPExcel->getProperties()->setCreator("绵阳市碳素云信息技术有限责任公司")
        ->setLastModifiedBy("绵阳市碳素云信息技术有限责任公司")
        ->setTitle("Input_Goods_message")
        ->setSubject("主题1")
        ->setDescription("随便一个描述了")
        ->setKeywords("关键字 用空格分开")
        ->setCategory("分类 ");
    for($i=1;$i<count($data);$i++){
        $objPHPExcel->addSheet(new PHPExcel_Worksheet($objPHPExcel,&#39;sheet&#39;.$i));
    }
    foreach($data as $sheetName => $sheetData){
        $Sheet = $objPHPExcel->setActiveSheetIndex($sheet_id);
        $Sheet->setTitle($sheetName);
        $insert_id = 1;
        foreach($sheetData as $rowData){
            if(is_array($rowData)&&$rowData){
                foreach($rowData as $id => $cellData){
                    if(is_numeric($id)&&(is_string($cellData)||is_numeric($cellData))){
                        $Sheet->setCellValue(chr(65+$id).$insert_id,$cellData);
                    }else{
                        return false;
                    }
                }
                $insert_id++;
            }else{
                return false;
            }
        }
        $sheet_id++;
    }
    try{
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, &#39;Excel2007&#39;);
        $objWriter->save($file);
    }catch (Exception $e){
        return false;
    }
}
Copy after login

How to use:

$data = [
        &#39;库房&#39;=>[
            [&#39;库房编号&#39;,&#39;库房名词&#39;,1],
            [&#39;库房编号&#39;,&#39;库房名词&#39;,1],
            [&#39;库房编号&#39;,&#39;库房名词&#39;,1],
            [&#39;库房编号&#39;,&#39;库房名词&#39;,1],
            [&#39;库房编号&#39;,&#39;库房名词&#39;,1],
        ],
        &#39;库房2&#39;=>[
            [&#39;库房编号&#39;,&#39;库房名词&#39;,1],
            [&#39;库房编号&#39;,&#39;库房名词&#39;,1],
            [&#39;库房编号&#39;,&#39;库房名词&#39;,1],
            [&#39;库房编号&#39;,&#39;库房名词&#39;,1],
            [&#39;库房编号&#39;,&#39;库房名词&#39;,1],
        ],
    ];
    excel_insert($data,&#39;s.xlsx&#39;);
Copy after login


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template