|
PHPExcel: Иероглифы при выводе в файл xls из запроса mysql. Теряется шапка отчёта
#39207739
Ссылка:
Ссылка на сообщение:
Ссылка с названием темы:
|
|
|
|
Проблема: в формируемый файл xls попадают форма и кнопка со страницы, теряется шапка отчёта, иероглифы.
Описание: Есть страничка с двумя формами: выбираются даты отчёта и кнопка ок.
И собственно сам отчёт, который должен сформировать xls файл с шапкой
и результатом вывода sql-запроса. Для этих целей был установлен PHPExcel
PHP владею на начальном уровне, слепил такую вот штуку:
начальная страница с формой:
detail.php
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60.
<html>
<title>Отчёт по звонкам</title>
<?php
include 'design.php';
$par=array('721'=>"Группа 721", '206'=>"Группа 206", '206ex'=>"206 Excel Report");
?>
<!--<table><tr><td>Отчёт по звонкам</td></tr></table>-->
<body>
<table width=80% border=0>
<tr><th colspan=2>Отчёт по звонкам</th></tr>
<tr><td></td></tr>
<tr><td align="center">
<form action="detail.php" method="post">
  <select name="status">
<?php
foreach($par as $key=>$value):
echo '<option value="'.$key.'">'.$value.'</option>';
endforeach;
?>
</select>
                              
<b>Дата1</b><input type="text" name="date1">
   <b>Дата2</b><input type="text" name="date2">
 <input type="submit" name ="button" value="ok">
</form>
</td>
<tr><td><hr>
<?php
$v721='721';
$v206='206';
$v206ex='206ex';
if(isset($_POST['button']))
{
$choice=$_POST['status']; // form value
if ($choice==$v721)
{
if (empty($_POST['date1']) || empty($_POST['date2']))
{
echo "<div class=\"errors\">".'Период не выбран!'."</div>";
}
else
{
include '721.php';
}
}
elseif ($choice==$v206) {
//echo 'This is NEW detail report';
include '206_detail.php';
}
elseif($choice==$v206ex) {
include 'report.php';
}
}
?>
</td></tr>
</table>
</body>
</html>
login.php
1. 2. 3. 4. 5. 6.
<?php
$db_hostname='myhost';
$db_database='mydb';
$db_username='admin';
$db_password='password';
?>
report.php
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83. 84. 85. 86. 87. 88. 89. 90. 91. 92. 93. 94. 95. 96. 97. 98. 99. 100. 101. 102. 103. 104. 105. 106. 107. 108. 109. 110. 111. 112. 113. 114. 115. 116. 117. 118.
<?php
//include 'design.php';
include 'login.php';
$d1=$_POST['date1'];
$d2=$_POST['date2'];
$pattern = '/(\d)[^\d]{1}(\d)/';
$replace = '\1-\2';
$regexp1= preg_replace($pattern,$replace,$d1);
$regexp2= preg_replace($pattern,$replace,$d2);
$eventDate1 = DateTime::createFromFormat('d-m-Y', $regexp1, new DateTimeZone('Europe/Moscow'));
$eventDate2 = DateTime::createFromFormat('d-m-Y', $regexp2, new DateTimeZone('Europe/Moscow'));
$date_format1=date_format($eventDate1, 'Y-m-d');
$date_format2=date_format($eventDate2, 'Y-m-d');
$db=mysql_connect($db_hostname,$db_username,$db_password);
mysql_select_db($db_database,$db);
$result = mysql_query("
select calldate, src, dst, substr(dstchannel,1,10), disposition,billsec
from cdr
where dst='222'
and calldate>='".$date_format1."'
and calldate<='".$date_format2."'
");
// Подключаем класс для работы с excel
require_once('PHPExcel.php');
// Подключаем класс для вывода данных в формате excel
require_once('PHPExcel/Writer/Excel5.php');
// Create Object PHPExcel Class
$xls = new PHPExcel();
// Set Index Active List
$xls->setActiveSheetIndex(0);
// Get Active List
$sheet = $xls->getActiveSheet();
// Set Header Font Color
$xls->getActiveSheet()->getStyle('D2:I2')->getFill()
->setFillType(PHPExcel_Style_Fill::FILL_SOLID)
->getStartColor()->setARGB('FFE8E5E5');
// Write List
$sheet->setTitle('Call Report PHP');
$datereport=date("Y-m-d");
// Report's Header
$sheet->setCellValue("D1", 'Дата выполнения: '.$datereport);
$sheet->setCellValue("D2", 'Дата');
$sheet->setCellValue("E2", 'От кого');
$sheet->setCellValue("F2", 'Кому');
$sheet->setCellValue("G2", 'Канал приёмник');
$sheet->setCellValue("H2", 'Статус');
$sheet->setCellValue("I2", 'Длительность');
foreach(range('D','I') as $columnID)
{
$sheet->getColumnDimension($columnID)->setAutoSize(true);
}
$sheet->getStyle('D2')->getFont()->setBold(true);
<------> $sheet->getStyle('E2')->getFont()->setBold(true);
<------> $sheet->getStyle('F2')->getFont()->setBold(true);
<------> $sheet->getStyle('G2')->getFont()->setBold(true);
<------> $sheet->getStyle('H2')->getFont()->setBold(true);
<------> $sheet->getStyle('I2')->getFont()->setBold(true);
//Start With: D3 <---><------>E3 <---><------>F3 <---><------>G3 <---><------>H3 <---><------>I3
//<----><------>row[0] <------>row[1] <------>row[2] <------>row[3] <------>row[4] <------>row[5]
$rowCount = 3;
.....
while ($row = mysql_fetch_array($result)) {
....
<------>$xls->getActiveSheet()->SetCellValue('D'.$rowCount, $row[0]); // D3,'data'
<------>$xls->getActiveSheet()->SetCellValue('E'.$rowCount, $row[1]); // E3,'data'
<------>$xls->getActiveSheet()->SetCellValue('F'.$rowCount, $row[2]); // F3,'data'
<------>$xls->getActiveSheet()->SetCellValue('G'.$rowCount, $row[3]); // G3,'data'
<------>$xls->getActiveSheet()->SetCellValue('H'.$rowCount, $row[4]); // H3,'data'
<------>$xls->getActiveSheet()->SetCellValue('I'.$rowCount, $row[5]); // I3,'data'
$rowCount++;
}
// Объединяем ячейки
//$sheet->mergeCells('A1:H1');
// Выводим HTTP-заголовки
header ( "Expires: Mon, 1 Apr 1974 05:00:00 GMT" );
header ( "Last-Modified: " . gmdate("D,d M YH:i:s") . " GMT" );
header ( "Cache-Control: no-cache, must-revalidate" );
header ( "Pragma: no-cache" );
header ( "Content-type: application/vnd.ms-excel" );
header ( "Content-Disposition: attachment; filename=report.xls" );
// Выводим содержимое файла
$objWriter = new PHPExcel_Writer_Excel5($xls);
$objWriter->save('php://output');
mysql_close($db);
?>
|
|
|