• logo
  • PHP
  • PHP OOPs
  • script
    • JavaScript
    • JQuery
    • Ajax
    • AngularJs
    • VueJs
    • NodeJs
    • ReactJs
  • wordpress
  • Codeigniter
  • Codeigniter 4
  • Laravel
  • Python
  • MySql
  • Json
  • C
  • C++
  • More ...
    • Cakephp Framework
    • MongoDb
    • MySqli PDO
    • .htaccess
    • HTML 5
    • CSS
    • SEO
    • DCA

Codeigniter Tutorials

  • What is codeigniter?
  • Application_Architecture
  • MVC Architecture
  • HMVC Architecture
  • Codeigniter Configuration
  • Remove index.php from url in codeigniter
  • MVC Concept
  • View
  • Alternate PHP Syntax for View Files
  • Routing
  • Codeigniter URL
  • Get Current URL
  • Previous page URL get
  • Seo Friendly URL
  • Slug Create in codeigniter
  • What is _remap() function
  • Remove controller name from url in codeigniter
  • Codeigniter Controller Class
  • Class Constructor
  • GET $ POST method in Codeigniter
  • Models
  • Basepath, Apppath, FCPATH
  • URI Segment
  • Page Redirect
  • Helper class
  • Custom Helper class
  • Form Helper
  • Common Helper Functions
  • Common Function
  • Array Problems
  • Call controller in Helper
  • Add active class to menu using Helper class
  • Custom Library
  • Custom Library Example
  • when to use get_instance()
  • Codeigniter Hook
  • how to work inline css in codeigniter
  • Custom 404 page
  • 404 custom error page
  • Create custom config file in codeigniter
  • How to set and get config item value
  • How to Speed Up CodeIgniter App?
  • Codeigniter Functions
  • Session
  • cookies
  • How to Set & Get Tempdata in Codeigniter
  • flash messages in Codeigniter
  • Flashdata
  • Encryption and Decryption In CodeIgniter
  • Codeigniter security
  • csrf token form security
  • Password Hashing
  • Form Validation
  • Custom Validation
  • Registration Form with validation
  • Server Side Form Validation
  • Validate Select Option Field
  • Date Format Validation
  • Date Format change in codeigniter
  • Date Functions
  • DOB Validation
  • CI CRUD
  • User SignUp
  • User Login
  • User Logout
  • Login Account
  • Login form with RememberMe
  • Login Form with session
  • User change password
  • Change Password with Callback Validation to Check Old Password
  • Forgot password
  • Reset password
  • Insert data in database
  • Fetch data from database
  • Update data in database
  • Delete data in database
  • File Upload
  • Image Upload with resize Image
  • Upload Multiple file and images
  • Upload Multiple images with CRUD
  • File and image update
  • Upload Image Using Ajax.
  • Email Send
  • Email Send Using Email library
  • Email Send Using SMTP Gmail
  • Notification send
  • store data in json format in DB
  • Json parse
  • Fetch data Using Ajax with Json data
  • How to Show data Using Ajax with Json parse
  • Get JSON Data from PHP Script using jQuery Ajax
  • Insert data Using Ajax
  • Submit data Using Ajax with form validation
  • How to show data Using Ajax in codeigniter
  • Insert & Update Using Ajax
  • Registration Form With Validation Using Ajax in codeigniter
  • Delete data Using Ajax Confirmation
  • Delete All data Using checkbox selection
  • Ajax CSRF Token
  • Ajax Post
  • Ajax serverside form validation
  • Contact form using AJAX with form validation
  • DataTable Using Ajax dynamically
  • DataTables pagination using AJAX with Custom filter
  • DataTables AJAX Pagination with Search and Sort in codeigniter
  • DataTables in Codeigniter using Ajax
  • Ajax Custom Serarch
  • Ajax Live Data Search using Jquery PHP MySql
  • Ajax Custom Serarch and sorting in datatable
  • Dynamic Search Using Ajax
  • Autocomplete using jquery ajax
  • Jquery Ajax Autocomplete Search using Typeahead
  • Dynamic Dependent Dropdown Using Ajax
  • Dynamic Dependent Dropdown list Using Ajax
  • Dynamic Dependent Dropdown in codeigniter using Ajax
  • ajax username/email availability check using JQuery
  • Check Email Availability Using Ajax
  • Data Load on mouse scroll
  • Ajax CI Pagination
  • Pagination in codeigniter
  • Ajax Codeigniter Pagination
  • email exists or not using ajax with json
  • CRUD using AJAX With Modal popup in CI
  • Add / Show Data on modal popup using Ajax
  • Modal popup Validation using Ajax
  • Data show on Modal popup Using Ajax
  • Add / Remove text field dynamically using jquery ajax
  • How to Add/Delete Multiple HTML Rows using JavaScript
  • Delete Multiple Rows using Checkbox
  • Multiple Checkbox value
  • Form submit using jquery Example
  • REST & SOAP API
  • Multi-Language implementation in CodeIgniter
  • How to pass multiple array in view
  • Captcha
  • create zip file and download
  • PhpOffice PhpSpreadsheet Library (Export data in excel sheet)
  • data export in excel sheet
  • Excel File generate in Codeigniter using PHPExcel
  • Dompdf library
  • tcpdf library
  • Html table to Excel & docs download
  • CI Database Query
  • Database Query
  • SQL Injection Prevention
  • Auth Model
  • Join Mysql
  • Tree View in dropdown option list
  • OTP Integration in codeigniter
  • curl post
  • download file using curl
  • Sweet Alert
  • Sweet alert Delete & Success
  • Log Message in Codeigniter
  • Menu & Submenu show dynamically
  • Set Default value in input box
  • Cron Jobs
  • Stored Procedure
  • Display Loading Image when AJAX call is in Progress
  • Send SMS
  • IP Address
  • Codeigniter Tutorialspoint
  • Website Link
  • How To Create Dynamic Xml Sitemap In Codeigniter
  • Paypal Payment Integration
  • Get Latitude and Longitude From Address in Codeigniter Using google map API
  • How To Create Simple Bar Chart In Codeigniter Using AmCharts?
  • dynamic Highcharts in Codeigniter
  • Barcode in Codeigniter
  • Codeigniter Interview Questions
  • Project
