php PHPExcel 保存文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17021449/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:07:20  来源:igfitidea点击:

PHPExcel save file

phpphpexcel

提问by Alessandro Minoccheri

Hi all I have a site and i want to create an excel file and save into my folder into my server. I have tried in this mode but every time ask me to download it. I don't want that ask to download because after I have to create many xls is a report and isn't necessary to download but only to save into a folder. This is my code:

大家好,我有一个站点,我想创建一个 excel 文件并将其保存到我的文件夹中到我的服务器中。我曾尝试过这种模式,但每次都要求我下载它。我不希望要求下载,因为在我必须创建许多 xls 之后是一份报告,不需要下载,而只需保存到文件夹中。这是我的代码:

require_once 'inc/PHPExcel.php';

// Create new PHPExcel object
$objPHPExcel = new PHPExcel();

// Set document properties
$objPHPExcel->getProperties()->setCreator("Alessandro Minoccheri")
                             ->setLastModifiedBy("Alessandro Minoccheri")
                             ->setTitle("Office 2007 XLSX Test Document")
                             ->setSubject("Office 2007 XLSX Test Document")
                             ->setDescription("Generazione report inverter")
                             ->setKeywords("office 2007 openxml php")
                             ->setCategory("");


// Add some data
$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', 'Hello')
            ->setCellValue('B2', 'world!')
            ->setCellValue('C1', 'Hello')
            ->setCellValue('D2', 'world!');

// Miscellaneous glyphs, UTF-8
$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A4', 'Miscellaneous glyphs')
            ->setCellValue('A5', 'éàèùaê?????ü???ü?');

// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle('Simple');


// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);


// Redirect output to a client's web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="01simple.xls"');
header('Cache-Control: max-age=0');

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;

I have also tried:

我也试过:

$objWriter->save('namee.xls');

But again ask me to download.

但再次要求我下载。

How can I solve this?

我该如何解决这个问题?

回答by hek2mgl

That's because the following headers:

那是因为以下标题:

// Redirect output to a client's web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="01simple.xls"');
header('Cache-Control: max-age=0');

That's because browsers will open the save as... box if they see the Content-Dispositionheader. Remove those lines.

这是因为浏览器会在看到Content-Disposition标题时打开另存为...框。删除这些行。

However, the excel should being saved on disk anyway. Don't you see it? if not, make sure that PHP has proper permissions to write into that folder

但是,无论如何,excel 应该保存在磁盘上。你没看到吗?如果没有,请确保 PHP 具有写入该文件夹的适当权限