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

번호 제   목   글쓴이 날짜 조회
게시물이 없습니다.