php 使用codeigniter将数据库导出到excel文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27456209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:22:45  来源:igfitidea点击:

database export to excel file using codeigniter

phpexcelcodeigniter

提问by Md. Hassan Bin Emrul Kayesh

Here I show some partial code for database table export my excel file. The file is written but not right formatted:

在这里,我展示了一些用于数据库表导出我的 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);
}

It is downloaded and extracted with data, but format is not right. I need it like in this image

它被下载并与数据一起提取,但格式不正确。我需要它就像这张图片

回答by Ashish pathak

Step 1: Get MySql data in key value pair.

第一步:在键值对中获取MySql数据。

    Array
(
    [14] => Array
        (
            [Name] => Parvez Alam
            [Age] => 21
            [Gender] => Male
        )

    [15] => Array
        (
            [Name] => Shavez Alam
            [Age] => 21
            [Gender] => Male
        )

)

Step 2: PHP code to get options type and force to browser download file instead of display.

第 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;
}

Step 3: Define html layout for display data in table and button to fire export-to-csv action.

第 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>

Step 4: Now we will use jQuery code to get click event.

第 4 步:现在我们将使用 jQuery 代码来获取点击事件。

    <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>

回答by Tinkeshwar Singh

Just use ','instead of semi-colon in this:

只需','在此使用而不是分号:

$data = $this->dbutil->csv_from_result($query, ';');