How does Thinkphp5 store the exported excel table in the specified directory of the project directory on the server?

/ *

  • excel table export
  • @ param string $fileName file name
  • @ param array $headArr header name
  • @ param array $data data to be exported
  • @ author static7 * /

function excelExport ($fileName ="", $headArr = [], $data = []) {

$fileName .= "_" . date("Y_m_d", Request::instance()->time()) . ".xls";

$objPHPExcel = new \PHPExcel();

$objPHPExcel->getProperties();

$key = ord("A"); // 

foreach ($headArr as $v) {

    $colum = chr($key);

    $objPHPExcel->setActiveSheetIndex(0)->setCellValue($colum . "1", $v);

    $objPHPExcel->setActiveSheetIndex(0)->setCellValue($colum . "1", $v);

    $key += 1;

}

$column = 2;

$objActSheet = $objPHPExcel->getActiveSheet();

foreach ($data as $key => $rows) { // 

    $span = ord("A");

    foreach ($rows as $keyName => $value) { // 

        $objActSheet->setCellValue(chr($span) . $column, $value);

        $spanPP;

    }

    $columnPP;

}

$fileName = iconv("utf-8", "gb2312", $fileName); // 

$objPHPExcel->setActiveSheetIndex(0); // ,Excel

header("Content-Type: application/vnd.ms-excel");

header("Content-Disposition: attachment;filename="$fileName"");

header("Cache-Control: max-age=0");

$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, "Excel5");

$objWriter->save("php://output"); // 

exit();

}
/ / this is to tell the browser to download the excel file, so how to generate the excel file to the specified directory without telling the browser to download it locally?

Jun.01,2021

has been solved. You can write the server absolute path directly at save
public function get_test ()

.
{
$file = '456.xls';
  $file_path = ROOT_PATH.'uploads/Tmp/'.$file;
$PHPExcel = new \PHPExcel();
$PHPSheet = $PHPExcel->getActiveSheet();
$PHPSheet->setTitle("demo"); //sheet
$PHPSheet->setCellValue("A1","")->setCellValue("B1","");//
$PHPSheet->setCellValue("A2","")->setCellValue("B2","2121");//
$objWriter = \PHPExcel_IOFactory::createWriter($PHPExcel, 'Excel2007');
$objWriter->save($file_path); //
}
Menu