这里,我展示了一些数据库表的部分代码,导出我的excel文件。文件是写入的,但格式不对:
function get_report(){
$this->load->dbutil();
$this->load->helper('download');
$query = $this->pharmacy_model->get_report();
$data = $this->dbutil->csv_from_result($query, ';');
force_download('CSV_Report.csv', $data);
}它是用数据下载和提取的,但是格式不正确。我需要它就像在这幅图像
发布于 2017-04-25 13:29:52
$data = $this->dbutil->csv_from_result($query, ';');只需在下面使用','而不是分号()
发布于 2017-01-04 10:17:32
步骤1:在键值对.中获取MySql数据
Array
(
[14] => Array
(
[Name] => Parvez Alam
[Age] => 21
[Gender] => Male
)
[15] => Array
(
[Name] => Shavez Alam
[Age] => 21
[Gender] => Male
)
)步骤2:获取选项类型的PHP代码,并强制下载文件,而不是显示.
if(isset($_POST["ExportType"])){
switch($_POST["ExportType"])
{
case "export-to-excel" :
// Submission from
$filename = $_POST["ExportType"] . ".xls";
header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=\"$filename\"");
ExportFile($data);
//$_POST["ExportType"] = '';
exit();
default :
die("Unknown action : ".$_POST["action"]);
break;
}}
function ExportFile($records) {
$heading = false;
if(!empty($records))
foreach($records as $row) {
if(!$heading) {
// display field/column names as a first row
echo implode("\t", array_keys($row)) . "\n";
$heading = true;
}
echo implode("\t", array_values($row)) . "\n";
}
exit;
}步骤3:定义html布局,用于在表和按钮中显示数据,以触发导出到csv操作.
<div><a href="javascript:void(0)" id="export-to-excel">Export to excel</a></div>
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post" id="export-form">
<input type="hidden" value='' id='hidden-type' name='ExportType'/>
</form>
<table id="" class="table table-striped table-bordered">
<tr>
<th>Name</th>
<th>Status</th>
<th>Priority</th>
<th>Salary</th>
</tr>
<tbody>
<?php foreach($data as $row):?>
<tr>
<td><?php echo $row ['Name']?></td>
<td><?php echo $row ['Status']?></td>
<td><?php echo $row ['Priority']?></td>
<td><?php echo $row ['Salary']?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>步骤4:现在我们将使用jQuery代码来获取单击event.
<script type="text/javascript">
$(document).ready(function() {
jQuery('#Export to excel').bind("click", function() {
var target = $(this).attr('id');
switch(target) {
case 'export-to-excel' :
$('#hidden-type').val(target);
//alert($('#hidden-type').val());
$('#export-form').submit();
$('#hidden-type').val('');
break
}
});
});
</script>https://stackoverflow.com/questions/27456209
复制相似问题