Home » Codeigniter » 

How to use phpOffice / PhpSpreadsheet Library in codeigniter?


PhpOffice PhpSpreadsheet is a library purely made in PHP. This library created to provide set of classes allowing you to read and write spreadsheet files such as excel, libreoffice etc.

File Name :

Open your terminal. Check your current working directory using pwd command (You must be inside of your project folder). Enter the command composer require phpoffice/phpspreadsheet to download PhpOffice PhpSpreadSheet library and it’s dependencies.

Install PhpOffice PhpSpreadSheet Library

File Name :

Step 3: Set Vendor Directory Path
$config[‘composer_autoload’] = ‘vendor/autoload.php’;

application/config/config.php
$config[‘composer_autoload’] = ‘vendor/autoload.php’;

Use PhpOffice/PhpSpreadSheet Library

File Name :

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

require 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

class ExportExcelFile extends CI_Controller {
/**
* Index Page for this controller.
*
* Maps to the following URL
* http://example.com/index.php/welcome
* - or -
* http://example.com/index.php/welcome/index
* - or -
* Since this controller is set as the default controller in
* config/routes.php, it's displayed at http://example.com/
*
* So any other public methods not prefixed with an underscore will
* map to /index.php/welcome/<method_name>
* @see https://codeigniter.com/user_guide/general/urls.html
*/

public function index() {

$spreadsheet = new Spreadsheet(); // instantiate Spreadsheet

$sheet = $spreadsheet->getActiveSheet();

// manually set table data value
$sheet->setCellValue('A1', 'Gipsy Danger');
$sheet->setCellValue('A2', 'Gipsy Avenger');
$sheet->setCellValue('A3', 'Striker Eureka');

$writer = new Xlsx($spreadsheet); // instantiate Xlsx

$filename = 'list-of-jaegers'; // set filename for excel file to be exported

header('Content-Type: application/vnd.ms-excel'); // generate excel file
header('Content-Disposition: attachment;filename="'. $filename .'.xlsx"');
header('Cache-Control: max-age=0');

$writer->save('php://output'); // download file
}

}

File Name :


File Name :


How to show table data in excel sheet.

File Name : Emp.php Controller

<?php
if (!defined('BASEPATH'))exit('No direct script access allowed');


use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

class Emp extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('Auth_model');
}
public function index() {
$data['page'] = 'export-excel';
$data['title'] = 'Export Excel data';
$data['employeeData'] = $this->Auth_model->employeeList();

$this->load->view('emp', $data);
}
public function createExcel() {
$fileName = 'employee.xlsx';
$employeeData = $this->Auth_model->employeeList();
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'Id');
$sheet->setCellValue('B1', 'Name');
$sheet->setCellValue('C1', 'Skills');
$sheet->setCellValue('D1', 'Address');
$sheet->setCellValue('E1', 'Age');
$sheet->setCellValue('F1', 'Designation');
$rows = 2;
foreach ($employeeData as $val){
$sheet->setCellValue('A' . $rows, $val['id']);
$sheet->setCellValue('B' . $rows, $val['name']);
$sheet->setCellValue('C' . $rows, $val['skills']);
$sheet->setCellValue('D' . $rows, $val['address']);
$sheet->setCellValue('E' . $rows, $val['age']);
$sheet->setCellValue('F' . $rows, $val['designation']);
$rows++;
}
$writer = new Xlsx($spreadsheet);
$writer->save("uploads/".$fileName);
header("Content-Type: application/vnd.ms-excel");
redirect(base_url()."/uploads/".$fileName);
}
}
?>

