Friday, June 20, 2014

2.0 alternate apache atom ci class codeigniter color css curl directory download error facebook feed


I am a professional web developer, founder of Ingeniarte.com . I build websites using the powerful LAMP stack and different front-end technologies including but not limited to HTML5, ddflimport CSS3 and jQuery.
Most of the time my clients need to download data from their database tables. Exporting to CSV is a pain in the rear for users and it leads to confusion (you know the colon and semicolon stuff). Today, I decided to make a very small controller that is portable and efficient for exporting full MySQL tables to Excel 2003 using PHPExcel and CodeIgniter. First of all, you need PHPExcel which should be installed as a CodeIgniter library. In order to do this, you should follow the steps posted here .
Once you have PHPExcel installed and configured, make a controller exactly like this one: class Table_export extends Controller { function __construct() { parent::Controller(); // Here you should add some sort of user validation // to prevent strangers ddflimport from pulling your table data } function index($table_name) { $query = $this->db->get($table_name); if(!$query) return false; // Starting the PHPExcel library $this->load->library('PHPExcel'); $this->load->library('PHPExcel/IOFactory'); $objPHPExcel = new PHPExcel(); $objPHPExcel->getProperties()->setTitle("export")->setDescription("none"); $objPHPExcel->setActiveSheetIndex(0); // Field names in the first row $fields ddflimport = $query->list_fields(); $col = 0; foreach ($fields as $field) { $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, 1, $field); $col++; } // Fetching the table data $row = 2; foreach($query->result() as $data) { $col = 0; foreach ($fields as $field) { $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $data->$field); $col++; } $row++; } $objPHPExcel->setActiveSheetIndex(0); $objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5'); // Sending headers ddflimport to force the user to download the file header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename="Products_'.date('dMy').'.xls"'); ddflimport header('Cache-Control: max-age=0'); $objWriter->save('php://output'); } }
Whenever you need to export data from a MySQL table, you just need to call this controller and pass the table name as a parameter, sort of like http://www.yoursite.com/table_export/products . Obviously, you should always add some sort of security measure in order to prevent strangers from pulling all your table information. Just add some session protection to your constructor and you’re set.
Só tenho uma dúvida: tentei transformar esse controller em uma lib, passando a query como parâmetro, porém não funcionou. Preciso passar a query como parâmetro devido a necessidade de usar vários joins aintes de exportar.
Hi, please post some code and I’d be glad to help. It should totally ddflimport work with a custom ddflimport query. In fact, most of the time I use custom queries ddflimport to export data. You just need to modify the controller.
while($fila = mysql_fetch_array($rst)) { $objPHPExcel->getActiveSheet()->setCellValue(“A”, $fila[saleman]); $objPHPExcel->getActiveSheet()->setCellValue(“B”, $fila[name_c]); $objPHPExcel->getActiveSheet()->setCellValue(“C”, ddflimport $fila[company]); }
//$objWriter->save($archivo_loc.$archivo_nom); header(‘Content-Type: application/vnd.ms-excel’); header(‘Content-Disposition: attachment;filename=”Report.xls”‘); header(‘Cache-Control: max-age=0′);
When I try to open it, Excel says “the file is corrupt and cannot be opened”. If I try to extract the data (as offered by Excel), it extracts the data and shows it, apparently, with no errors.
hi,
~~~~~~~~~~~~~Controller~~~~~~~~~~~~~ function get_report() { $this->load->dbutil(); // get the object $report = $this->usermodel->index(); //pass it to db utility function $new_report = $this->dbutil->csv_from_result($report); //Now use it to write file. write_file helper function will do it write_file(‘useruploads/break_report.csv’,$new_report); //Done $data = file_get_contents(“useruploads/break_report.csv”); // Read the file’s contents $name = ‘break_report.csv’; force_download($name, $data); }
2.0 alternate apache atom ci class codeigniter color css curl directory download error facebook feed ftp hotlinking htaccess html ie7 images index ipad javascript jquery list listing method mobile mysql option php phpmyadmin posts rows select subdomain tables thumbnails twitter utf-8 validation wordpress www z-index
This space intentionally left blank.

No comments:

Post a Comment