从php表导出到.csv,而不创建新的sql表

时间:2015-10-13 13:41:45

标签: php html sql postgresql export-to-csv

对于一个项目,我必须将我所做的报告导出到文件位置。它必须是.csv文件格式并且需要包含标题

<html>
    <?php
    $profiles = new CGenRs("SELECT * from adapt_profile ", $database);
    $profiles->first();
    $users = new CGenRs("SELECT * from authuser where userno in (select user_number from adapt_profile_time where exported = 'f')", $database);
    $users->first();
    $logtime = new CGenRs("SELECT time_id,
  profile_id ,
  user_number ,
  start_time ,
  end_time,
  description,
  exported, EXTRACT(hour FROM(end_time - start_time)) as diff from adapt_profile_time where exported = 'f' order by user_number", $database);
    $logtime->first();
    $timestamp = new CGenRS("SELECT EXTRACT(minute FROM(end_time - start_time))as difference FROM adapt_profile_time WHERE exported='f'", $database);
    $timestamp->first();

    ?>

    <table width="100%" >
        <tr>
            <th align="left">USER NAME</th>
            <th align="left">WORKED FROM</th>
            <th align="left">WORKED TO</th>
            <th align="left">TOTAL</th>
            <th align="left">FOR PROFILE</th>
            <th align="left">DESCRIPTION</th>

    </tr>

    <?php
    $curr_userno = $logtime->valueof('user_number');
    $tot_mins = 0;
    while (!$logtime->eof()) {
        while (!$timestamp->eof()) {


            if ($curr_userno != $logtime->valueof('user_number')) {
                $total_time = floor($tot_mins / 60) . " hours " . ($tot_mins % 60)." minutes"
                ?>
                <tr>

                    <td></td>
                    <td></td>  
                    <td align="right"><b>Total =</b></td>
                    <td colspan="8"><b><?php echo $total_time; ?></b></td>
                    <td></td>
                    <td></td>

                </tr>

                <?php
                $curr_userno = $logtime->valueof('user_number');
                $tot_mins = 0;
            }
            $tot_mins = ($tot_mins + $logtime->valueof('diff') * 60) + $timestamp->valueof('difference');
            ?>
            <tr>
                <td>
                    <?php
                    while (!$users->eof()) {
                        if ($users->valueof('userno') == $logtime->valueof('user_number')) {
                            echo $users->valueof('auth_name') . ' ' . $users->valueof('auth_surname');

                            $users->first();
                            break;
                        }
                        $users->next();
                    }
                    ?>
                </td>

                <td><?php echo $logtime->valueof('start_time') ?></td>
                <td><?php echo $logtime->valueof('end_time') ?></td>
                <td><?php echo $logtime->valueof('diff') . " " . "hours" . " " . $timestamp->valueof('difference') . " " . "minutes"; ?></td>
                <td>
                    <?php
                    while (!$profiles->eof()) {
                        if ($profiles->valueof('profile_id') == $logtime->valueof('profile_id')) {
                            echo $profiles->valueof('profile_name');
                            $profiles->first();
                            break;
                        }
                        $profiles->next();
                    }
                    ?> 
                </td>
                <td><?php echo $logtime->valueof('description') ?></td>
            </tr>

            <?php
            $timestamp->next();
            $logtime->next();
        }
        echo "<hr></hr>";
    }
    $total_time = floor($tot_mins / 60) . " hours " . ($tot_mins % 60)." minutes";
    ?>
    <tr>
        <td> </td>
        <td></td>  
        <td align="right"><b>Total =</b></td>
        <td colspan="8"><b><?php echo $total_time; ?></b></td>
        <td></td>
        <td></td>

    </tr>


    <?php
    ?>
    <tr>
        <td colspan="8"><hr></hr></td>
    </tr>
</table>
    <form id="submit_form" action="" method="post" name="sbt_frm" align="center">
        <input align="center" id="sbt_btn" type="submit" value="Export" name="sbt_btn"></input>
        </form>
</html>

以上是我从上一页生成的报告,该报告输入了工作时间和日期。我想让底部表单在按下导出按钮的同时将我已经制作的页面/报告导出到“location / x”。我怎么会这样做。使用POSTGRESQL 我只需要知道要使用或查看的查询或命令,将进行进一步的研究,然后发布反馈

1 个答案:

答案 0 :(得分:0)

这可能是一个好的开始(使用 Javascript / JQuery ),改编自演示代码中引用的来源:

JSFiddle demo here注意:摆弄版本可能无法在IE中使用 - 尝试使用FF或Chrome。

以下是我使用javascript将表数据提取到字符串中的方法:

var $rows = $table.find('tr:has(td)'),

// Temporary delimiter characters to avoid accidentally splitting the actual contents
tmpColDelim = String.fromCharCode(11), // vertical tab character
tmpRowDelim = String.fromCharCode(0), // null character

// actual delimiter characters for CSV format
colDelim = ',',
rowDelim = '\r\n',

// Grab text from table into CSV formatted string
csv = $rows.map(function(i, row) {
  var $row = $(row),
    $cols = $row.find('td');

  return $cols.map(function(j, col) {
    var $col = $(col),
      text = '="' + $col.text() + '"'; // to make dates interpret literally in excel
    return text.replace(/"/g, '"'); // escape double quotes

  }).get().join(tmpColDelim);

}).get().join(tmpRowDelim)
.split(tmpRowDelim).join(rowDelim)
.split(tmpColDelim).join(colDelim);

IE的行为与其他浏览器不同:

// IE9+ support - using iframe to set up file
// https://github.com/angular-ui/ui-grid/issues/2312#issuecomment-70348120
// note: if using IE10+, consider ieblob: https://github.com/mholt/PapaParse/issues/175#issuecomment-75597039

if (msieversion()) {
  var frame = document.createElement('iframe');
  document.body.appendChild(frame);

  frame.contentWindow.document.open("text/html", "replace");
  frame.contentWindow.document.write('sep=,\r\n' + csv);
  frame.contentWindow.document.close();
  frame.contentWindow.focus();
  frame.contentWindow.document.execCommand('SaveAs', true, fileName);

  document.body.removeChild(frame);
  return true;

} else {
  var uri = 'data:application/csv;charset=utf-8,' + encodeURIComponent(csv);
  $(this).attr({
    'download': fileName,
    'href': uri,
    'target': '_blank'
  });
}

这些只是片段,有关详细信息,请参阅完整的jsfiddle。

我修改了注释掉的代码引用以使用我的表 - 我想如果你想做任何这个客户端,那么表到字符串的位会对你有帮助。