如何使用PHPExcel将多个HTML表单数据添加到Excel工作表中

时间:2017-12-23 10:34:39

标签: php excel phpexcel

第一个表单数据插入正常,但是当我尝试添加另一个字段时,它会被覆盖。如何将其添加到工作表中的新行?

当我再次运行代码并提交表单时,新值必须存储在Excel工作表的下一行。

这是我的代码:

<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');

}

?>

2 个答案:

答案 0 :(得分:0)

试试这个

// 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);

答案 1 :(得分:0)

在您的代码中,用于在Excel工作表中添加行。 尝试添加此代码,如下所示。

const uploader: FileUploader = new FileUploader({
  url: URL,
  headers: [{ name: 'Autenfication', value: 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJfaWQiOiI1YTIzNDBkOWE0MDM5YTI2MGM1OWYzNTMiLCJleHAiOjE1MTUyMjc5NDcyMzV9.uVpwN9vrjpoKOzNN_DYOgonB1N46Pl' }]
})

fileItem.withCredentials = false;

这就完成了。

修改 也是先做这个

//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++;

}       

所以最终的代码将是

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

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

修改已在此完成

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.

修改已在此完成

    $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();