简体   繁体   English

MSSQL选择导出为CSV

[英]MSSQL Select export to CSV

I try to create a csv file with selected rows from a mssql database. 我尝试使用mssql数据库中的选定行创建一个csv文件。 The export works but the formatting is wrong. 导出有效,但格式错误。

PHP: PHP:

require_once("config/config.php");

$connectionInfo = array("Database"=>DB_DB, "UID"=>DB_USER, "PWD"=>DB_PASSWORD);
$conn = sqlsrv_connect(DB_HOST, $connectionInfo);

if ($conn === false ) {
    die (print_r(sqlsrv_errors(), true));
}

$sql = "SELECT * FROM [RC.Appointments]";
$result = sqlsrv_query($conn, $sql);

if (!$result) die ('Couldn\'t fetch records');

$headers = array();

foreach (sqlsrv_field_metadata($result) as $fieldMetadata) {
    $headers[] = $fieldMetadata['Name'];
}

$fp = fopen('php://output', 'w');
if ($fp && $result) {
    header('Content-Type: text/csv');
    header('Content-Disposition: attachment; filename="export.csv"');
    header('Pragma: no-cache');
    header('Expires: 0');
    fputcsv($fp, $headers);
    while ($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_NUMERIC)) {
        fputcsv($fp, array_values($row));
    }
    die;
}

Dont know why there are no headers and I need the data seperate by columns. 不知道为什么没有标题,我需要按列分开数据。 Is the problem caused by the missing headers? 问题是由丢失的标题引起的吗?

the output in sublimetext sublimetext中的输出

appointment_id,terminname,datum
151,"Bitte Terminnamen vergeben",18.02.2014
152,"Bitte Terminnamen vergeben",19.02.2014
153,"Bitte Terminnamen vergeben",20.02.2014
154,"Bitte Terminnamen vergeben",25.02.2014
155,"Bitte Terminnamen vergeben",26.02.2014
156,"Bitte Terminnamen vergeben",27.02.2014
157,"Bitte Terminnamen vergeben",31.12.2014

As from the second output with sublimetext, the $headers array doesn't contain anything. 从带有sublimetext的第二个输出开始, $headers数组不包含任何内容。

So the statement $headers[] = sqlsrv_get_field($result , $i); 所以语句$headers[] = sqlsrv_get_field($result , $i); needs to be checked. 需要检查。

As it's furthermore dumping the array instead of printing it, I would try: fputcsv($fp, array_values($headers)); 因为它进一步转储数组而不是打印它,我会尝试: fputcsv($fp, array_values($headers));

In order to get this interpreted by Excel properly, change the delimiter to ; 为了正确解释Excel,请将分隔符更改为; : fputcsv($fp, array_values($headers), ';'); fputcsv($fp, array_values($headers), ';');

The problem is that sqlsrv_get_field() returns the data in the current field and not the name and it has to be setup to use the correct index by calling sqlsrv_fetch(). 问题是sqlsrv_get_field()返回当前字段中的数据而不是名称,并且必须通过调用sqlsrv_fetch()来设置它以使用正确的索引。 But this is not what you want to do. 但这不是你想要做的。 What you need to do is to get the headers which can be done like this: 你需要做的是获得可以这样做的标题:

foreach( sqlsrv_field_metadata($result) as $fieldMetadata)
{
    $headers[] = $fieldMetadata['Name'];
}

and as been already pointed out by Alex the correct way to write the headers to the csv file is: 正如Alex已经指出的那样,将头文件写入csv文件的正确方法是:

fputcsv($fp, array_values($headers));

Now as for the way the file is displayed in Excel you can import the file using the Text import Wizard explained here 现在,关于文件在Excel中的显示方式,您可以使用此处说明的文本导入向导导入文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM