Hướng dẫn đọc file excel bằng lib – php thuần

Bước 1: Tạo file formupload.html

Thêm thư viện đọc file excel

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.13.5/xlsx.full.min.js"></script>
<script src="excel.js"></script>
<input type="file" id="file_upload">
<button id='getData'>Get Data</button><br>

Bước 2: Tạo file excel.js đọc vào gửi data

$(document).ready(function(){
$('#getData').click(function(){
var selectedFile = $('#file_upload')[0].files[0];
var reader = new FileReader();
reader.onload = function(event) {
var data = event.target.result;
var workbook = XLSX.read(data, {
type: 'binary'
});
workbook.SheetNames.forEach(function(sheetName) {
var XL_row_object = XLSX.utils.sheet_to_row_object_array(workbook.Sheets[sheetName]);
var json_object = JSON.stringify(XL_row_object);
console.log(json_object);
$.ajax({
url: 'excel.php',
type: 'POST',
data: 'data_excel=' + json_object,
success:function(data) {
},
error: function (e) {
}
});
})
};
reader.onerror = function(event) {
console.error("File could not be read! Code " + event.target.error.code);
};
reader.readAsBinaryString(selectedFile);
});
});

Kết quả trả về của console.log(json_object);

Bước 3: Tạo file excel.php


GeSHi Error: GeSHi could not find the language php (using path /var/www/html/kb.innocom.vn/wp-content/plugins/wp-synhighlight/geshi/geshi/) (code 2)

Kết quả echo $_POST[‘data_excel’

Leave a Reply

You must be logged in to post a comment.