Jump to content

Export to excel


umishra

Recommended Posts

Hi All,

 

I need a bit of help...

I want to export some data from the webpage,which is being populated from the Database.

The data is spread across 5 Pages and each page has 25 rows per page.

Now,when I export this data to excel,it exports only the 1st page.Even going to the 2nd page and trying exporting,exports the 1st page only.I have a piece of code which is like

if( IMPORT_EXPORT_TO_EXCEL ) {
        if( isset($headers) ) {

                # HEADER ROW
                $row = $rows[0];
                $header = "";
                $data = "";

                foreach($headers as $key => $value) {

                        # Replace double quotes with two double quotes
                        $value = str_replace('"', '""', $value);
                        $header .= '"'. $value .'"'. "\t";
                }
                //print "$header\n";exit;
                # DATA ROWS
                //print_r($headers);exit;
                foreach($rows as $row) {

                        $data_row = "";
                        $k=0;
                        foreach($row as $key => $value) {

                                //print"$key = $value<br>";
                                $data_field = str_replace( '"', '""', util_unhtmlentities( util_strip_html_tags($row[$key]) ) );
                                //print "$data_field\n";exit;
                                //if (($k!=0) AND ($k<11))      {
                                //      $data_row .= '"'. $data_field .'"'. "\t";
                                //}
                                if ($type == "results") {
                                        if (($k > 0) and ($k < 11))     {
                                                $data_row .= '"'. $data_field .'"'. "\t";
                                        }

                                }
                                else    {
                                        $data_row .= '"'. $data_field .'"'. "\t";
                                }

                                $k++;
                        }

                        $data .= trim($data_row)."". NEWLINE;
                }
        else {

                # HEADER ROW
                $row = $rows[0];
                $data_row = "";

                foreach($row as $key => $value) {

                        # Replace double quotes with two double quotes
                        $data_field = str_replace('"', '""', $key);

                        $data_row .= '"'.$data_field.'"\t';
                }

                $data .= substr($data_row, 0, -1)."". NEWLINE;

                # DATA ROWS
                foreach($rows as $row) {

                        $data_row = "";

                        foreach($row as $key => $value) {

                                $data_field = str_replace('"', '""', util_unhtmlentities( util_strip_html_tags($value) ) );

                                $data_row .= '"'.$data_field.'"\t';
                        }

                        $data .= substr($data_row, 0, -1)."". NEWLINE;

                }
        }


        $headers = str_replace("\r","",$header);
        $data = str_replace("\r","",$data);
//exit;
        # OUTPUT
        header("Content-type: application/x-msdownload");
        header("Content-Disposition: attachment; filename=\"$_GET[table].xls\"");
        header("Pragma: no-cache");
        header("Expires: 0");
        print "$header\n$data";

}

 

Link to comment
https://forums.phpfreaks.com/topic/154090-export-to-excel/
Share on other sites

Any updates on this?

Hi All,

 

I need a bit of help...

I want to export some data from the webpage,which is being populated from the Database.

The data is spread across 5 Pages and each page has 25 rows per page.

Now,when I export this data to excel,it exports only the 1st page.Even going to the 2nd page and trying exporting,exports the 1st page only.I have a piece of code which is like

if( IMPORT_EXPORT_TO_EXCEL ) {
        if( isset($headers) ) {

                # HEADER ROW
                $row = $rows[0];
                $header = "";
                $data = "";

                foreach($headers as $key => $value) {

                        # Replace double quotes with two double quotes
                        $value = str_replace('"', '""', $value);
                        $header .= '"'. $value .'"'. "\t";
                }
                //print "$header\n";exit;
                # DATA ROWS
                //print_r($headers);exit;
                foreach($rows as $row) {

                        $data_row = "";
                        $k=0;
                        foreach($row as $key => $value) {

                                //print"$key = $value<br>";
                                $data_field = str_replace( '"', '""', util_unhtmlentities( util_strip_html_tags($row[$key]) ) );
                                //print "$data_field\n";exit;
                                //if (($k!=0) AND ($k<11))      {
                                //      $data_row .= '"'. $data_field .'"'. "\t";
                                //}
                                if ($type == "results") {
                                        if (($k > 0) and ($k < 11))     {
                                                $data_row .= '"'. $data_field .'"'. "\t";
                                        }

                                }
                                else    {
                                        $data_row .= '"'. $data_field .'"'. "\t";
                                }

                                $k++;
                        }

                        $data .= trim($data_row)."". NEWLINE;
                }
        else {

                # HEADER ROW
                $row = $rows[0];
                $data_row = "";

                foreach($row as $key => $value) {

                        # Replace double quotes with two double quotes
                        $data_field = str_replace('"', '""', $key);

                        $data_row .= '"'.$data_field.'"\t';
                }

                $data .= substr($data_row, 0, -1)."". NEWLINE;

                # DATA ROWS
                foreach($rows as $row) {

                        $data_row = "";

                        foreach($row as $key => $value) {

                                $data_field = str_replace('"', '""', util_unhtmlentities( util_strip_html_tags($value) ) );

                                $data_row .= '"'.$data_field.'"\t';
                        }

                        $data .= substr($data_row, 0, -1)."". NEWLINE;

                }
        }


        $headers = str_replace("\r","",$header);
        $data = str_replace("\r","",$data);
//exit;
        # OUTPUT
        header("Content-type: application/x-msdownload");
        header("Content-Disposition: attachment; filename=\"$_GET[table].xls\"");
        header("Pragma: no-cache");
        header("Expires: 0");
        print "$header\n$data";

}

Link to comment
https://forums.phpfreaks.com/topic/154090-export-to-excel/#findComment-810501
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.