gpt4 book ai didi

php - 将数组导出到 csv 将整个 html 文档导出到 csv

转载 作者:行者123 更新时间:2023-12-04 02:01:20 27 4
gpt4 key购买 nike

我正在尝试将数组导出到 CSV 文件中。我已经读过 this问题,我现在可以下载 CSV 文件了。问题是,CSV 文件中还包含整个 html 文档,我不知道它来自哪里。

数组看起来像这样:

Array
(
[0] => Array
(
[id] => 1
[tphInventoryNumber] => D17001
[title] => Assets in Leasing
[price] => 1299.95
[recievedDate] => 2017-11-02
[departement] => 1
[unit] => 1
)

[1] => Array
(
[id] => 2
[tphInventoryNumber] => D17002
[title] => Assets in Leasing
[price] => 12.05
[recievedDate] => 2017-10-31
[departement] => 7
[unit] => 3
)

)

我导出 CSV 的函数如下所示:

function exportToCSV($array, $filename = "export.csv", $delimiter = ";"){
header('Content-Type: application/csv; charset=UTF-8');
header('Content-Disposition: attachment;filename="'.$filename.'";');
$f = fopen('php://output', 'w');
foreach ($array as $line) {
fputcsv($f, $line, $delimiter);
}
}

知道我在这里做错了什么吗?我找不到解决此问题的任何方法...

更新:

我通过发送 post 变量的按钮调用该函数。

表格:

<form action="start.php?load=export" method="post">
<input type="hidden" name="exportCSV" value="exportCSV">
<button class="btn btn-info" type="submit">CSV Export</button>
</form>

检查 Post 变量:

if (isset($_POST['exportCSV'])) {
exportToCSV($arrayCSV);
}

更新 2

这是 CSV 的实际内容:

  <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>TEST IPA</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" href="css/style.css">
</head>
<body>


<nav class="navbar navbar-default navbar-fixed-top">
<div class="container-fluid">

<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="start.php?load=inventory">
<img height="25" alt="Brand" src="img/logo_tph.png"/>
</a>
</div>

<div class="collapse navbar-collapse" id="navbar">
<ul class="nav navbar-nav">
<li><p class="navbar-text"><b>Test Inventar IPA</b></p></li>
<!--<li class="active"> <a href="start.php?load=inventory">Start</a></li>-->
<!--<li class=""><a href='assets.php?action=add'>Neu</a></li>-->
</ul>

<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">ipa2 <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="logout.php">Logout</a></li>
</ul>
</li>
</ul>
</div>

</div>
<div role="navigation" class="options-bar">
<ul class="nav nav-tabs">
<li role="presentation" class=""><a href="start.php?load=inventory">Inventar</a></li>
<li role="presentation" class=""><a href="start.php?load=createnew">Neu Erfassen</a></li>
<li role="presentation" class="active"><a href="start.php?load=export">Export</a></li>
<li role="presentation" class=""><a href="start.php?load=lists">Listen</a></li>
</ul>
</nav>
<div class="container-fluid wrapper-content">

1;D17001;"Assets in Leasing";1299.95;2017-11-02;1;1
2;D17002;"Assets in Leasing";12.05;2017-10-31;7;3

最佳答案

我只能假设您的框架导致 Controller 操作输出 View 或类似内容。您需要在函数末尾添加 exit;die;。示例:

function exportToCSV($array, $filename = "export.csv", $delimiter = ";") {
header('Content-Type: application/csv; charset=UTF-8');
header('Content-Disposition: attachment;filename="'.$filename.'";');
$f = fopen('php://output', 'w');
foreach ($array as $line) {
fputcsv($f, $line, $delimiter);
}

// halt execution of the script after output stream finalised
exit;
}

更新:您似乎已经将一些 html 输出写入输出缓冲区,因此您需要先使用 ob_end_clean(); 清理它。请参见以下示例:

function exportToCSV($array, $filename = "export.csv", $delimiter = ";") {
header('Content-Type: application/csv; charset=UTF-8');
header('Content-Disposition: attachment;filename="'.$filename.'";');

// clean output buffer
ob_end_clean();

$f = fopen('php://output', 'w');
foreach ($array as $line) {
fputcsv($f, $line, $delimiter);
}

// halt execution of the script after output stream finalised
exit;
}

关于php - 将数组导出到 csv 将整个 html 文档导出到 csv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47078721/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com