gpt4 book ai didi

php - 如何使用 PHP 下载 2 个表格到 Excel 工作表

转载 作者:行者123 更新时间:2023-11-29 23:56:27 26 4
gpt4 key购买 nike

我有一个 PHP 代码,用于将一个 mysql 表下载到 Excel 工作表。我想做的是将 2 个表下载到同一个 Excel 工作表中。这些表应该位于不同的 Excel 工作表中。请帮助我。

这段代码工作正常。它下载一张表。

<?php

ob_start();
session_start();
include('dbconnection.php');
$usr = $_SESSION['fname'];

header('Content-Type: text/csv');
header('Content-Disposition: attachment;filename=exported-data.csv');

$select_table = mysql_query("select * from regfarmer WHERE ffname='$usr'");

$rows = mysql_fetch_assoc($select_table);

if ($rows) {
getcsv(array_keys($rows));
}
while ($rows) {
getcsv($rows);
$rows = mysql_fetch_assoc($select_table);
}

function getcsv($no_of_field_names) {
$separate = '';

foreach ($no_of_field_names as $field_name) {
if (preg_match('/\\r|\\n|,|"/', $field_name)) {
$field_name = '' . str_replace('', $field_name) . '';
}
echo $separate . $field_name;

$separate = ',';
}

echo "\r\n";
}
?>

最佳答案

我找到了一个用于拆分到 Excel 工作表的代码。现在我如何为此代码集成 mysql 表选择?

<?php
require_once 'PHPExcel.php';
require_once 'PHPExcel/IOFactory.php';

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

// Create a first sheet, representing sales data
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->setCellValue('A1', 'Something');

// Rename sheet
$objPHPExcel->getActiveSheet()->setTitle('Name of Sheet 1');

// Create a new worksheet, after the default sheet
$objPHPExcel->createSheet();

// Add some data to the second sheet, resembling some different data types
$objPHPExcel->setActiveSheetIndex(1);
$objPHPExcel->getActiveSheet()->setCellValue('A1', 'More data');

// Rename 2nd sheet
$objPHPExcel->getActiveSheet()->setTitle('Second sheet');

// Redirect output to a client’s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="name_of_file.xls"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
?>

关于php - 如何使用 PHP 下载 2 个表格到 Excel 工作表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25301448/

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