Model

File Name :

/* ############## download mysql data in excel sheet ################# */
public function employeeList() {
$this->db->select(array('id', 'name', 'skills', 'address', 'designation', 'age'));
$this->db->from('emp');
$this->db->limit(10);
$query = $this->db->get();
return $query->result_array();
}
/* ###################################### */

view

File Name : emp


<table class="table table-hover tablesorter">
<thead>
<tr>
<th class="header">Id.</th>
<th class="header">Name</th>
<th class="header">Skills</th>
<th class="header">Address</th>
<th class="header">Age</th>
<th class="header">Designation</th>
</tr>
</thead>
<a class="pull-right btn btn-warning btn-large" style="margin-right:40px" href="<?php echo base_url(); ?>/emp/createexcel"><i class="fa fa-file-excel-o"></i> Export to Excel</a>
<tbody>
<?php
if (isset($employeeData) && !empty($employeeData)) {
foreach ($employeeData as $key => $emp) {
?>
<tr>
<td><?php echo $emp['id']; ?></td>
<td><?php echo $emp['name']; ?></td>
<td><?php echo $emp['skills']; ?></td>
<td><?php echo $emp['address']; ?></td>
<td><?php echo $emp['age']; ?></td>
<td><?php echo $emp['designation']; ?></td>
</tr>
<?php
}
} else {
?>
<tr>
<td colspan="5" class="alert alert-danger">No Records founds</td>
</tr>
<?php } ?>
</tbody>
</table>

download phpoffice package

File Name :

Download phpOffice package

File Name :


Example :

File Name : Controller

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
class PhpspreadsheetController extends CI_Controller {
public function __construct(){
parent::__construct();
}
public function index(){
$this->load->view('spreadsheet');
}
public function export(){
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'Hello World !');
$writer = new Xlsx($spreadsheet);
$filename = 'name-of-the-generated-file';
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'. $filename .'.xlsx"');
header('Cache-Control: max-age=0');
$writer->save('php://output'); // download file
}
public function import(){
$file_mimes = array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel', 'text/plain', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
if(isset($_FILES['upload_file']['name']) && in_array($_FILES['upload_file']['type'], $file_mimes)) {
$arr_file = explode('.', $_FILES['upload_file']['name']);
$extension = end($arr_file);
if('csv' == $extension){
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Csv();
} else {
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
}
$spreadsheet = $reader->load($_FILES['upload_file']['tmp_name']);
$sheetData = $spreadsheet->getActiveSheet()->toArray();
echo "<pre>";
print_r($sheetData);
}
}
}

View

File Name : spreadsheet.php

<html>
<head>
<title>Import/Export using phpspreadsheet in codeigniter</title>
</head>
<body>
<style>
h3
{
font-family: Verdana;
font-size: 14pt;
font-style: normal;
font-weight: bold;
color:red;
text-align: center;
}
table.tr{
font-family: Verdana;
color:black;
font-size: 12pt;
font-style: normal;
font-weight: bold;
text-align:left;
}
</style>
<h3><u>Import/Export using phpspreadsheet in codeigniter</u></h3>
<?php echo form_open_multipart('spreadsheet/import',array('name' => 'spreadsheet')); ?>
<table align="center" cellpadding = "5">
<tr>
<td>File :</td>
<td><input type="file" size="40px" name="upload_file" /></td>
<td class="error"><?php echo form_error('name'); ?></td>
<td colspan="5" align="center">
<input type="submit" value="Import Users"/></td>
</tr>
</table>
<?php echo form_close();?>
</body>
</html>

Route

File Name :

$route['spreadsheet'] = 'PhpspreadsheetController';
$route['spreadsheet/import'] = 'PhpspreadsheetController/import';
$route['spreadsheet/export'] = 'PhpspreadsheetController/export';

File Name :





Itechtuto

Connect Us Socially:

Quick Links

  • itech
  • About Us
  • Feedback
  • Trademarks
  • Privacy Policy
  • Terms of Use
  • Sitemap
  • Trademarks
  • Privacy Policy
  • Terms of Use
  • Sitemap

Copyright © 2016 itechxpert (P) Ltd. All rights reserved.

Copyright © 2016 itechxpert (P) Ltd. All rights reserved.