Skip to content

Instantly share code, notes, and snippets.

@antonkril
Created June 25, 2011 20:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antonkril/1046840 to your computer and use it in GitHub Desktop.
Save antonkril/1046840 to your computer and use it in GitHub Desktop.
<?php
error_reporting(0);
header('Content-type: text/html; charset=windows-1251');
$error = null;
$message = null;
if (!empty($_POST)) {
try {
if(empty($_POST['db'])){
throw new LogicException('Не задано базу данних');
}
if(empty($_POST['table'])){
throw new LogicException('Не задано таблицю');
}
$db = new PDO('mysql:dbname='.trim($_POST['db']).';host=127.0.0.1', 'root', '');
$result = $db->prepare('SELECT * FROM ' . $_POST ['table']);
if(!$result->execute()){
throw new RuntimeException('Немає такої таблиці');
}
$items = $result->fetchAll(PDO::FETCH_BOTH);
if (count($items) === 0) {
throw new RuntimeException('Таблиця пуста');
} else {
$excel = new COM ( "excel.application" );
$excel->Visible = 0;
$workbook = $excel->Workbooks->Add ();
$sheet = $workbook->Worksheets ( 1 );
$sheet->activate;
foreach($items as $key => $item){
$i = $key+1;
$sheet->Cells ( $i, 1 )->value = $item [0];
$sheet->Cells ( $i, 2 )->value = $item [1];
$sheet->Cells ( $i, 3 )->value = $item [2];
$sheet->Cells ( $i, 4 )->value = $item [3];
$sheet->Cells ( $i, 5 )->value = $item [4];
$sheet->Cells ( $i, 6 )->value = $item [5];
}
$workbook->SaveAs ( "test.xls" );
$workbook->Close ( false );
$excel->Workbooks->Close ();
$excel->Quit ();
$message = "Дані збережено";
}
} catch (PDOException $e) {
switch($e->getCode()){
case 2002:
$error = 'Неправильна адреса сервера баз данних';
break;
case 1049:
$error = "Немає такої бази данних";
break;
default:
$error = "Сталась помилка сервера баз данних. Сервер відповів: ". $e->getCode();
break;
}
} catch (Exception $e){
$error = $e->getMessage();
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="cp-1251" />
<style type="text/css">
body{width:400px;margin:auto;font-family:Arial; padding-top:100px;}
.error{background-color:#fdd;border:1px solid #a55;padding:5px;}
.message{background-color:#fef;border:1px solid #a5a;padding:5px;}
</style>
</head>
<body>
<?php if ($error):?>
<dl class="error">
<dt>Помилка</dt>
<dd><?php echo $error; ?></dd>
</dl>
<?php endif;?>
<?php if ($message):?>
<dl class="message">
<dt>Повідомлення</dt>
<dd><?php echo $message; ?></dd>
</dl>
<?php endif;?>
<form method="post">
<div>
<fieldset>
<dl>
<dt><label for="db">Назва БД:</label></dt>
<dd><input type="text" size="45" name="db" id="db" value="<?php echo isset($_POST['db'])?$_POST['db']:'';?>" /></dd>
<dt><label for="table">Таблиця:</label></dt>
<dd><input type="text" size="45" name="table" id="db" value="<?php echo isset($_POST['table'])?$_POST['table']:'';?>" /></dd>
</dl>
</fieldset>
<input type="submit" value="Створити" />
</div>
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment