表HTML中的选择性列并将其导出为CSV文件

时间:2014-03-12 03:27:46

标签: jquery codeigniter csv

如何选择要导出为CSV文件的列?我尝试隐藏不需要添加到CSV文件中的列,但它仍然获取隐藏的数据。我在这里使用codeigniter和jquery将表格HTML导出到CSV文件中。我这里不使用数据库只是CSV文件和表格html。

下面是我导出CSV文件的代码。这是在VIEW

$(document).ready(function () {

                function exportTableToCSV($table, filename) {

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

                        tmpColDelim = String.fromCharCode(11), 
                        tmpRowDelim = String.fromCharCode(0), 

                        colDelim = '","',
                        rowDelim = '"\r\n"',

                        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();

                                return text.replace('"', '""'); // escape double quotes

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

                        }).get().join(tmpRowDelim)
                            .split(tmpRowDelim).join(rowDelim)
                            .split(tmpColDelim).join(colDelim) + '"',

                        csvData = 'data:application/csv;charset=utf-8,' + encodeURIComponent(csv);

                    $(this)
                        .attr({
                        'download': filename,
                            'href': csvData,
                            'target': '_blank'
                    });
                }

                $(".export").on('click', function (event) {

                    exportTableToCSV.apply(this, [$('#dvData>table'), 'pdw_table.csv']);


                });
            });


<div id="div1" class="row-fluid">
                    <div class="span12" id="dvData">
                            <table class="table table-hover table-striped table-bordered" id="table-data">
                                    <thead>
                    <tr>
                        <th>Field 1</th>
                        <th>Field 2</th>
                        <th>Field 3</th>
                                                    <th style="display: none;"></th>
                        <th>Field 4</th>
                        <th>Field 5</th>
                                                    <th style="display: none;"></th>
                                                    <th style="display: none;"></th>
                        <th>Field 6</th>    
                        <th>Field 7</th>
                        <th>Field 8</th>
                        <th>Field 9</th>
                        <th>Field 10</th>
                        <th>Field 11</th>
                        <th>Field 12</th>                               
                        <th>Field 13</th>
                                                    <th style="display: none;"></th>
                        <th>Field 14</th>
                        <th>Field 15</th>
                        <th>Field 16</th>
                                                    <th>Field 17</th>
                                                    <th style="display: none;"></th>

                    </tr>
                </thead>
                <tbody>
                                        <tr style="display: none;">
                                                    <td>Field 1</td>
                        <td>Field 2</td>
                        <td>Field 3</td>
                                                    <td style="display: none;"></td>
                        <td>Field 4</td>
                        <td>Field 5</td>
                                                    <td style="display: none;"></td>
                                                    <td style="display: none;"></td>
                        <td>Field 6</td>    
                        <td>Field 7</td>
                        <td>Field 8</td>
                        <td>Field 9</td>
                        <td>Field 10</td>
                        <td>Field 11</td>
                        <td>Field 12</td>                               
                        <td>Field 13</td>
                                                    <td style="display: none;"></td>
                        <td>Field 14</td>
                        <td>Field 15</td>
                        <td>Field 16</td>
                                                    <td>Field 17</td>
                                                    <td style="display: none;"></td>
                                               </tr>
                                        <?php foreach($csvData as $field)
                                            {
                                        ?>
                    <tr>

                        <td><?php echo $field['Field 1']; ?></td>
                        <td><?php echo $field['Field 2']; ?></td>
                        <td><?php echo $field['Field 3']; ?></td>
                                                    <td style="display: none;"></td>
                        <td><?php echo $field['Field 4']; ?></td>
                        <td><?php echo $field['Field 5']; ?></td>
                                                    <td style="display: none;"></td>
                                                    <td style="display: none;"></td>
                        <td><?php echo $field['Field 6']; ?></td>
                        <td><?php echo $field['Field 7']; ?></td>
                        <td><?php echo $field['Field 8']; ?></td>
                        <td><?php echo $field['Field 9']; ?></td>
                        <td><?php echo $field['Field 10']; ?></td>
                        <td><?php echo $field['Field 11']; ?></td>
                        <td><?php echo $field['Field 12']; ?></td>
                        <td><?php echo $field['Field 13']; ?></td>
                                                    <td style="display: none;"></td>
                        <td><?php echo $field['Field 14']; ?></td>
                        <td><?php echo $field['Field 15']; ?></td>  
                        <td><?php echo $field['Field 16']; ?></td>
                                                    <td style="display: none;"></td>
                                                    <td><a style="cursor: pointer;" class="btnDelete">Delete</a></td>
                    </tr>
                                        <?php }?> 

                                    <form method="post" name="add1" id="add1" action="<?php echo base_url();?>index.php/datacast_ctr/write_csv" autocomplete="off">
                                          <tr class="td1" id="td1" >  
                                                <td><input type="text" name="val1" id="val1"/></td>
                                                <td><input type="text" name="val2" id="val2"/></td>
                                                <td><input type="text" name="val3" id="val3"/></td>
                                                <td style="display: none;"><input type="hidden" name="valh1" id="valh1"/></td>
                                                <td><input type="text" name="val4" id="val4"/></td>
                                                <td><input type="text" name="val5" id="val5"/></td>
                                                <td style="display: none;"><input type="hidden" name="valh2" id="valh2"/></td>
                                                <td style="display: none;"><input type="hidden" name="valh3" id="valh3"/></td>
                                                <td><input type="text" name="val6" id="val6"/></td>
                                                <td><input type="text" name="val7" id="val7"/></td>
                                                <td><input type="text" name="val8" id="val8"/></td>
                                                <td><input type="text" name="val9" id="val9"/></td>
                                                <td><input type="text" name="val10" id="val10"/></td>
                                                <td><input type="text" name="val11" id="val11"/></td>
                                                <td><input type="text" name="val12" id="val12"/></td>
                                                <td><input type="text" name="val13" id="val13"/></td>
                                                <td style="display: none;"><input type="hidden" name="valh4" id="valh4"/></td>
                                                <td><input type="text" name="val14" id="val14"/></td>
                                                <td><input type="text" name="val15" id="val15"/> </td>
                                                <td><input type="text" name="val16" id="val16"/></td>
                                                <td style="display: none;"><input type="hidden" name="valh5" id="valh5" value="--"/></td>
                                                <td><a onclick="submit();" style="cursor: pointer;">Save</a></td>
                                            </tr>
                                        </form>
                </tbody>
            </table>
                        <div class="span12">
                            <a href="#" class="export">Export CSV file</a>
                        </div>
        </div>  
    </div>

控制器:

function __construct()
{
    parent::__construct();
    $this->load->library('csvreader');
    $this->load->helper('csv');
}

public function index()
{
   $this->load->library('csvreader');

   $filePath = 'C:\xampp\htdocs\Datacast\bin\pdw_table.csv';

   $data['csvData'] = $this->csvreader->parse_file($filePath);

   $this->load->view('datacast_view', $data);
}

function write_csv()
{
    $R1 = $this->input->post('val1');
    $R2 = $this->input->post('val2');
    $R3 = $this->input->post('val3');
    $H1 = $this->input->post('valh1');
    $R4 = $this->input->post('val4');
    $R5 = $this->input->post('val5');
    $H2 = $this->input->post('valh2');
    $H3= $this->input->post('valh3');
    $R6 = $this->input->post('val6');
    $R7 = $this->input->post('val7');
    $R8 = $this->input->post('val8');
    $R9 = $this->input->post('val9');
    $R10 = $this->input->post('val10');
    $R11 = $this->input->post('val11');
    $R12 = $this->input->post('val12');
    $R13 = $this->input->post('val13');
    $H4 = $this->input->post('valh4');
    $R14 = $this->input->post('val14');
    $R15 = $this->input->post('val15');
    $R16 = $this->input->post('val16');
    $H5 = $this->input->post('valh5');

    $data = $R1.",".$R2.",".$R3.",".$H1.",".$R4.",".$R5.",".$H2.",".$H3.",".$R6.",".$R7.",".$R8.",".$R9.",".$R10.",".$R11.",".$R12.",".$R13.",".$H4.",".$R14.",".$R15.",".$R16.",".$H5;
    $list = array($data);

    $file = fopen("./bin/pdw_table.csv","a");

    foreach ($list as $line)
    {
        fputcsv($file,explode(',',$line));
    }

    fclose($file);
    redirect('datacast_ctr');
}

1 个答案:

答案 0 :(得分:0)

尝试将一些特殊属性或类推送到您不想导出的列,例如,推送类“hidden_​​col” 然后,改变

$cols = $row.find('td');

$cols = $row.find('td:not(.hidden_col)');