최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday

티스토리 뷰

Create가 아니다 Modify 개념이다. 즉 엑셀의 셀 데이터를 수정한뒤 그대로 다시 저장하는 것.



write1.php

testFile.xls



<?php
header("Content-Type:text/html; charset=utf-8");
header("Content-Encoding:utf-8");
ini_set('memory_limit', '128M');

//error_reporting(E_ALL);
set_time_limit(0);
set_include_path(get_include_path() . PATH_SEPARATOR . './phpexcel/Classes/');

include_once 'PHPExcel.php';
include_once 'PHPExcel/IOFactory.php';

$mode = $_POST['mode'];
$G2 = $_POST['G2'];
$G3 = $_POST['G3'];
$G4 = $_POST['G4'];

$objPHPExcel = new PHPExcel();
$fileName = './testFile.xls';
$fileType = 'Excel5';

echo "<!DOCTYPE html>
		<html>
			<head>
				<meta charset='utf-8'>
			</head>
			<body>";

try{
	//읽기모드
	if(!$mode){
		$objReader = PHPExcel_IOFactory::createReaderForFile($fileName);
		$objExcel = $objReader->load($fileName);
		$objExcel->setActiveSheetIndex(0);
		$objWorksheet = $objExcel->getActiveSheet();

		$highestRow  = $objWorksheet->getHighestRow();  // 열4
		$highestColumn = $objWorksheet->getHighestColumn(); // 행G
		$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn); // G를 7
		
		$G_count = 2;
		echo '엑셀 파싱 결과';
		echo "<form name='excelFormData' method='POST' action='$PHP_SELF'>";
		echo '<table border=1>';
		for($row = 1; $row <= $highestRow; ++$row){
			echo '<tr>';
			
			for($col = 0; $col < $highestColumnIndex; ++$col){
				if($row > 1 && $col == 6){

					echo "<td><input type=text name=G".$G_count." value=".$objWorksheet->getCellByColumnAndRow($col, $row)->getValue()." /></td>";
					$G_count++;

				}else{
					echo '<td>' . $objWorksheet->getCellByColumnAndRow($col, $row)->getValue() . '</td>';
				}
			}
			echo '</tr>';
		}
		echo '</table> <br /><br />';
		
		echo "<input type='hidden' name='mode' value='update' />";
		echo "<input type='submit' value='확인' />";

		echo "</form>";
	}

	//저장모드
	if($mode == 'update'){
		//echo $G2;
		//echo $G3;
		//echo $G4;

		$objReader = PHPExcel_IOFactory::createReader($fileType);
		$objPHPExcel = $objReader->load($fileName);
		$objPHPExcel->setActiveSheetIndex(0);
		$objPHPExcel->setActiveSheetIndex(0)
					->setCellValue('G2', $G2)
					->setCellValue('G3', $G3)
					->setCellValue('G4', $G4);

		$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, $fileType);
		$objWriter->save($fileName);

		echo '변경 내역 저장 중';

		$objWorksheet = $objPHPExcel->getActiveSheet();

		$highestRow  = $objWorksheet->getHighestRow();  // 열의 참조 : 4
		$highestColumn = $objWorksheet->getHighestColumn(); // 행의 참조 : F
		$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn); // 알파벳 : F를 6

		echo '<table border=1>';
		for($row = 1; $row <= $highestRow; ++$row){
			echo '<tr>';
			
			for($col = 0; $col < $highestColumnIndex; ++$col){
				echo '<td>' . $objWorksheet->getCellByColumnAndRow($col, $row)->getValue() . '</td>';
			}
			echo '</tr>';
		}
		echo '</table> <br /><br />';
	}
}catch(exception $e){
	//
}

echo "</body>
	  </html>";
?>



댓글