Php 我正在尝试从mysqli数据库导出excel工作表中的数据,但无法在多个工作表中序列化S No,如1、2、3..等?
下面是我的代码。我想序列化的S号,因为我得到的S号根据数据库。我附加的屏幕截图excel文件以及。任何帮助将是伟大的为我。[Php 我正在尝试从mysqli数据库导出excel工作表中的数据,但无法在多个工作表中序列化S No,如1、2、3..等?,php,mysqli,phpexcel,Php,Mysqli,Phpexcel,下面是我的代码。我想序列化的S号,因为我得到的S号根据数据库。我附加的屏幕截图excel文件以及。任何帮助将是伟大的为我。[ 请更新此部分并尝试: while ($row_q = mysqli_fetch_assoc($q)) { $i = 0;$cnt=1; foreach ($row_q as $key => $value) { if ($key == 'location')
请更新此部分并尝试:
while ($row_q = mysqli_fetch_assoc($q)) {
$i = 0;$cnt=1;
foreach ($row_q as $key => $value) {
if ($key == 'location')
continue;
if($key == 'id')
{
$objPHPExcel->getActiveSheet()->setCellValue($columns[$i] . $row, $cnt);
}
else {
$objPHPExcel->getActiveSheet()->setCellValue($columns[$i] . $row, $row_q[$key]);
}
$i++;
}
$cnt++; //this part is missing
$row++;
}
在迪潘维塔的帮助和支持下,我终于发布了我的答案。。 我已经为第一张做了解决方案,也可以为第二张做同样的解决方案
<?php
error_reporting(E_ALL);
ini_set("display_errors", "ON");
require_once 'PHPExcel.php';
require_once 'PHPExcel/IOFactory.php';
$conn = new mysqli("localhost","root","","test");
$xls_filename = 'Data' . date('d-m-Y') . '.xls'; // Define Excel (.xls) file name
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->setTitle('Programming');
$heading = array(
'id' => 'S.No',
'name' => 'Name',
'dept' => 'Department',
);
$no_of_cols = count($heading);
$rowNumberH = 1;
$colH = 'A';
$columns = array('0' => 'A', '1' => 'B', '2' => 'C', '3' => 'D', '4' => 'E', '5' => 'F', '6' => 'G', '7' => 'H', '8' => 'I', '9' => 'J', '10' => 'K', '11' => 'L', '12' => 'M', '13' => 'N', '14' => 'O', '15' => 'P', '16' => 'Q', '17' => 'R', '18' => 'S', '19' => 'T', '20' => 'U', '21' => 'V', '22' => 'W', '23' => 'X', '24' => 'Y', '25' => 'Z');
$q = $conn->query("SELECT * FROM form where dept='programming'");
if (mysqli_num_rows($q) > 0) {
foreach ($heading as $h) {
$objPHPExcel->getActiveSheet()->setCellValue($colH . $rowNumberH, $h);
$objPHPExcel->getActiveSheet()->getColumnDimension($colH)->setWidth(25);
$colH++;
}
$row = 2;
while ($row_q = mysqli_fetch_assoc($q)) {
$i = 0;
//$cnt = 1;
foreach ($row_q as $key => $value)
{
if ($key == 'location')
continue;
if($key == 'id')
{
$objPHPExcel->getActiveSheet()->setCellValue($columns[$i] . $row, $row-1);
}
else
{
$objPHPExcel->getActiveSheet()->setCellValue($columns[$i] . $row, $row_q[$key]);
}
//var_dump($cnt);
//alert($cnt);
$i++;
//$cnt++;
}
$row++;
}
}
/*--------------------2nd sheet-----------------------------*/
//$objPHPExcel = new PHPExcel();
$objPHPExcel->createSheet();
$objPHPExcel->setActiveSheetIndex(1);
$objPHPExcel->getActiveSheet()->setTitle('Graphics');
$heading = array(
'id' => 'S.No',
'name' => 'Name',
'dept' => 'Department',
);
$no_of_cols = count($heading);
$rowNumberH = 1;
$colH = 'A';
$columns = array('0' => 'A', '1' => 'B', '2' => 'C', '3' => 'D', '4' => 'E', '5' => 'F', '6' => 'G', '7' => 'H', '8' => 'I', '9' => 'J', '10' => 'K', '11' => 'L', '12' => 'M', '13' => 'N', '14' => 'O', '15' => 'P', '16' => 'Q', '17' => 'R', '18' => 'S', '19' => 'T', '20' => 'U', '21' => 'V', '22' => 'W', '23' => 'X', '24' => 'Y', '25' => 'Z');
$q = $conn->query("SELECT * FROM form where dept='graphics'");
if (mysqli_num_rows($q) > 0) {
foreach ($heading as $h) {
$objPHPExcel->getActiveSheet()->setCellValue($colH . $rowNumberH, $h);
$objPHPExcel->getActiveSheet()->getColumnDimension($colH)->setWidth(25);
$colH++;
}
$row = 2;
while ($row_q = mysqli_fetch_assoc($q)) {
$i = 0;
foreach ($row_q as $key => $value) {
if ($key == 'location')
continue;
$objPHPExcel->getActiveSheet()->setCellValue($columns[$i] . $row, $row_q[$key]);
$i++;
}$row++;
}
}
$objPHPExcel->setActiveSheetIndex(0);
header("Content-Type: application/xls");
header("Content-Disposition: attachment; filename=$xls_filename");
header("Pragma: no-cache");
header("Expires: 0");
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
?>
cnt的值没有增加..为什么会这样?请在foreach()的末尾检查。
已经尝试过了,但值没有增加,它每行只打印1个。根据您的评论更改后,我上传了一个图像,但它也不起作用..与您提到的一样。这个代码示例确实帮助了我。谢谢!
<?php
error_reporting(E_ALL);
ini_set("display_errors", "ON");
require_once 'PHPExcel.php';
require_once 'PHPExcel/IOFactory.php';
$conn = new mysqli("localhost","root","","test");
$xls_filename = 'Data' . date('d-m-Y') . '.xls'; // Define Excel (.xls) file name
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->setTitle('Programming');
$heading = array(
'id' => 'S.No',
'name' => 'Name',
'dept' => 'Department',
);
$no_of_cols = count($heading);
$rowNumberH = 1;
$colH = 'A';
$columns = array('0' => 'A', '1' => 'B', '2' => 'C', '3' => 'D', '4' => 'E', '5' => 'F', '6' => 'G', '7' => 'H', '8' => 'I', '9' => 'J', '10' => 'K', '11' => 'L', '12' => 'M', '13' => 'N', '14' => 'O', '15' => 'P', '16' => 'Q', '17' => 'R', '18' => 'S', '19' => 'T', '20' => 'U', '21' => 'V', '22' => 'W', '23' => 'X', '24' => 'Y', '25' => 'Z');
$q = $conn->query("SELECT * FROM form where dept='programming'");
if (mysqli_num_rows($q) > 0) {
foreach ($heading as $h) {
$objPHPExcel->getActiveSheet()->setCellValue($colH . $rowNumberH, $h);
$objPHPExcel->getActiveSheet()->getColumnDimension($colH)->setWidth(25);
$colH++;
}
$row = 2;
while ($row_q = mysqli_fetch_assoc($q)) {
$i = 0;
//$cnt = 1;
foreach ($row_q as $key => $value)
{
if ($key == 'location')
continue;
if($key == 'id')
{
$objPHPExcel->getActiveSheet()->setCellValue($columns[$i] . $row, $row-1);
}
else
{
$objPHPExcel->getActiveSheet()->setCellValue($columns[$i] . $row, $row_q[$key]);
}
//var_dump($cnt);
//alert($cnt);
$i++;
//$cnt++;
}
$row++;
}
}
/*--------------------2nd sheet-----------------------------*/
//$objPHPExcel = new PHPExcel();
$objPHPExcel->createSheet();
$objPHPExcel->setActiveSheetIndex(1);
$objPHPExcel->getActiveSheet()->setTitle('Graphics');
$heading = array(
'id' => 'S.No',
'name' => 'Name',
'dept' => 'Department',
);
$no_of_cols = count($heading);
$rowNumberH = 1;
$colH = 'A';
$columns = array('0' => 'A', '1' => 'B', '2' => 'C', '3' => 'D', '4' => 'E', '5' => 'F', '6' => 'G', '7' => 'H', '8' => 'I', '9' => 'J', '10' => 'K', '11' => 'L', '12' => 'M', '13' => 'N', '14' => 'O', '15' => 'P', '16' => 'Q', '17' => 'R', '18' => 'S', '19' => 'T', '20' => 'U', '21' => 'V', '22' => 'W', '23' => 'X', '24' => 'Y', '25' => 'Z');
$q = $conn->query("SELECT * FROM form where dept='graphics'");
if (mysqli_num_rows($q) > 0) {
foreach ($heading as $h) {
$objPHPExcel->getActiveSheet()->setCellValue($colH . $rowNumberH, $h);
$objPHPExcel->getActiveSheet()->getColumnDimension($colH)->setWidth(25);
$colH++;
}
$row = 2;
while ($row_q = mysqli_fetch_assoc($q)) {
$i = 0;
foreach ($row_q as $key => $value) {
if ($key == 'location')
continue;
$objPHPExcel->getActiveSheet()->setCellValue($columns[$i] . $row, $row_q[$key]);
$i++;
}$row++;
}
}
$objPHPExcel->setActiveSheetIndex(0);
header("Content-Type: application/xls");
header("Content-Disposition: attachment; filename=$xls_filename");
header("Pragma: no-cache");
header("Expires: 0");
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
?>