본문으로 바로가기

excel upload 샘플

category [ Web 관련 ]/php 2020. 5. 21. 12:28

PHPExcel-1.8.zip
4.96MB

 

excel_up.php

<html>
<head>
    <title>:: PHPExcel 샘플 ::</title>
</head>
<form enctype="multipart/form-data" action="./excel_up_ok.php" method="post">
    <table border="1">
        <tr>
            <th style="background-color:#DCDCDC">파일</th>
            <td><input type="file" name="excelFile"/></td>
        </tr>
        <tr>
            <th style="background-color:#DCDCDC">등록</th>
            <td style="text-align:center;"><input type="submit" value="업로드"/></td>
        </tr>
</form>
</html>

 

excel_up_ok.php

<?
include_once("../plugin/PHPExcel-1.8/Classes/PHPExcel.php");

$objPHPExcel = new PHPExcel();

// 엑셀 데이터를 담을 배열을 선언한다.
$allData = array();

// 파일의 저장형식이 utf-8일 경우 한글파일 이름은 깨지므로 euc-kr로 변환해준다.
$filename = iconv("UTF-8", "EUC-KR", $_FILES['excelFile']['tmp_name']);

try {

    // 업로드한 PHP 파일을 읽어온다.
    $objPHPExcel = PHPExcel_IOFactory::load($filename);

    $extension = strtoupper(pathinfo($filename, PATHINFO_EXTENSION));
    $sheetsCount = $objPHPExcel -> getSheetCount();

    // 시트Sheet별로 읽기
    for($sheet = 0; $sheet < $sheetsCount; $sheet++) {

        $objPHPExcel -> setActiveSheetIndex($sheet);
        $activesheet = $objPHPExcel -> getActiveSheet();
        $highestRow = $activesheet -> getHighestRow();          // 마지막 행
        $highestColumn = $activesheet -> getHighestColumn();    // 마지막 컬럼

        // 한줄읽기
        for($row = 1; $row <= $highestRow; $row++) {

            // $rowData가 한줄의 데이터를 셀별로 배열처리 된다.
            $rowData = $activesheet -> rangeToArray("A" . $row . ":" . $highestColumn . $row, NULL, TRUE, FALSE);

            // $rowData에 들어가는 값은 계속 초기화 되기때문에 값을 담을 새로운 배열을 선안하고 담는다.
            $allData[$row] = $rowData[0];

            //echo $rowData[0];
        }
    }
} catch(exception $exception) {
    echo $exception;
}

//echo "<pre>";
//print_r($allData);
//echo "</pre>";


foreach($allData as $key=>$value){
    //echo $key . " : " . $value . "</br>";

    foreach($value as $key2=>$value2){
        echo $key2 . " : " . $value2 . "</br>";
    }

    echo "</br>";
}

echo "</br></br></br></br>";



foreach($allData as $key=>$value){
    echo $value[0]."/".$value[1]."</br>";
}

 

 

github.com/PHPOffice/PHPExcel

 

PHPOffice/PHPExcel

ARCHIVED. Contribute to PHPOffice/PHPExcel development by creating an account on GitHub.

github.com

 

'[ Web 관련 ] > php' 카테고리의 다른 글

php 어제 날짜 구하기  (0) 2020.09.02
변수 타입 확인하기  (0) 2020.07.03
php + rocketchat api 사용  (0) 2020.05.20
php + python 코드 실행  (0) 2020.03.20
php curl에서 proxy 사용 + 이미지를 Base64 변환  (0) 2020.03.19