php - 如何使用 php 在 excelsheet 上重复输出
问题描述
请我在下面有一些代码块,它们将数据库中的数据填充到excel表中并下载到一个设备中。它对我来说很好,除了我想重复来自数据库的每个数据3x,即John John John
在它的行中,即A1、A2、A3 等,用于检索到的所有数据。请帮助因为我尝试的所有方法都失败了,比如代码块之间的“IF条件”......谢谢
require_once "Classes/PHPExcel.php";
//create PHPExcel object
$excel = new PHPExcel();
//database connection here
include_once "db.php";
//insert some data to PHPExcel object
$excel->setActiveSheetIndex(0);
$query =mysqli_query($mysqli,"select * from student WHERE class='".$class."' ");
$row=4;
$count=0;
while($data=mysqli_fetch_object($query)){
if($count<2)
{
$excel->getActiveSheet()
->setCellValue('A'.$row , $data->name)
->setCellValue('B'.$row , $class)
->setCellValue('C'.$row , $data->email)
->setCellValue('D'.$row , 'Exam');
}
else
{
}
$row++;
} $count++;
// set column dimension/width
$excel->getActiveSheet()->getColumnDimension('A')->setWidth(10);
$excel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
$excel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
//make table headers
$excel -> getActiveSheet()
->setCellValue('A1', 'TABLE LISTS/DATA') //this is a title
->setCellValue('A3', 'NAME')
->setCellValue('B3', 'CLASS')
->setCellValue('C3', 'EMAIL')
//merging the title
$excel->getActiveSheet()->mergeCells('A1:D1');
//aligning
$excel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal('center');
//styling
$excel->getActiveSheet()->getStyle('A1')->applyFromArray(
array(
'font'=>array(
'size'=>24,
)
)
);
$excel->getActiveSheet()->getStyle('A3:C3')->applyFromArray(
array(
'font'=>array(
'bold'=>true
),
'borders'=>array(
'allborders'=>array(
'style'=> PHPExcel_Style_Border::BORDER_THIN
)
)
)
);
//redirect to browser (download) instead of saving the result as a file
//this is for MS Office Excel 2007 xlsx format
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header("Content-Disposition: attachment; filename=$class._Resultsheet.xlsx");
//this is for MS Office Excel 2003 xls format
//header('Content-Type: application/vnd.ms-excel');
//header('Content-Disposition: attachment; filename="test.xlsx"');
header('Cache-Control: max-age=0');
//write the result to a file
//for excel 2007 format
$file = PHPExcel_IOFactory::createWriter($excel,'Excel2007');
//for excel 2003 format
//$file = PHPExcel_IOFactory::createWriter($excel,'Excel5');
//output to php output instead of filename
$file->save('php://output');
?>
```
解决方案
编辑:
对不起,我误解了你的问题,你可以做一个技巧来重复这些行
select *
from student cross join
(select "" union all select "" union all select "") repeater
order by student.id
sqlfiddle http://sqlfiddle.com/#!9/e3349b/269
原始答案
它对我来说很好,除了我想重复来自数据库的每个数据 3x,即 John John John 在其行中,即 A1、A2、A3 等,用于检索的所有数据。
然后我建议您保持 PHP 代码不变,并使用 MySQL concat()从 MySQL 获得所需的较低级别的结果
而不是这样做
select * from student
像这样做
select
concat(col1, ',', col1, ',', col1) as col1,
concat(col2, ',', col2, ',', col2) as col2,
concat(col3, ',', col3, ',', col3) as col3,
concat(col4, ',', col4, ',', col4) as col4
-- .....
from student
(将分隔符从您喜欢的列更改","
为空格)" "
IMO,我认为选择在数据库级别而不是 PHP 级别做事总是更好。这使您的数据库更易于移植并且更独立于应用程序代码,例如,您可以将在 PHP 应用程序下工作的 MySQL 数据库移动到在 JAVA 应用程序下工作,而无需新的 JAVA 开发人员进行太多工作。
推荐阅读
- swift - 更新 tableView - 不完全刷新
- mongodb - 使用 MongoDB 将文档中数组的每个元素链接到另一个文档的数组中的相应元素
- celery - Airflow 中 Celery Executor 的代理
- powershell - 用于查找所有没有安全标记和组的虚拟机的 Powershell 脚本
- javascript - 我制作的网页不会停止在 JavaScript 中的 while 循环(或其他原因)中运行代码,导致网页无法加载
- php - 如何使会话变量可用作 Twig 全局变量?
- ios - Swift - 下一行的属性字符串背景颜色
- excel - 条件格式中 IF 语句的语法 两组条件
- reactjs - Apollo 客户端没有读取使用 useQuery 钩子传入的变量
- google-admin-sdk - 使用GET chromeosdevices方法时,返回的资源中是否返回了自定义字段?