HOME > 이용안내
이용안내
 
작성일 : 15-01-23 12:21
[스크립트언어] [PHP] php에서 Excel 파일 읽기/쓰기
 글쓴이 : 제이네트워크
조회 : 591,359  
[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 385

번호 제   목   글쓴이 날짜 조회
265 [리눅스서버] INIT: no more processes left in this runlevel 메세지 발생후 부팅에러 제이네트워크 2016-08-01 37910
264 [리눅스서버] vsftp 에러코드 중 500 OOPS: priv_sock_get_int 오류 발생시 해결법 제이네트워크 2016-07-31 46776
263 [스크립트언어] php 에러 메세지 출력하기 (php.ini 설정) 제이네트워크 2016-07-27 69551
262 [리눅스서버] 리눅스에서 공백이 포함된 디렉토리 처리 제이네트워크 2016-07-26 39963
261 [메일] 리눅스 sendmail 로 폼메일사용시 [email protected] 으로 발송될 경우 (1) 제이네트워크 2016-07-06 39576
260 [리눅스서버] [아파치SSL 에러] File size limit exceeded$HTTPD -DSSL 해결법 제이네트워크 2016-06-22 49165
259 [리눅스서버] CentOS 7.x vsftpd 자신의 홈디렉토리 상위폴더 접근막기 (2) 제이네트워크 2016-06-01 42035
258 [리눅스서버] Centos7.x 4TB 이상 하드디스크 추가 및 포맷하기 제이네트워크 2016-06-01 41087
257 [리눅스서버] 스왑메모리 초기화 혹은 swap 메모리 관리 제이네트워크 2016-05-13 40543
256 [기타] tar 특정폴더에 압축을 풀어야 할 경우 사용 옵션 제이네트워크 2016-05-10 35886
255 [DB] [mysqldump 에러] Got errno 28 on write 발생시 제이네트워크 2016-03-29 37688
254 [윈도우서버] Windows Server 2012R2 (윈도우2012 R2) IIS8.5 서버 FTP 가상디렉토리 설정 제이네트워크 2016-03-25 44360
253 [리눅스서버] [우분투]ubuntu 15.10 서버 네트워크 설정 및 ufw 방화벽 제이네트워크 2016-03-24 39258
252 [리눅스서버] 우분투15.10 서버에서 SSH접속 안될 때 [에러-서버와 매치되는 outgoing encryption … 제이네트워크 2016-03-17 41261
251 [PHP] php 에러시 Warning: date(): It is not safe to rely on the system's timezone settings 제이네트워크 2016-02-03 37290
250 [DB] phpMyAdmin 4.x 버전에서 필요없는 에러메세지 없애는 방법 제이네트워크 2016-02-01 36306
249 [쉘스크립트] /bin/sh^M: bad interpreter: No such file or directory 오류 발생 시 제이네트워크 2016-01-22 38677
248 [리눅스서버] 리눅스 에서 Apche / php / mysql 컴파일 옵션 확인하기 제이네트워크 2015-12-09 35687
247 [스크립트언어] [php-openssl] php-4.4.x openssl 추가 모듈 설치 제이네트워크 2015-12-01 42094
246 [스크립트언어] php zip 설치 (ziparchive php extension) 제이네트워크 2015-11-25 54907
245 [스크립트언어] php-5.3.x 확장모듈 cURL 설치 방법 제이네트워크 2015-11-24 50539
244 [스크립트언어] php-4.x.x 의경우 phpize 사용하여 curl 모듈 추가] 제이네트워크 2015-11-24 45792
243 [스크립트언어] php 재컴파일 없이 모듈추가 하는 방법 입니다. 제이네트워크 2015-11-24 50022
242 [스크립트언어] [php]php-5.3.x 버전 부터 Zend Guard Loader 설치 제이네트워크 2015-11-06 41844
241 [리눅스서버] [apache] mod_ruid2 모듈 설치시 에러 mod_ruid2.c:45:28: error: sys/capability.h: No such file … 제이네트워크 2015-11-06 39215
240 [메일] 센드메일-아웃룩에러 중 다른메일메세지가 배달중이거나... 제이네트워크 2015-11-03 37112
239 [스크립트언어] 각언어별 redirect 구문 제이네트워크 2015-10-06 43908
238 [메일] [sendmail]메일 수발신이 느릴때 (sendmail이 정상동작 하나 느리게 작동할 때) 제이네트워크 2015-09-22 39648
237 [DB] [mysql]mysqldump 로 백업시 lock table 에러로 백업이 안되는 경우 제이네트워크 2015-09-17 37201
236 [리눅스서버] CentOS(리눅스)버전, 혹은 APM(apache, php, mysql) 버전 확인하려고 할때 사용하는 … 제이네트워크 2015-09-07 42011
235 [스크립트언어] mysql 설치 후 phpMyAdmin #2002 MySQL 서버에 로그인할 수 없습니다. 해결법 (1) 제이네트워크 2015-09-03 55678
234 [리눅스서버] 리눅스서버에서 chracterset 을 맞출려고 할 때 제이네트워크 2015-08-25 38568
233 [윈도우서버] windows 2008/2012 서버 셋팅후 403 forbbiden 에러시 제이네트워크 2015-07-22 40942
232 [DB] [mysql] Mysql 에러코드 중에 (Got error 134 from storage engine) 나올때 처리법 제이네트워크 2015-06-04 35483
231 [DB] [mysql-5.6.x 설치 후 에러로 실행 안될때 ][Warning] TIMESTAMP with implicit DEFAULT value … 제이네트워크 2015-05-22 70237
230 [리눅스서버] [apche 1.3.x | 2.0.x 2.2 x.2.4.x ] 아파치 버전별 차이점 제이네트워크 2015-05-19 46947
229 [DB] [MS-SQL SERVER 2008 로그 파일 사이즈 줄이기] 제이네트워크 2015-05-12 38403
228 [윈도우서버] IIS7.x / IIS8.x 에서 asp 호스팅시 "Response 버퍼 제한 초과됨 " 오류 제이네트워크 2015-05-11 44918
227 [윈도우서버] [windows2008] SQL Server 액세스를 허용하도록 Windows 방화벽 구성 제이네트워크 2015-05-04 40644
226 [윈도우서버] [iis7.x ASP 에러] An error occurred on the server when processing the URL 제이네트워크 2015-05-04 71332
 1  2  3  4  5  6  7  8  9  10