我试图在点击按钮后生成excel文件。我把它作为形式:
<form method="post" action="ExcelStudio.php" target="_blank" style="float:right;">
<input type="submit" name="export_excel_studio" class="button" value="Studio Report"></input>
</form>
因此,在提交表单后,我继续生成excel文件:
<?php
require_once ('PHPExcel/Classes/PHPExcel.php');
header('Content-Type: text/html; charset=utf-8');
include('inc/database_connection.php');
$conn = mysqli_connect(DATABASE_HOST, DATABASE_USER, DATABASE_PASSWORD, DATABASE_NAME);
$output = '';
$conn->set_charset('utf8_unicode_ci');//if not by default
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
$F = $objPHPExcel->getActiveSheet();
if(isset($_POST["export_excel_studio"]))
{
// 1.Get data
$sql2 = '
select
p2.proc_leader as username,
max(case when studio = "APEX" then "x" else "" end) as APEX,
max(case when studio = "BASECAMP" then "x" else "" end) as BASECAMP,
max(case when studio = "CANVAS" then "x" else "" end) as CANVAS,
max(case when studio = "HORIZON" then "x" else "" end) as HORIZON,
max(case when studio = "LAUNCHPAD" then "x" else "" end) as LAUNCHPAD,
max(case when studio = "NEBULA" then "x" else "" end) as NEBULA,
max(case when studio = "ORBIT" then "x" else "" end) as ORBIT,
max(case when studio = "PALETTE" then "x" else "" end) as PALETTE,
max(case when studio = "SANDBOX" then "x" else "" end) as SANDBOX,
max(case when studio = "STELLAR" then "x" else "" end) as STELLAR,
max(case when studio = "THE CLIMB" then "x" else "" end) as THECLIMB,
max(case when studio = "TOONIGAMI" then "x" else "" end) as TOONIGAMI,
max(case when studio = "TREEHOUSE" then "x" else "" end) as TREEHOUSE
from process p1
left join (
select *
from proc_leader
union all
select *
from proc_checker
union all
select *
from proc_staff
) p2 on p1.projectNo = p2.projectNo
and p1.process = p2.process
group by p2.proc_leader
';
// 2. Output table
$query2 = mysqli_query($conn, $sql2);
$header = '<th style="border: 1px solid black;">Username</th>
<th style="border: 1px solid black;">Studio APEX</th>
<th style="border: 1px solid black;">Studio BASECAMP</th>
<th style="border: 1px solid black;">Studio CANVAS</th>
<th style="border: 1px solid black;">Studio HORIZON</th>
<th style="border: 1px solid black;">Studio LAUNCHPAD</th>
<th style="border: 1px solid black;">Studio NEBULA</th>
<th style="border: 1px solid black;">Studio ORBIT</th>
<th style="border: 1px solid black;">Studio PALETTE</th>
<th style="border: 1px solid black;">Studio SANDBOX</th>
<th style="border: 1px solid black;">Studio STELLAR</th>
<th style="border: 1px solid black;">Studio THECLIMB</th>
<th style="border: 1px solid black;">Studio TOONIGAMI</th>
<th style="border: 1px solid black;">Studio TREEHOUSE</th>';
$body = '';
while ($data2 = mysqli_fetch_assoc($query2)) {
$username = $data2['username'];
$apex = $data2['APEX'];
$basecamp = $data2['BASECAMP'];
$canvas = $data2['CANVAS'];
$horizon = $data2['HORIZON'];
$launchpad = $data2['LAUNCHPAD'];
$nebula = $data2['NEBULA'];
$orbit = $data2['ORBIT'];
$palette = $data2['PALETTE'];
$sandbox = $data2['SANDBOX'];
$stellar = $data2['STELLAR'];
$theclimb = $data2['THECLIMB'];
$toonigame = $data2['TOONIGAMI'];
$treehouse = $data2['TREEHOUSE'];
$row = '<td style="border: 1px solid black;">' . htmlspecialchars($username) . '</td>
<td style="border: 1px solid black;">' . $apex . '</td>
<td style="border: 1px solid black;">' . $basecamp . '</td>
<td style="border: 1px solid black;">' . $canvas . '</td>
<td style="border: 1px solid black;">' . $horizon . '</td>
<td style="border: 1px solid black;">' . $launchpad . '</td>
<td style="border: 1px solid black;">' . $nebula . '</td>
<td style="border: 1px solid black;">' . $orbit . '</td>
<td style="border: 1px solid black;">' . $palette . '</td>
<td style="border: 1px solid black;">' . $sandbox . '</td>
<td style="border: 1px solid black;">' . $stellar . '</td>
<td style="border: 1px solid black;">' . $theclimb . '</td>
<td style="border: 1px solid black;">' . $toonigame . '</td>
<td style="border: 1px solid black;">' . $treehouse . '</td>';
$body .= "<tr style='border: 1px solid black;'>$row</tr>";
}
$output .= "
<table class='table' style='border: 1px solid black;'>
<thead><tr>$header</tr></thead>
<tbody>$body</tbody>
</table>
";
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="StudioReport.xls"');
header('Cache-Control: max-age=0');
echo $output;
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
}
除了一件事,它工作得很好,我在桌子后面得到了奇怪的符号。所以我认为这意味着某处存在问题。这是我打开excel文件后得到的。什么可能是一个问题,我该如何解决这个问题?谢谢
答案 0 :(得分:2)
通过更改文件来实现它:
header( "Content-Type: application/vnd.ms-excel" );
header( "Content-disposition: attachment; filename=StudioReport.xls" );
include('inc/database_connection.php');
$conn = mysqli_connect(DATABASE_HOST, DATABASE_USER, DATABASE_PASSWORD, DATABASE_NAME);
$output = '';
$conn->set_charset('utf8_unicode_ci');//if not by default
// print your data here. note the following:
// - cells/columns are separated by tabs ("\t")
// - rows are separated by newlines ("\n")
if(isset($_POST["export_excel_studio"]))
{
// 1.Get data
$sql2 = '
select
p2.proc_leader as username,
max(case when studio = "APEX" then "x" else "" end) as APEX,
max(case when studio = "BASECAMP" then "x" else "" end) as BASECAMP,
max(case when studio = "CANVAS" then "x" else "" end) as CANVAS,
max(case when studio = "HORIZON" then "x" else "" end) as HORIZON,
max(case when studio = "LAUNCHPAD" then "x" else "" end) as LAUNCHPAD,
max(case when studio = "NEBULA" then "x" else "" end) as NEBULA,
max(case when studio = "ORBIT" then "x" else "" end) as ORBIT,
max(case when studio = "PALETTE" then "x" else "" end) as PALETTE,
max(case when studio = "SANDBOX" then "x" else "" end) as SANDBOX,
max(case when studio = "STELLAR" then "x" else "" end) as STELLAR,
max(case when studio = "THE CLIMB" then "x" else "" end) as THECLIMB,
max(case when studio = "TOONIGAMI" then "x" else "" end) as TOONIGAMI,
max(case when studio = "TREEHOUSE" then "x" else "" end) as TREEHOUSE
from process p1
left join (
select *
from proc_leader
union all
select *
from proc_checker
union all
select *
from proc_staff
) p2 on p1.projectNo = p2.projectNo
and p1.process = p2.process
group by p2.proc_leader
';
// 2. Output table
$query2 = mysqli_query($conn, $sql2);
$header = '<th style="border: 1px solid black;">Username</th>
<th style="border: 1px solid black;">Studio APEX</th>
<th style="border: 1px solid black;">Studio BASECAMP</th>
<th style="border: 1px solid black;">Studio CANVAS</th>
<th style="border: 1px solid black;">Studio HORIZON</th>
<th style="border: 1px solid black;">Studio LAUNCHPAD</th>
<th style="border: 1px solid black;">Studio NEBULA</th>
<th style="border: 1px solid black;">Studio ORBIT</th>
<th style="border: 1px solid black;">Studio PALETTE</th>
<th style="border: 1px solid black;">Studio SANDBOX</th>
<th style="border: 1px solid black;">Studio STELLAR</th>
<th style="border: 1px solid black;">Studio THECLIMB</th>
<th style="border: 1px solid black;">Studio TOONIGAMI</th>
<th style="border: 1px solid black;">Studio TREEHOUSE</th>';
$body = '';
while ($data2 = mysqli_fetch_assoc($query2)) {
$username = $data2['username'];
$apex = $data2['APEX'];
$basecamp = $data2['BASECAMP'];
$canvas = $data2['CANVAS'];
$horizon = $data2['HORIZON'];
$launchpad = $data2['LAUNCHPAD'];
$nebula = $data2['NEBULA'];
$orbit = $data2['ORBIT'];
$palette = $data2['PALETTE'];
$sandbox = $data2['SANDBOX'];
$stellar = $data2['STELLAR'];
$theclimb = $data2['THECLIMB'];
$toonigame = $data2['TOONIGAMI'];
$treehouse = $data2['TREEHOUSE'];
$row = '<td style="border: 1px solid black;">' . htmlspecialchars($username) . '</td>
<td style="border: 1px solid black;">' . $apex . '</td>
<td style="border: 1px solid black;">' . $basecamp . '</td>
<td style="border: 1px solid black;">' . $canvas . '</td>
<td style="border: 1px solid black;">' . $horizon . '</td>
<td style="border: 1px solid black;">' . $launchpad . '</td>
<td style="border: 1px solid black;">' . $nebula . '</td>
<td style="border: 1px solid black;">' . $orbit . '</td>
<td style="border: 1px solid black;">' . $palette . '</td>
<td style="border: 1px solid black;">' . $sandbox . '</td>
<td style="border: 1px solid black;">' . $stellar . '</td>
<td style="border: 1px solid black;">' . $theclimb . '</td>
<td style="border: 1px solid black;">' . $toonigame . '</td>
<td style="border: 1px solid black;">' . $treehouse . '</td>';
$body .= "<tr style='border: 1px solid black;'>$row</tr>";
}
$output .= "
<table class='table' style='border: 1px solid black;'>
<thead><tr>$header</tr></thead>
<tbody>$body</tbody>
</table>
";
echo $output;
}