1

I am developing a web application using php and bootstrap.Uploading an excel sheet is one of the requirements of my application.Now i am partially implemented it (extract the data from excel sheet),the next thing i want upload these data into database table but failed.Here is my code

$file = fopen($filename, "r");
$row=1;

while (($Data = fgetcsv($file, 10000, ",")) !== FALSE)
{
    echo $Data[0];
}
fclose($file);

Here is the output of $Data[0]

enter image description here

I want to store the data in database table with these fields .How can i achieve this?

1

1 Answer 1

2

Here is a sample code of how you should do it . details can be found here . Excel reader library will Handel most of the hustle for you

 ini_set("display_errors",1);
    require_once 'excel_reader2.php';



$data = new Spreadsheet_Excel_Reader("example.xls");

echo "Total Sheets in this xls file: ".count($data->sheets)."<br /><br />";

$html="<table border='1'>";
for($i=0;$i<count($data->sheets);$i++) // Loop to get all sheets in a file.
{
if(count($data->sheets[$i][cells])>0) // checking sheet not empty
{
echo "Sheet $i:<br /><br />Total rows in sheet $i  ".count($data->sheets[$i][cells])."<br />";
for($j=1;$j<=count($data->sheets[$i][cells]);$j++) // loop used to get each row of the sheet
{
$html.="<tr>";
for($k=1;$k<=count($data->sheets[$i][cells][$j]);$k++) // This loop is created to get data in a table format.
{
$html.="<td>";
$html.=$data->sheets[$i][cells][$j][$k];
$html.="</td>";
}

$html.="</tr>";
}
}

}

$html.="</table>";
echo $html;
echo "<br />Data Inserted in dababase";
?>
Sign up to request clarification or add additional context in comments.

6 Comments

here is the error Deprecated: Function split() is deprecated in C:\xampp\htdocs\api\excel_reader2.php on line 844 i am getting
download that source code phpgang.com/… and see if this runs then let me know
preg_split() is the suggested alternative to this function. If you don't require the power of regular expressions, it is faster to use explode(), in the library .. This will work just change this in the library . you have a newer php version that's the problem
I am getting the answer but some errors Use of undefined constant cells - assumed 'cells' in C:\xampp\htdocs\excelphp\index.php on line 16
Deprecated: Assigning the return value of new by reference is deprecated in C:\xampp\htdocs\excelphp\excel_reader2.php on line 916 Notice: iconv(): Detected an incomplete multibyte character in input string in C:\xampp\htdocs\excelphp\excel_reader2.php on line 1718
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.