I was thinking since i can import the data from excel to my database, is it possible to upload the excel file itself and be able to display it in your website?
Asked
Active
Viewed 788 times
-2
-
Yes it is possible; can you post your code, the one that you tried? – Funk Forty Niner Oct 09 '17 at 01:51
-
i havent tried this one yet, the one that i tried was importing the data. I am currently searching for some tutorials on how to do this one. – keme Oct 09 '17 at 01:57
1 Answers
0
For client webpage side, use html form <input type="file">
to upload excel file from browser to PHP file.
Example upload-submit.html:
<form action="upload.php">
<input type="file" name="excel-upload" />
<input type="submit" />
</form>
In your PHP upload script you will need to include the PHPExcel library (https://github.com/PHPOffice/PHPExcel) to read and parse the uploaded Excel file. You can read Mark Baker's very good example on how to read from Excel and insert into a database: how to use phpexcel to read data and insert into database?
Example upload.php :
// Include PHPExcel_IOFactory
include 'PHPExcel/IOFactory.php';
// Move the uploaded file from temporary server directory into an 'uploads' directory
$fileName = basename($_FILES["excel-upload"]["name"]);
$uploadedExcelFullPath = __DIR__.'/uploads/'.$fileName;
move_uploaded_file($_FILES['excel-upload']['tmp_name'],$uploadedExcelFullPath);
// Read your Excel workbook
try {
$inputFileType = PHPExcel_IOFactory::identify($uploadedExcelFullPath);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($uploadedExcelFullPath);
} catch(Exception $e) {
die('Error loading file "'.pathinfo($uploadedExcelFullPath,PATHINFO_BASENAME).'": '.$e->getMessage());
}
// Get worksheet dimensions
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow();
$highestColumn = $sheet->getHighestColumn();
// Loop through each row of the worksheet in turn
for ($row = 1; $row <= $highestRow; $row++){
// Read a row of data into an array
$rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row,
NULL,
TRUE,
FALSE);
// Insert row data array into your database here
// You can also format data into HTML table row format and echo out here
var_dump($rowData);
}

Kevin HR
- 249
- 1
- 6
-
-
it aint working and it's a long one, is there any other easy and short way to do it, i mean uploading csv file iiself not data in it. Im trying to upload a csv file into database then display it on my webpage and make it downloadable. – keme Oct 12 '17 at 11:13