테스트 사이트 - 개발 중인 베타 버전입니다

엑셀 xlsx 로 저장 방법

· 2년 전 · 3676 · 32

 

GitHub - mk-j/PHP_XLSXWriter: Lightweight XLSX Excel Spreadsheet Writer in PHP

 

xlsxwriter.class.php 파일 다운받아 lib 폴더에 넣어 주세요( 이 파일만 필요합니다.)

 

아래 코드를 excel_view.php 로 저장해서 엑셀로 저장하고 싶은 데이터가 있는 스킨폴더에 복사

 

필요한 위치에 아래처럼 링크를 걸어 사용하시면 됩니다. 

현재 코드는   &wr_id=<?=$list[$i]['wr_id'] 를 이용해 wr_id 기준으로 검색하는 예시입니다.

생략하면 $bo_table 전체데이터를 저장합니다. $result 쿼리 구문도 수정해 줘야됩니다.

 

<a href="<?= $board_skin_url?>/excel_view.php?bo_table=<?=$bo_table?>&wr_id=<?=$list[$i]['wr_id']?>" class="btn_b01 btn">엑셀다운</a>

 

 [code]

<?php
include_once('./_common.php');

set_include_path(get_include_path().PATH_SEPARATOR."..");
include_once(G5_LIB_PATH."/xlsxwriter.class.php");

 

$writer = new XLSXWriter();

 

$wr_id = isset($_GET['wr_id']) ? intval($_GET['wr_id'])  : 0;

$bot_table = $g5['write_prefix'].$bo_table;

 

$filename = "테스트신청현황.xlsx";

$filepath1 = G5_DATA_PATH.'/tmp/'.$filename;

 

$header = [];
$widths = [];

$contents = [];

 

// $header 제목명은 모두 달라야 됨, 뒤에 여러 서식을 지정할 수 있습니다.(위 github 참조) 
$header["연번"] = 'integer';
$header["예약자"] = 'string';
$header["예약일자"] = 'string';
$header["예약시간"] = 'string';
$header["전화번호"] = 'string';
$header["성별"] = 'string';
$header["요청사항"] = 'string';
$header["신청일"] = 'string';

 

//셀너비- 순서대로 알맞게 지정
$widths[] = 10; $widths[] = 20; $widths[] = 20; $widths[] = 20; $widths[] = 20; $widths[] = 20;$widths[] = 50; $widths[] = 30;

 

$styles1 = array('font'=>'맑은 고딕','font-size'=>12,'font-style'=>'bold', 'fill'=>'#ccff999', 'halign'=>'left', 'valign'=>'center', 'border'=>'left,right,top,bottom', 'widths'=>$widths);

$styles2 = array('font'=>'맑은 고딕','font-size'=>12, 'halign'=>'left', 'valign'=>'center', 'border'=>'left,right,top,bottom','wrap_text'=>true);
 

$writer->writeSheetHeader('Sheet1', $header, $styles1);    //제목줄 서식 포함

 

// 가져올 쿼리 구문

$result = sql_query("select * from {$bot_table} where wr_id = '{$wr_id}' order by wr_name");

foreach ($result as $key=>$field) {
    $contents[] = ($key + 1);
    $contents[] = $field['wr_name'];
    $contents[] = $field['wr_1'];
    $contents[] = $field['wr_2'];
    $contents[] = $field['wr_3'];
    $contents[] = $field['wr_4'];
    $contents[] = $field['wr_content'];
    $contents[] = $field['wr_last'];

    $writer->writeSheetRow('Sheet1', $contents, $styles2);
    $contents = [];
}

$writer->writeToFile($filepath1);

 

//아래부분은 수정할 필요 없습니다.
$filepath = addslashes($filepath1);
$original = urlencode($filename);

if(preg_match("/msie/i", $_SERVER["HTTP_USER_AGENT"]) && preg_match("/5\.5/", $_SERVER["HTTP_USER_AGENT"])) {
    header("content-type: doesn/matter");
    header("content-length: ".filesize("$filepath"));
    header("content-disposition: attachment; filename=\"$original\"");
    header("content-transfer-encoding: binary");
} else if (preg_match("/Firefox/i", $_SERVER["HTTP_USER_AGENT"])){
    header("content-type: file/unknown");
    header("content-length: ".filesize("$filepath"));
    header("content-disposition: attachment; filename=\"".basename($filename)."\"");
    header("content-description: php generated data");
} else {
    header("content-type: file/unknown");
    header("content-length: ".filesize("$filepath"));
    header("content-disposition: attachment; filename=\"$original\"");
    header("content-description: php generated data");
}
header("pragma: no-cache");
header("expires: 0");
flush();

$fp = fopen($filepath, "rb");

if (!fpassthru($fp)) {
    fclose($fp);
}

 

//파일 삭제

if(file_exists($filepath1)) {
    @unlink($filepath1);
}

[/code]

댓글 작성

댓글을 작성하시려면 로그인이 필요합니다.

로그인하기

댓글 32개

5개월 전

@WebloveKR 

$header = []; [] 를 모두 array(); 로 바꿔보세요

$header = array();
$widths = array();
$contents = array();

그리고 배열에 값 추가시

array_push($contents, $a, $b, $c);

$contents = array(); 초기화

엑셀저장

게시글 목록

번호 제목
24318
24317
24315
24309
24294
24293
24277
24262
24260
24253
24251
24236
24233
24228
24226
24221
24214
24203
24201
24199
24196
24195
24194
24192
24191
24187
24185
24183
24172
24168