ㅡ
php에서 엑셀파일을 불러와 데이터형식으로 변환한다음 db에 저장을 하고 싶었다.
블로그를 찾던 도중 소스코드를 발견해서 그대로 붙여넣었는데??
위사진처럼 에러가 떳다.
삽질을 한 20분 하다가 알게되었다.
일단 블로그에서 줍줍한코드
<html>
<?require_once $_SERVER["DOCUMENT_ROOT"]."/lib/PHPExcel/Classes/PHPExcel.php";?>
<html>
<head>
<title>:: PHPExcel 파일읽기 ::</title>
</head>
<form enctype="multipart/form-data" action="./excel_read.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>
<php>
<?
require_once $_SERVER["DOCUMENT_ROOT"]."/lib/PHPExcel/Classes/PHPExcel.php";
require_once $_SERVER["DOCUMENT_ROOT"]."/lib/PHPExcel/Classes/PHPExcel/IOFactory.php";
$objPHPExcel = new PHPExcel();
$allData = array();
$filename = $_FILES['excelFile']['name']; // 읽어들일 엑셀 파일의 경로와 파일명을 지정한다.
/*$filename = iconv("UTF-8", "EUC-KR", $_FILES['excelFile']['name']);*/
try {
// 업로드한 PHP 파일을 읽어온다.
$objPHPExcel = PHPExcel_IOFactory::load($filename);
$sheetsCount = $objPHPExcel -> getSheetCount();
// 시트Sheet별로 읽기
for($i = 0; $i < $sheetsCount; $i++) {
$objPHPExcel -> setActiveSheetIndex($i);
$sheet = $objPHPExcel -> getActiveSheet();
$highestRow = $sheet -> getHighestRow(); // 마지막 행
$highestColumn = $sheet -> getHighestColumn(); // 마지막 컬럼
// 한줄읽기
for($row = 1; $row <= $highestRow; $row++) {
// $rowData가 한줄의 데이터를 셀별로 배열처리 된다.
$rowData = $sheet -> rangeToArray("A" . $row . ":" . $highestColumn . $row, NULL, TRUE, FALSE);
// $rowData에 들어가는 값은 계속 초기화 되기때문에 값을 담을 새로운 배열을 선안하고 담는다.
$allData[$row] = $rowData[0];
}
}
} catch(exception $e) {
echo $e;
}
print_r($allData);exit;
?>
위처럼 했는데 작동이 되지않았다.
이유는 $filename의 경로문제엿다.
아래는 form타입으로 file을 submit 받을 때 배열이다.
: $_FILES['userfile']['tmp_name'] - 웹 서버에 임시로 저장된 파일의 위치.
: $_FILES['userfile']['name'] - 사용자 시스템에 있을 때의 파일 이름.
: $_FILES['userfile']['size'] - 파일의 바이트 크기.
: $_FILES['userfile']['type'] - 파일의 MIME 타입을 가리킴. 예를 들어, text/plain이나 image/gif.
: $_FILES['userfile']['error'] - 파일 업로드할 때 일어난 오류 코드를 알려주는데, PHP 4.2.0에서 추가됨.
$objPHPExcel = PHPExcel_IOFactory::load($filename);
$filename에는 파일의 경로가 필요하다.
form 타입으로 받은 file데이터는 tmp_name 이라는 경로에 임시로 저장이 되어있다.
수정한코드
<?
require_once $_SERVER["DOCUMENT_ROOT"]."/lib/PHPExcel/Classes/PHPExcel.php";
require_once $_SERVER["DOCUMENT_ROOT"]."/lib/PHPExcel/Classes/PHPExcel/IOFactory.php";
$objPHPExcel = new PHPExcel();
$allData = array();
$filename = $_SERVER["DOCUMENT_ROOT"].'/lib/sample.xlsx'; // 읽어들일 엑셀 파일의 경로와 파일명을 지정한다.
/*$filename = iconv("UTF-8", "EUC-KR", $_FILES['excelFile']['tmp_name']);*/
try {
// 업로드한 PHP 파일을 읽어온다.
$objPHPExcel = PHPExcel_IOFactory::load($_FILES['excelFile']['name']);
$sheetsCount = $objPHPExcel -> getSheetCount();
// 시트Sheet별로 읽기
for($i = 0; $i < $sheetsCount; $i++) {
$objPHPExcel -> setActiveSheetIndex($i);
$sheet = $objPHPExcel -> getActiveSheet();
$highestRow = $sheet -> getHighestRow(); // 마지막 행
$highestColumn = $sheet -> getHighestColumn(); // 마지막 컬럼
// 한줄읽기
for($row = 1; $row <= $highestRow; $row++) {
// $rowData가 한줄의 데이터를 셀별로 배열처리 된다.
$rowData = $sheet -> rangeToArray("A" . $row . ":" . $highestColumn . $row, NULL, TRUE, FALSE);
// $rowData에 들어가는 값은 계속 초기화 되기때문에 값을 담을 새로운 배열을 선안하고 담는다.
$allData[$row] = $rowData[0];
}
}
} catch(exception $e) {
echo $e;
}
print_r($allData);exit;
?>
잘된다.
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- EQ
- 데이터베이스#RDBMS#스키마
- ~`
- p
- js
- ㅂㅜ티
- RSS태그#지식
- 버블링
- 유입증가시키는방법
- onClick
- 배열선택자
- 선택자
- 다짐#개발자#Vlog
- domain
- 캡처링
- 검색결과
- data속성일때 선택자
- 티스토리
- 도메인이해
- ㅏ
- 네이버 노출
- PP
- 네이버
- react
- CheckBox
- jQuey
- Develop 사전
- CSP#콘텐츠보안정책#style코드 작성시
- 네이버노출
- 도메인개념
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
글 보관함