gpt4 book ai didi

php - 发送 Excel 文件而不保存在服务器上

转载 作者:行者123 更新时间:2023-12-04 16:40:28 24 4
gpt4 key购买 nike

创建后我需要通过电子邮件发送excel文件,但不幸的是php没有写入服务器的权限,所以我需要发送它而不保存文件。而且通过浏览器是不可能的,因为你需要等待excell的创建并且数据库检索非常慢,所以我想知道是否有办法直接通过电子邮件发送。找了好久没找到解决办法,非常感谢!

编辑添加代码:

<?php

/** Include path **/
ini_set('include_path', ini_get('include_path').';../Classes/');
/** PHPExcel */
include (dirname(__FILE__)."/../../lib/PHPExcel.php");
/** PHPExcel_Writer_Excel2007 */
include (dirname(__FILE__)."/../../lib/PHPExcel/Writer/Excel2007.php");
/** Php Mailer */
require (dirname(__FILE__).'/../PHPMailer/class.phpmailer.php');

require '../mysqlvars.php';
require '../lib/db.php';
require '../lib/form_functions.php';
require 'includes/session.php';
require("classes.php");
$dbConn = connectDB($dbHost, $dbUser, $dbPass, $dbDB);
if (!$dbConn) {
die ('Cannot connect to database');
}

require 'includes/session.php';
require '../init.php';
require '../licence.php';


require("classes.php");
// Create new PHPExcel object
echo date('H:i:s') . " Create new PHPExcel object\n";
$objPHPExcel = new PHPExcel();

// Set properties
echo date('H:i:s') . " Set properties\n";
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw");
$objPHPExcel->getProperties()->setLastModifiedBy("Maarten Balliauw");
$objPHPExcel->getProperties()->setTitle("Office 2007 XLSX Test Document");
$objPHPExcel->getProperties()->setSubject("Office 2007 XLSX Test Document");
$objPHPExcel->getProperties()->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.");


// Add some data
echo date('H:i:s') . " Add some data\n";
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->SetCellValue('A1', 'Hello');
$objPHPExcel->getActiveSheet()->SetCellValue('B2', 'world!');
$objPHPExcel->getActiveSheet()->SetCellValue('C1', 'Hello');
$objPHPExcel->getActiveSheet()->SetCellValue('D2', 'world!');

// Rename sheet
echo date('H:i:s') . " Rename sheet\n";
$objPHPExcel->getActiveSheet()->setTitle('Simple');


// Save Excel 2007 file
echo date('H:i:s') . " Write to Excel2007 format\n";
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');

$objWriter->save();
print_r('ok');die; //never prints nothing
?>

最佳答案

由于您没有提供完整的代码片段,我的猜测是:

第一个你应该将文件保存为/到变量中,这是一个例子:

https://stackoverflow.com/a/9480344/4421474

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
ob_start();
$objWriter->save('php://output');
$excelOutput = ob_get_clean();

之后,您应该将该文件附加为 BASE64 编码字符串:

https://stackoverflow.com/a/1202577/4421474

关于php - 发送 Excel 文件而不保存在服务器上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33215761/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com