Home php教程 PHP源码 Using the PHPExcel library

Using the PHPExcel library

Nov 09, 2016 pm 02:46 PM

/**
 * 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


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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)