Home > 类库下载 > PHP类库 > body text

PHPexcel excel export and import

高洛峰
Release: 2016-10-09 11:57:45
Original
1276 people have browsed it

Export and import form HTML code:

<p style="margin:10px 0"><a href="export.php" class="btn">导出</a></p> 
<form action="import.php" method="post" enctype="multipart/form-data"> 
    <p class="control-group"> 
        <label>Excel表格:</label> 
        <input type="file"  name="file"/> 
    </p> 
    <p class="control-group"> 
        <input type="submit"  value="导入" /> 
    </p> 
</form>
Copy after login

PHP excel export

$query = mysql_query("select * from user limit 50");  
$i =0; 
$list = array(); 
while($row=mysql_fetch_array($query)){  
    $list[$i][&#39;id&#39;] = $row[&#39;id&#39;];  
    $list[$i][&#39;username&#39;] = $row[&#39;username&#39;];  
    $list[$i][&#39;password&#39;] = $row[&#39;password&#39;];  
    $i++; 
}  
 
$title = array(&#39;ID&#39;, &#39;邮箱&#39;, &#39;密码&#39;); //设置要导出excel的表头 
exportExcel($list, &#39;素材火用户表&#39;, $title);
Copy after login

exportExcel method code:

function exportExcel($data, $savefile = null, $title = null, $sheetname = &#39;sheet1&#39;) { 
    require_once &#39;PHPExcel.class.php&#39;; 
    //若没有指定文件名则为当前时间戳 
    if (is_null($savefile)) { 
        $savefile = time(); 
    } 
    //若指字了excel表头,则把表单追加到正文内容前面去 
    if (is_array($title)) { 
        array_unshift($data, $title); 
    } 
    $objPHPExcel = new PHPExcel(); 
    //Excel内容 
    $head_num = count($data); 
 
    foreach ($data as $k => $v) { 
        $obj = $objPHPExcel->setActiveSheetIndex(0); 
        $row = $k + 1; //行 
        $nn = 0; 
 
        foreach ($v as $vv) { 
            $col = chr(65 + $nn); //列 
            $obj->setCellValue($col . $row, $vv); //列,行,值 
            $nn++; 
        } 
    } 
    //设置列头标题 
    for ($i = 0; $i < $head_num - 1; $i++) { 
        $alpha = chr(65 + $i); 
        $objPHPExcel->getActiveSheet()->getColumnDimension($alpha)->setAutoSize(true); //单元宽度自适应  
        $objPHPExcel->getActiveSheet()->getStyle($alpha . &#39;1&#39;)->getFont()->setName("Candara");  //设置字体 
        $objPHPExcel->getActiveSheet()->getStyle($alpha . &#39;1&#39;)->getFont()->setSize(12);  //设置大小 
        $objPHPExcel->getActiveSheet()->getStyle($alpha . &#39;1&#39;)->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_BLACK); //设置颜色 
        $objPHPExcel->getActiveSheet()->getStyle($alpha . &#39;1&#39;)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //水平居中 
        $objPHPExcel->getActiveSheet()->getStyle($alpha . &#39;1&#39;)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); //垂直居中 
        $objPHPExcel->getActiveSheet()->getStyle($alpha . &#39;1&#39;)->getFont()->setBold(true); //加粗 
    } 
 
    $objPHPExcel->getActiveSheet()->setTitle($sheetname); //题目 
    $objPHPExcel->setActiveSheetIndex(0); //设置当前的sheet   
    header(&#39;Content-Type: application/vnd.ms-excel&#39;); 
    header(&#39;Content-Disposition: attachment;filename="&#39; . $savefile . &#39;.xls"&#39;);//文件名称 
    header(&#39;Cache-Control: max-age=0&#39;); 
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, &#39;Excel5&#39;); //Excel5 
    $objWriter->save(&#39;php://output&#39;); 
}
Copy after login

PHP excel import

$tmp = $_FILES[&#39;file&#39;][&#39;tmp_name&#39;]; 
if (empty($tmp)) { 
    echo &#39;请选择要导入的Excel文件!&#39;; 
    exit; 
} 
 
$save_path = "uploads/"; 
$filename = $save_path . date(&#39;Ymdhis&#39;) . ".xls"; //上传后的文件保存路径和名称  
if (copy($tmp, $filename)) { 
    require_once &#39;PHPExcel.class.php&#39;; 
    require_once &#39;PHPExcel/Reader/Excel5.php&#39;; 
 
 
    $PHPReader = new PHPExcel_Reader_Excel5(); //PHPExcel_Reader_Excel2007 PHPExcel_Reader_Excel5 
    //载入文件 
    $PHPExcel = $PHPReader->load($filename); 
 
    //获取表中的第一个工作表,如果要获取第二个,把0改为1,依次类推 
    $currentSheet = $PHPExcel->getSheet(0); 
    //获取总列数 
    $allColumn = $currentSheet->getHighestColumn(); 
    //获取总行数 
    $allRow = $currentSheet->getHighestRow(); 
    //循环获取表中的数据,$currentRow表示当前行,从哪行开始读取数据,索引值从0开始 
    for ($currentRow = 1; $currentRow <= $allRow; $currentRow++) { 
        //从哪列开始,A表示第一列 
        for ($currentColumn = &#39;A&#39;; $currentColumn <= $allColumn; $currentColumn++) { 
            //数据坐标 
            $address = $currentColumn . $currentRow; 
            //读取到的数据,保存到数组$arr中 
            $data[$currentRow][$currentColumn] = $currentSheet->getCell($address)->getValue(); 
        } 
    } 
 
    $add_time = date(&#39;Y-m-d H:i:s&#39;, time()); 
    foreach ($data as $k => $v) { 
        if ($k > 1) { 
            $sql = "insert into user (username,password) values (&#39;" . $v[&#39;B&#39;] . "&#39;, &#39;" . $v[&#39;C&#39;] . "&#39;)"; 
 
            mysql_query($sql); 
        } 
    } 
 
    $sql = "SELECT * FROM user"; 
    $result = mysql_query($sql); 
    $tip = &#39;用户导入成功&#39; . &#39;,现在&#39; . mysql_num_rows($result) . &#39;条数据了!&#39;; 
    echo "<script>alert(&#39;" . $tip . "&#39;);history.go(-1);</script>"; 
    exit; 
}
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!