1

我正在使用 phpexcel 库将我的报告导出到 excel 文件。但我想拥有自己的 excel 文件并将其与 phpexcel 一起使用。这是我使用的代码:

public function event_reportexcel($id)
{
    $this->load->library('excel');
    $this->excel->setActiveSheetIndex(0);
    $this->excel->getActiveSheet()->setTitle('test worksheet');
    $this->excel->getActiveSheet()->setCellValue('A1', 'This is just some text value');
    $this->excel->getActiveSheet()->getStyle('A1')->getFont()->setSize(20);
    $this->excel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
    $this->excel->getActiveSheet()->mergeCells('A1:D1');
    $this->excel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

    $filename='just_some_random_name.xls'; //save our workbook as this file name
    header('Content-Type: application/vnd.ms-excel'); //mime type
    header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
    header('Cache-Control: max-age=0'); //no cache


    $objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');  
    //force user to download the Excel file without writing it to server's HD
    $objWriter->save('php://output');
}

它会将我的数据放入一个新的 excel 文件中,但我需要此功能才能使用 excel 文件作为模板。

4

2 回答 2

4

您可以打开文件并根据需要进行编辑,然后保存

$fileType = 'Excel5';
$fileName = 'testFile.xls';

// Read the file
$objReader = PHPExcel_IOFactory::createReader($fileType);
$objPHPExcel = $objReader->load($fileName);

// Change the file
$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', 'Hello')
            ->setCellValue('B1', 'World!');

// Write the file
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, $fileType);
$objWriter->save($fileName);

您可以查看此帖子了解更多详细信息。

于 2013-04-16T14:31:32.880 回答
0

这会帮助你

 public function export(){

        $this->load->model('authors_model');
        $this->load->library('excel');

        $object = new PHPExcel();

        $object->setActiveSheetIndex(0);


         $table_columns = array('ID','first_name','last_name','Email','BirthDate','Added');

         $column = 1;

         foreach ($table_columns as $dd) {
            $object->getActiveSheet()->setCellValueByColumnAndRow($column,1, $dd);
              $column++;
         }


         $employee_data = $this->authors_model->get_all_data();

         $row_no = 2 ;
         foreach ($employee_data as  $value) {

                $object->getActiveSheet()->setCellValueByColumnAndRow(0 ,$row_no,$value->id );
                $object->getActiveSheet()->setCellValueByColumnAndRow(1 ,$row_no,$value->first_name );
                $object->getActiveSheet()->setCellValueByColumnAndRow(2 ,$row_no,$value->last_name );
                $object->getActiveSheet()->setCellValueByColumnAndRow(3 ,$row_no,$value->email );
                $object->getActiveSheet()->setCellValueByColumnAndRow(4 ,$row_no,$value->birthdate );
                $object->getActiveSheet()->setCellValueByColumnAndRow(5 ,$row_no,$value->added );

                $row_no++;
         }


             ob_end_clean(); 
              header('Content-Type: application/vnd.ms-excel'); //mime type

            header("Content-Disposition: attachment; filename=\"filename.xls\"");
            header("Cache-Control: max-age=0");
            $objWriter = PHPExcel_IOFactory::createWriter($object, 'Excel5');
              $objWriter->save('php://output');











}
于 2019-05-26T13:11:26.200 回答