HOME > 이용안내
이용안내
 
작성일 : 15-01-23 12:21
[스크립트언어] [PHP] php에서 Excel 파일 읽기/쓰기
 글쓴이 : 제이네트워크
조회 : 493,258  
[PHP] php에서 Excel 파일 읽기/쓰기


PHP - Excel 내려받기 
문서의 해드에 아래 소스를 추가합니다.
 
header("Content-type: application/vnd.ms-excel"); 
header("Content-Disposition: attachment; filename=파일명.xls"); 
header("Expires: 0"); 
header("Cache-Control: must-revalidate, post-check=0,pre-check=0"); 
header("Pragma: public"); 
 
위 파일을 추가 했을때 아래와 같은 오류가 나올경우 해경 방법
 
* 오류 메시지
Warning: Cannot modify header information - headers already sent by 
(output started at 파일경로명/php_excel/excel_export.php:16) 
in 파일경로명/php_excel/excel_export.php on line 70 
 
* 원인및 해결 방법
원인은 문서의 해드 아래에 해드 변화 소스를 입력했기 때문입니다.
 
즉 ) 아래와 같이 코딩하면 오류 메시지 나옵니다.
 
<html>
 <head>
소스코딩
</head>
 <body>
 
<? 
header("Content-type: application/vnd.ms-excel"); 
header("Content-Disposition: attachment; filename=파일명.xls"); 
header("Expires: 0"); 
header("Cache-Control: must-revalidate, post-check=0,pre-check=0"); 
header("Pragma: public"); 
?>
</body> 
</html>
 
* 올바른 사용예는 아래와 같습니다.
 
<?
header("Content-type: application/vnd.ms-excel"); 
header("Content-Disposition: attachment; filename=파일명.xls"); 
header("Expires: 0"); 
header("Cache-Control: must-revalidate, post-check=0,pre-check=0"); 
header("Pragma: public"); 
?>
<html>
<head>
소스코딩
</head>
<body>
 
</body>
</html>

=====================================================================================

[PHP 에서 Excel 파일 읽어 DB에 저장 방법]
 
 http://code.google.com/p/php-excel-reader/
 위 주소에서 php-excel-reader  를 다운로드 하여 사용.
 
 기본 사용 방법은 example.php를 참조하면 됩니다.
 
 엑셀 로단위로 읽어 DB에 저장하게 수정한 소스는 아래와같습니다.
 
<?php
error_reporting(E_ALL ^ E_NOTICE);
require_once 'excel_reader2.php';
$data = new Spreadsheet_Excel_Reader("파일명.xls");
?>
<html>
<head>
</head>
 
<body>
<?php 
  $server = "localhost"; //서버 이름
  $id    = ""; //db 접속 아이디
  $pass   = ""; //디비 접속 비밀번호
  $connect = mysql_connect($server,$id,$pass);
  mysql_select_db("dbname",$connect);
  
  
 // 엑셀 첫번째 sheet 행 개수
 $rowcount = $data->rowcount($sheet_index=0); 
 
 for($i=2 ;$i<=$rowcount; $i++){
  
   
 //val(행,열)
  $ID    = $data->val($i,4);
  
 $Point = $data->val($i,3);
   $QUE = "Sql Query";
     $ok = mysql_query($QUE , $connect) or die(db_error());
    } 
?>
</body>
</html>






Cloud server Streaming service Domain Cloud Firewall

제이네트워크 15-01-23 12:24
 
[mysql db 내용을 엑셀파일로 내려받기]

<table>
  <tr>
    <td>번호</td>
    <td>이름</td>
  </tr>
 
<?
$table = "테이블이름";
$sql = "select * from ".$table;
$query = mysql_qyery($sql);
 

///////////////////////////////////////////////////////////////////////// 엑셀얻기 시작
 $downfile= $table."_".date("Y-m-d").".xls";  // 다운받을 화일
 header( "Content-type: application/vnd.ms-excel" );
 header( "Content-Disposition: attachment; filename=".$downfile );
 header( "Content-Description: PHP4 Generated Data" );
 ///////////////////////////////////////////////////////////////////////// 엑셀얻기 끝
 
 
while($data = mysql_fetch_assoc($query)) {
 
?>
  <tr>
    <td><?=$data[번호필드]?></td>
    <td><?=$data[이름필드]?></td>
  </tr>
 <?
}
?>

</table>
제이네트워크 15-01-23 12:28
 
PHP header를 이용해서 PHP페이지를 엑셀 파일 형태로 내려받을 수 있는데요. 두 가지 방법이 있습니다.

 

1. xls로 내려받기

<?
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=파일명.xls");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0,pre-check=0");
header("Pragma: public");
?>
<table>
<?for($i=1;$i<10;$i++){?>
<tr>
<td>안녕하세요</td>
<td>반갑습니다.</td>
</tr>
<?}?>
</table>
* 헤더 아래에 있는 테이블 내용들이 엑셀로 출력됩니다.

2. csv로 내려받기

<?
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=파일명.csv");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0,pre-check=0");
header("Pragma: public");
?>
<?for($i=1;$i<10;$i++){?>
안녕하세요,반갑습니다.
<?}?>
</table>

* xls와는 다르게 csv는 ,(쉼표)로 열을 구분하고 줄바꿈으로 행을 구분합니다.
 
 

Total 377

번호 제   목   글쓴이 날짜 조회
17 [스크립트언어] [php]php5.3.x 버전에서 phpinfo 가 안보일 때 제이네트워크 2013-08-19 46241
16 [메일] 메일에러 리턴메세지 제이네트워크 2011-06-17 46355
15 [기타] 메일 첨부파일에 winmail.dat 파일만 첨부되었을때 확인방법 관리자 2012-01-30 46602
14 [리눅스서버] 리눅스 서비스 목록 및 내용 (1) 제이네트워크 2014-01-24 46874
13 [스크립트언어] [XE] xe (제로보드xe) 로그인 패스워드를 잊어 버렸을 경우 제이네트워크 2013-08-06 47059
12 [리눅스서버] [SSH]SSH Geoip적용 하여 국가별 IP 허용하기 제이네트워크 2015-01-21 47357
11 [DB] [mysql] 해당 컬럼 값을 1씩 증가시키기 와 mysql 자료형,제약조건,엔진 제이네트워크 2015-01-21 48092
10 [리눅스서버] Centos 6.x + php5.x 에서 mssql 서버 연동방법 (1) 제이네트워크 2014-12-12 48093
9 [스크립트언어] [php]설치시 에러 configure: error: xml2-config not found. please check your libxml2 installatio… 관리자 2011-10-11 48968
8 [스크립트언어] php 에러 메세지 출력하기 (php.ini 설정) 제이네트워크 2016-07-27 49486
7 [리눅스서버] 리눅스서버에서 한글파일명 깨질때 (파일질라나 ftp로 한글깨짐) 제이네트워크 2017-03-16 49811
6 [스크립트언어] [php]php설치후 에러 SAFE MODE Restriction in effect. The script whose uid is 501 is not allowed… 제이네트워크 2013-08-19 52608
5 [리눅스서버] find 명령어로 일정기간, 특정용량 파일 삭제 및 찾기 제이네트워크 2013-02-27 56607
4 [윈도우서버] [ASP] IIS 7.x 에서 500내부서버오류 정보보기 제이네트워크 2013-02-05 58326
3 [DB] [mysql] DB 에러 메세지 Unable to lock ./ibdata1, error: 11 (1) 제이네트워크 2012-11-18 58736
2 [스크립트언어] [php]php.ini 설정에서 register_globals = on/off 차이점과 기능설명 (1) 관리자 2012-02-09 66673
1 [스크립트언어] [PHP] php에서 Excel 파일 읽기/쓰기 (2) 제이네트워크 2015-01-23 493259
 1  2  3  4  5  6  7  8  9  10