如何修复未在php中下载的phpspreadsheet excel?

时间:2019-03-26 07:12:47

标签: php phpspreadsheet

我正在将Web门户作为我正在工作的公司。现在,我被困在使用PhpSpreadSheet生成Excel报告中。我不能下载文件,而只能从phpfile所在的位置保存excel文件。但是,如果我在我的网址“ Get_summary_excel.php”中调用此文件,它将下载。

这是我在“ Get_summary_excel.php”中的代码

需要“ ../../action/connection.php”;     要求'../ API / vendor / autoload.php';

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup;
use PhpOffice\PhpSpreadsheet\Helper\Sample;
use PhpOffice\PhpSpreadsheet\Style\Border;
// error_reporting(E_ALL);


$filename = "userReports";
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();


$headerstyle = array(
    'font'  => array(
        'bold'  => true,
        'color' => array('rgb' => '000000'),
        'size'  => 12,
        'name'  => 'Verdana'
    ),
    'alignment' => array(
            'horizontal' => \PHPOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
    )
    );

$company_name_style = array(
    'font'  => array(
        'bold'  => true,
        'color' => array('rgb' => '000000'),
        'size'  => 10,
        'name'  => 'Verdana'
    ),
    'alignment' => array(
        'horizontal' => \PHPOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
        'vertical' => \PHPOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER,
    )
    );

$titlestyle = array(
    'font'  => array(
        'bold'  => true,
        'color' => array('rgb' => '000000'),
        'size'  => 14,
        'name'  => 'Verdana'
    ),
    'alignment' => array(
            'horizontal' => \PHPOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
    )
    );

$titledatestyle = array(
    'font'  => array(
        'bold'  => true,
        'color' => array('rgb' => '000000'),
        'size'  => 10,
        'name'  => 'Verdana'
    )
    );

$titleborder = array(
    'borders' => array(
    'allBorders' => array(
          'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN, //BORDER_THIN BORDER_MEDIUM BORDER_HAIR
          'color' => array('rgb' => '000000')
    )
  )
);

$contentstyle = array(
    'alignment' => array(
        'vertical' => \PHPOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_TOP,
        'horizontal' => \PHPOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_RIGHT,
    )
    );


    $sheet->mergeCells('B1:D2');
    $sheet->mergeCells('B5:D6');
    $sheet->mergeCells('B7:D7');

    $sheet->setCellValue('B1',"KABUKLOD SA KAUNLARAN CREDIT, INC.");
    $sheet->setCellValue('B5',"REPORT");
    $sheet->setCellValue('B7',date("M d Y"));
    $sheet->setCellValue('B9', 'Subject')
          ->setCellValue('C9', 'Datetime')
          ->setCellValue('D9', 'Status');

    foreach(range('B','D') as $columnID) {
        $sheet->getColumnDimension($columnID)->setAutoSize(true);
    }

        $string =   "solved,pending";
        $array  =   array_map("strval",explode(',', $string));
        $array  =   implode("','",$array);

        $query  =   "SELECT * FROM tbl_concern WHERE status IN ('".$array."') ORDER BY status";
        $stmt = $con->prepare($query);
        $stmt->execute();
        $result = $stmt->get_result();

        $counter = 10;
        while($row = $result->fetch_array()){
            $sheet->setCellValue('B'.$counter, ''.$row['subject'])
                ->setCellValue('C'.$counter, ''.$row['datetime'])
                ->setCellValue('D'.$counter, ''.$row['status']);
                $counter++;
                //   $sheet->getRowDimension(6)->setRowHeight(40);
        }

    $sheet->getColumnDimension('B')->setAutoSize(false);
    $sheet->getColumnDimension('D')->setAutoSize(false);
    $sheet->getColumnDimension('B')->setWidth(30);
    $sheet->getColumnDimension('D')->setWidth(20);
    // $sheet->getRowDimension('4')->setRowHeight(20);
    $sheet->getStyle('B1')->applyFromArray($company_name_style);
    $sheet->getStyle('B9:B'.$sheet->getHighestRow())->getAlignment()->setWrapText(true);
    $sheet->getStyle('B5')->applyFromArray($titlestyle);
    $sheet->getStyle('B7')->applyFromArray($titledatestyle);
    $sheet->getStyle('B5:D7')->applyFromArray($titleborder);
    $sheet->getStyle('C5:D'.$sheet->getHighestRow())->applyFromArray($contentstyle);
    $sheet->getStyle('B9:D9')->applyFromArray($headerstyle);
    $sheet->getStyle('B9:D9')->getFont()->setBold(true);
    $sheet->setShowGridLines(false);



    $spreadsheet->getActiveSheet()->setTitle('userReport');

    $spreadsheet->setActiveSheetIndex(0);

    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header('Content-Disposition: attachment;filename="'.$filename.'.xlsx"');
    header('Cache-Control: max-age=0');
    header('Cache-Control: max-age=1');

    $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
    $writer->save('php://output');
    exit;

这是我的JavaScript

$.ajax({
    type: "GET",
    url: "../phpfile/GET_summary_excel.php",
    data: "solved=" + d_solved + "&ongoing=" + d_ongoing + "&pending=" + 
    d_pending + "&fileformat=" + type,
    beforeSend: function () {

    },
    success: function (msg) {
        console.log(msg);
    }
});

我希望用户下载报告。

1 个答案:

答案 0 :(得分:0)

您无需创建ajax请求,只需打开一个指向“ Get_summary_excel.php” URL的新选项卡,并使用所有参数即可下载文件。