gpt4 book ai didi

javascript - 将带分页的 PHP 表导出为 EXCEL、PDF、PRINTABLE

转载 作者:行者123 更新时间:2023-11-29 20:56:31 24 4
gpt4 key购买 nike

如何将带分页的 php 表格导出为 excel 和 pdf 格式?我在 youtube 上尝试了很多插件和教程,但仍然没有成功,但它们只能显示数据,而不是整个表格本身。如果我可以将我的表格转换为 PDF 或 Excel 格式,我想将我的表格至少导出为可打印版本。在我的 table 上,我最多有 100 行分页,每页 10 行。我怎样才能做到这一点?我需要你的专业知识。

Here is the screen shot of the table

PHP 导出到分支:

<?php

include "connect.php";
require('lib/js/fpdf.php');

$result = mysqli_query($conn,"SELECT * FROM tblSales");

$header = mysqli_query($conn,"SELECT UCASE('date','sales')
FROM 'INFORMATION_SCHEMA'.'COLUMNS'
WHERE 'TABLE_SCHEMA'='DB_NAME'
AND 'TABLE_NAME'='tblSales'
and 'COLUMN_NAME' in ('date','sales')");

$pdf = new FPDF();

$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
foreach($header as $heading) {
foreach($heading as $column_heading)
$pdf->Cell(95,12,$column_heading,1);
}
foreach($result as $row) {
$pdf->Ln();
foreach($row as $column)
$pdf->Cell(95,12,$column,1);
}
$pdf->Output();


?>

AJAX 代码:

$(document).on('click', '.export-branch-excel', function () {
var branch = $("#branch-hidden-data").val();
$.ajax ({
url:"export-branch-excel.php",
data: "id="+branch,
method: "POST",
dataType: "text",
success: function(data){
window.location = "export-branch-excel.php";
}
});
});

最佳答案

@lawrence agulto 如您所说,我将此代码更改为程序类型。试试这个代码。

Download In excel:

            <?php

include_once "connect.php";

$query = mysqli_query($conn,"SELECT * FROM tblSales ORDER BY date DESC ");


$columnHeader = "Column Name"."\t"."Column Name1"."\t"."Column Name2"."\t"."Column Name3"."\t";

$setData='';
if (mysqli_num_rows($query) > 0) {
while ($rec = mysqli_fetch_assoc($query)) {
$rowData = '';

foreach ($rec as $value) {
$value = '"'.$value.'"'."\t";
$rowData.=$value;
}
$setData.=trim($rowData)."\n";
}
}


header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=Nobelz_Sushank.xls");
header("Pragme: no-cache");
header("Expires: 0");

echo "\t\tSales Data\n";
echo ucwords($columnHeader)."\n".$setData."\n";
?>

Download in pdf :

Download the FPDF library from here.

并根据需要更改查询、表名和 TABLE_COLUMN_NAME

    <?php

$result = mysqli_query($conn," Your QUery");

$header = mysqli_query($conn,"SELECT UCASE(`COLUMN_NAME`)
FROM `INFORMATION_SCHEMA`.`COLUMNS`
WHERE `TABLE_SCHEMA`='DB_NAME'
AND `TABLE_NAME`='TABLE_NAME'
and `COLUMN_NAME` in ('TABLE_COLUMN_NAME','TABLE_COLUMN_NAME1', 'TABLE_COLUMN_NAME2', 'TABLE_COLUMN_NAME3')");


require('fpdf181/fpdf.php');

$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
foreach($header as $heading) {
foreach($heading as $column_heading)
$pdf->Cell(95,12,$column_heading,1);
}
foreach($result as $row) {
$pdf->Ln();
foreach($row as $column)
$pdf->Cell(95,12,$column,1);
}
$pdf->Output();
?>

关于javascript - 将带分页的 PHP 表导出为 EXCEL、PDF、PRINTABLE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49061958/

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