国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home 類庫下載 PHP類庫 PHPexcel excel export and import

PHPexcel excel export and import

Oct 09, 2016 am 11:57 AM

Export and import form HTML code:

<p style="margin:10px 0"><a href="export.php" class="btn">導(dǎo)出</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="導(dǎo)入" /> 
    </p> 
</form>

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;); //設(shè)置要導(dǎo)出excel的表頭 
exportExcel($list, &#39;素材火用戶表&#39;, $title);

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表頭,則把表單追加到正文內(nèi)容前面去 
    if (is_array($title)) { 
        array_unshift($data, $title); 
    } 
    $objPHPExcel = new PHPExcel(); 
    //Excel內(nèi)容 
    $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++; 
        } 
    } 
    //設(shè)置列頭標題 
    for ($i = 0; $i < $head_num - 1; $i++) { 
        $alpha = chr(65 + $i); 
        $objPHPExcel->getActiveSheet()->getColumnDimension($alpha)->setAutoSize(true); //單元寬度自適應(yīng)  
        $objPHPExcel->getActiveSheet()->getStyle($alpha . &#39;1&#39;)->getFont()->setName("Candara");  //設(shè)置字體 
        $objPHPExcel->getActiveSheet()->getStyle($alpha . &#39;1&#39;)->getFont()->setSize(12);  //設(shè)置大小 
        $objPHPExcel->getActiveSheet()->getStyle($alpha . &#39;1&#39;)->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_BLACK); //設(shè)置顏色 
        $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); //設(shè)置當前的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;); 
}

PHP excel import

$tmp = $_FILES[&#39;file&#39;][&#39;tmp_name&#39;]; 
if (empty($tmp)) { 
    echo &#39;請選擇要導(dǎo)入的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); 
    //獲取總列數(shù) 
    $allColumn = $currentSheet->getHighestColumn(); 
    //獲取總行數(shù) 
    $allRow = $currentSheet->getHighestRow(); 
    //循環(huán)獲取表中的數(shù)據(jù),$currentRow表示當前行,從哪行開始讀取數(shù)據(jù),索引值從0開始 
    for ($currentRow = 1; $currentRow <= $allRow; $currentRow++) { 
        //從哪列開始,A表示第一列 
        for ($currentColumn = &#39;A&#39;; $currentColumn <= $allColumn; $currentColumn++) { 
            //數(shù)據(jù)坐標 
            $address = $currentColumn . $currentRow; 
            //讀取到的數(shù)據(jù),保存到數(shù)組$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;用戶導(dǎo)入成功&#39; . &#39;,現(xiàn)在&#39; . mysql_num_rows($result) . &#39;條數(shù)據(jù)了!&#39;; 
    echo "<script>alert(&#39;" . $tip . "&#39;);history.go(-1);</script>"; 
    exit; 
}
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

Undress AI Tool

Undress AI Tool

Undress images for free

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.

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)

Hot Topics

PHP Tutorial
1502
276