简体   繁体   中英

How to add multiple HTML form data into Excel sheet using PHPExcel

The first form data is being inserted just fine, but when I try to add another field, it gets overwritten. How can I make it add on the new row in the sheet?

When I run the code again and submit the form, the new values must be stored on the next row in the Excel sheet.

Here is my code:

<form action="write_excel.php" method="post">
<input type="text" name="fname" id="fname"/>
<input type="text" name="lname" id="lname"/>
<input type="email" name="email" id="email"/>
<textarea name="des" id="des"></textarea>
<button type="submit" value="submit">Submit</submit>
</form>
<?php

$name1=$_POST['fname'];
$name2=$_POST['lname'];
$email=$_POST['email'];
$des=$_POST['des'];
//The Header Row
$Header = array('Firstname', 'LastName','email','Designation');
$data = array();

//Data to be written in the excel sheet -- Sample Data
array_push($data, array($name1 ,$name2,$email,$des));

$filename = write_excel1($data, $Header);


function write_excel1($data, $Header)
{
    //We are using PHPExcel Library for creating the Microsoft Excel file
    require_once  './PHPExcel/Classes/PHPExcel.php';

    $objPHPExcel = new PHPExcel();
    //Activate the First Excel Sheet
    $ActiveSheet = $objPHPExcel->setActiveSheetIndex(0);


    //Write the Header
    $i=0;
    foreach($Header as $ind_el)
    {
        //Convert index to Excel compatible Location
        $Location = PHPExcel_Cell::stringFromColumnIndex($i) . '1';
        $ActiveSheet->setCellValue($Location, $ind_el);
        $i++;
    }

    //this piece of code use to add rows in excel sheet.
    //Insert that data from Row 2, Column A (index 0)
    $rowIndex=2;
    // echo $rowIndex;
    $columnIndex=0; //Column A
    foreach($data as $row)
    {           
        foreach($row as $ind_el)
        {       

            $Location = PHPExcel_Cell::stringFromColumnIndex($columnIndex) . $rowIndex;
            //var_dump($Location);
            $ActiveSheet->setCellValue($Location, $ind_el);     //Insert the Data at the specific cell specified by $Location
            $columnIndex++;
        }

        $rowIndex++;

    }       

    //1. Mark the Header Row  in Color Red
    $Range = 'A1:B1:C1:D1';
    $color = 'FFFF0000';
    $ActiveSheet->getStyle($Range)->getFill($Range)->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setARGB($color);

    //2. Set the Column Width

    for($i=0; $i<count($Header);$i++)
    {
        $Location = PHPExcel_Cell::stringFromColumnIndex($i) ;
        $ActiveSheet->getColumnDimension($Location)->setAutoSize(true); 
    }


    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
    //Result File name
    $objPHPExcel = PHPExcel_IOFactory::load("myfile.xlsx");

    $objWriter->save('myfile.xlsx');

}

?>

Try from this

// create new PHPExcel object
$objPHPExcel = new PHPExcel;
$objPHPExcel->setActiveSheetIndex(0); 

// set default font
$objPHPExcel->getDefaultStyle()->getFont()->setName('Calibri');
// set default font size
$objPHPExcel->getDefaultStyle()->getFont()->setSize(10);
$objSheet = $objPHPExcel->getActiveSheet();
$objSheet->getStyle('A1:J1')->getFont()->setSize(12);

$objSheet->getProtection()->setSheet(true);//do not allow editing in 
excelsheet

$rowCount = 2;
while($row = mysqli_fetch_array($result,MYSQLI_BOTH)){
 $objSheet->getStyle('A'.$rowCount)->getFont()->setSize(10);
 $objSheet->getCell('A'.$rowCount)->setValue($value1);
 $objSheet->getStyle('B'.$rowCount)->getFont()->setSize(10);
 $objSheet->getCell('B'.$rowCount)->setValue($value2);

 $rowCount++; 
} 
$objSheet->getColumnDimension('A')->setAutoSize(true);//adjust 
excelsheet column value
$objSheet->getColumnDimension('B')->setAutoSize(true); //adjust 
excelsheet column value

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 
"Excel2007");
 $objWriter->save($filename);

in your code for adding rows in excel sheet. try to add this code as shown below.

//Insert that data from Row 2, Column A (index 0)
// $rowIndex=2;
// echo $rowIndex;
// getting the highest row.
$rowIndex= $objPHPExcel->setActiveSheetIndex(0)->getHighestRow();
$rowIndex++; // increment the highest row with 1
$columnIndex=0; //Column A
foreach($data as $row)
{           
    foreach($row as $ind_el)
    {       

        $Location = PHPExcel_Cell::stringFromColumnIndex($columnIndex) . $rowIndex;
        //var_dump($Location);
        $ActiveSheet->setCellValue($Location, $ind_el);     //Insert the Data at the specific cell specified by $Location
        $columnIndex++;
    }

    $rowIndex++;

}       

And this is done.

Edit also do this first

//Result File name
$objPHPExcel = PHPExcel_IOFactory::load("myfile.xlsx");

$objWriter->save('myfile.xlsx');

So the final code will be

function write_excel1($data, $Header)
{
    //We are using PHPExcel Library for creating the Microsoft Excel file
    require_once  './PHPExcel/Classes/PHPExcel.php';

    //load your excel file here first.

Edit has done here

    $inputFileType = PHPExcel_IOFactory::identify("myfile.xlsx");
    $objReader = PHPExcel_IOFactory::createReader($inputFileType);
    $objPHPExcel = $objReader->load("myfile.xlsx");
    //Activate the First Excel Sheet
    $ActiveSheet = $objPHPExcel->setActiveSheetIndex(0);


    //Write the Header
    $i=0;
    foreach($Header as $ind_el)
    {
        //Convert index to Excel compatible Location
        $Location = PHPExcel_Cell::stringFromColumnIndex($i) . '1';
        $ActiveSheet->setCellValue($Location, $ind_el);
        $i++;
    }

    //Insert that data from Row 2, Column A (index 0)
    // $rowIndex=2;
    // echo $rowIndex;
    // getting the highest row.
    $rowIndex= $objPHPExcel->setActiveSheetIndex(0)->getHighestRow();

Edit has done here

    $rowIndex++; // increment the highest row with 1

    foreach($data as $row)
    {           
        $columnIndex=0; //Column A
        foreach($row as $ind_el)
        {       

            $Location = PHPExcel_Cell::stringFromColumnIndex($columnIndex) . $rowIndex;
            //var_dump($Location);
            $ActiveSheet->setCellValue($Location, $ind_el);     //Insert the Data at the specific cell specified by $Location
            $columnIndex++;
        }

        $rowIndex++;

    }       

    //1. Mark the Header Row  in Color Red
    $Range = 'A1:B1:C1:D1';
    $color = 'FFFF0000';
    $ActiveSheet->getStyle($Range)->getFill($Range)->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setARGB($color);

    //2. Set the Column Width

    for($i=0; $i<count($Header);$i++)
    {
        $Location = PHPExcel_Cell::stringFromColumnIndex($i) ;
        $ActiveSheet->getColumnDimension($Location)->setAutoSize(true); 
    }


    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
    //Result File name

    $objWriter->save('myfile.xlsx');

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM