본문으로 바로가기

PHPExcel

category Backend/PHP 2020. 11. 5. 13:05
728x90
반응형
<!DOCTYPE html>
<html lang="ko">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>

<body>
    <form id="frmExcel" action="excelAddUpload.php" method="POST" enctype="multipart/form-data">
        <table class="table table_basic horizontal_border panelTabel">
            <tbody>
                <tr>
                    <th width="15%" height="30">엑셀 업로드</th>
                    <td width="50%" colspan="42">
                        <div class="fileBox">
                            <input type="file" id="excelFile" class="upload-hidden" accept=".xls, .xlsx" name="excelFile" style="margin-left: 10px;">
                            <input type="button" value="추가하기" class="btn btn_default" id="frmSubmit" style="margin-top: 10px;">
                        </div>
                    </td>
                </tr>
            </tbody>
        </table>
    </form>
    <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
    <script>
        $(function() {
            $("#excelFile").change(function() {
                const target = $(this).val();
                const ext = target.substring(target.lastIndexOf(".")+1);

                if(ext !== "xls" && ext !== "xlsx") {
                    alert("엑셀만 업로드 가능합니다.");
                    $(this).val("");
                }
            });

            $("#frmSubmit").click(function() {
                const target = $("#excelFile").val();
                const ext = target.substring(target.lastIndexOf(".")+1);

                if(ext !== "xls" && ext !== "xlsx") {
                    alert("엑셀만 업로드 가능합니다.");
                    $("#excelFile").val("");
                } else {
                    $("#frmExcel").submit();
                }
            });
        });
    </script>
</body>
</html>
<?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];
          }
    }
} catch(exception $exception) {
    echo $exception;
}

echo("<pre>".print_r($allData, true)."</pre>");
?>
728x90
반응형

'Backend > PHP' 카테고리의 다른 글

PHP에서 Python 파일 실행하기  (4) 2020.12.30
XML to JSON (NOCDATA)  (0) 2020.09.21
MYSQL PASSWORD와 비밀번호 비교  (0) 2020.09.02
CURL SSL 무시 및 multipart form-data POST 요청  (0) 2020.07.31
금액을 한글로 변환  (0) 2020.07.10