PhpSpreadsheet is a PHP library for reading and writing spreadsheet files. Import Excel and CSV into MySQL help to save the user time and avoid repetitive work.
Software requirementsPHP version 7.2 or newer to develop using PhpSpreadsheet. Other requirements, such as PHP extensions, are enforced by the composer.
Installation- Download and install CodeIgniter.
- Use Composer to install PhpSpreadsheet into your project:
composer require phpoffice/phpspreadsheet
- Open
application/config/config.php
file and set your vendor directory path.$config['composer_autoload'] = 'vendor/autoload.php';
- Use
phpspreadsheet
the library inside your controller.
The following SQL query creates a
0 table in the MySQL database.
CREATE TABLE `client_info` [
`id` int[10] NOT NULL PRIMARY KEY AUTO_INCREMENT,
`name` varchar[255] COLLATE utf8mb4_unicode_ci NOT NULL,
`country_code` varchar[10] COLLATE utf8mb4_unicode_ci NOT NULL,
`mobile` varchar[100] COLLATE utf8mb4_unicode_ci NOT NULL,
`email` varchar[255] COLLATE utf8mb4_unicode_ci NOT NULL,
`city` varchar[255] COLLATE utf8mb4_unicode_ci NOT NULL,
`ip_address` varchar[100] COLLATE utf8mb4_unicode_ci NOT NULL,
`created_at` datetime NOT NULL,
`updated_at` timestamp NOT NULL DEFAULT current_timestamp[] ON UPDATE current_timestamp[],
`status` int[10] NOT NULL
] ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
Create RoutesOpen
1 file and add the following lines.
Create Model
Create a model file named User_model.php inside the
2 folder.
3 directory. See the following code for the view file.
Upload
Choose File *
Upload excel or csv file only.
Please wait ...
Upload
$[document].ready[function[] {
$["body"].on["submit", "#form-upload-user", function[e] {
e.preventDefault[];
var data = new FormData[this];
$.ajax[{
type: 'POST',
url: "",
data: data,
dataType: 'json',
contentType: false,
cache: false,
processData:false,
beforeSend: function[] {
$["#btnUpload"].prop['disabled', true];
$[".user-loader"].show[];
},
success: function[result] {
$["#btnUpload"].prop['disabled', false];
if[$.isEmptyObject[result.error_message]] {
$[".result"].html[result.success_message];
} else {
$[".sub-result"].html[result.error_message];
}
$[".user-loader"].hide[];
}
}];
}];
}];
Create ViewCreate a view named display.php inside the
3 directory. See the following code for the view file.