模板参考
这里使用的模板test111.grt参照进行生成 ,需要确保字段对应才能将数据进行传递
Grid++Report:自定义模板设计(自由表格使用),详细教程-CSDN博客https://blog.csdn.net/weixin_46001736/article/details/144315191?spm=1001.2014.3001.5502
PHP引入Grid++Report参考
php:完整部署Grid++Report到php项目,并实现模板打印-CSDN博客https://blog.csdn.net/weixin_46001736/article/details/144309295?spm=1001.2014.3001.5502
一、官方提供模板
可参考plugins中的案例
1、将模板写入index.php
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Web报表(B/S报表)演示 - 查询显示控件展现报表</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="php/plugins/CreateControl.js" type="text/javascript"></script>
<style type="text/css">
html,
body {
margin: 0;
height: 100%;
}
</style>
</head>
<body style="margin:0">
<script type="text/javascript">
//用查询显示控件展现报表,从URL“../grf/1a.grf”获取报表膜板定义,从URL“../data/DataCenter.php?data=Customer”获取XML形式的报表数据,
//CreateDisplayViewerEx("100%", "100%", urlAddRandomNo("../../grf/1a.grf"), "../../data/DataCenter.php?data=Customer", true, "");
InsertDisplayViewer({
report: urlAddRandomNo("php/grf/test111.grf"),
data: "php/data/DataCenter.php?data=Customer"
});
</script>
</body>
</html>
2、数据库查询、数据传递
代码路径:php/data/DataCenter.php
文件DataCenter.php:使用Switch case进行判断,并查询数据传递给模板
数据查询,这里传递的参数是data = Customer,也就是case=Customer的时候执行查询语句
这是在DataCenter.php进行数据查询这里查询的数据都是xml类型,在sql中进行了转换
mysql_GenXmlData.php代码
<?php
include 'BaseReportData.php';
// 产生报表的多个记录集的 XML 数据
function XML_GenMultiRecordset($QueryList, $DataType = const_DefaultDataType)
{
// 连接到 MySQL 服务器
$conn = new mysqli("localhost", "root", "", "gridreport");
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
$conn->set_charset("utf8");
$XMLText = "<xml>\n";
foreach ($QueryList as $RecordsetName => $QuerySQL) {
$result = $conn->query($QuerySQL);
$fldTypes = array();
$numfields = $result->field_count;
for ($i = 0; $i < $numfields; $i++) {
$fldType = $result->fetch_field_direct($i)->type;
if (stripos($fldType, "date") !== false)
$fldTypes[$i] = 1;
else if ($fldType == "blob" || $fldType == "image")
$fldTypes[$i] = 2;
else
$fldTypes[$i] = 0;
}
while ($row = $result->fetch_assoc()) {
$XMLText .= "<" . $RecordsetName . ">";
for ($i = 0; $i < $numfields; $i++) {
if ($fldTypes[$i] == 2)
$XMLText .= ("<" . $result->fetch_field_direct($i)->name . ">" . base64_encode($row[$result->fetch_field_direct($i)->name]) . "</" . $result->fetch_field_direct($i)->name . ">");
else
$XMLText .= ("<" . $result->fetch_field_direct($i)->name . ">" . htmlspecialchars($row[$result->fetch_field_direct($i)->name], ENT_QUOTES) . "</" . $result->fetch_field_direct($i)->name . ">");
}
$XMLText .= "</" . $RecordsetName . ">\n";
}
$result->free();
}
$XMLText .= "</xml>\n";
ResponseReportData($XMLText, $DataType);
$conn->close();
}
// 产生报表的一个记录集的 XML 数据
function XML_GenOneRecordset($QuerySQL, $DataType = const_DefaultDataType)
{
$QueryList = array("row" => $QuerySQL);
XML_GenMultiRecordset($QueryList, $DataType);
}
function BatchGetDataCount($QuerySQL)
{
// 连接到 MySQL 服务器
$conn = new mysqli("localhost", "root", "", "gridreport");
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
$conn->set_charset("utf8");
$result = $conn->query($QuerySQL);
$Total = 0;
if ($row = $result->fetch_assoc()) {
$Total = $row[0];
}
$result->free();
$conn->close();
return $Total;
}
// 产生字段类型,用于调试中分析子段的类型名称
function ListFieldType($QuerySQL)
{
header("Content-Type: text/plain");
// 连接到 MySQL 服务器
$conn = new mysqli("localhost", "root", "", "gridreport");
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
$conn->set_charset("utf8");
$result = $conn->query($QuerySQL);
$XMLText = "<xml>\n";
$numfields = $result->field_count;
for ($i = 0; $i < $numfields; $i++) {
$field = $result->fetch_field_direct($i);
$XMLText .= ($field->name . "=" . $field->type . "\n");
}
$result->free();
$conn->close();
$XMLText .= "</xml>\n";
echo $XMLText;
}
二、自定义数据传递
这里采用json格式进行传递
1、index.php
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Web报表(B/S报表)演示 - 查询显示控件展现报表</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="php/plugins/CreateControl.js" type="text/javascript"></script>
<style type="text/css">
html,
body {
margin: 0;
height: 100%;
}
</style>
</head>
<body style="margin:0">
<script type="text/javascript">
//用查询显示控件展现报表,从URL“../grf/1a.grf”获取报表膜板定义,从URL“../data/DataCenter.php?data=Customer”获取XML形式的报表数据,
//CreateDisplayViewerEx("100%", "100%", urlAddRandomNo("../../grf/1a.grf"), "../../data/DataCenter.php?data=Customer", true, "");
var data = {
"Master": [{
"Barcode1": '1234567890', //条形码名称
"header1": '123456',
"header2": "客户",
"header3": "零件料号",
"header4": "2024-07-01",
"header5": "零件名称",
"header6": "班别",
}],
"Detail": [{
"line1": "HUNGC",
"line2": "五金机械",
"line3": "苏先生",
"line4": "销售代表",
"line5": "德昌路甲"
},
{
"line1": "CENTC",
"line2": "三捷实业",
"line3": "王先生",
"line4": "市场经理",
"line5": "英雄山路"
},
{
"line1": "CACTU",
"line2": "威航货运",
"line3": "刘先生",
"line4": "销售代理",
"line5": "经七纬"
},
{
"line1": "BLONP",
"line2": "国皓",
"line3": "黄雅玲",
"line4": "市场经理",
"line5": "广发北路"
},
],
}
// 将数据转换为 JSON 格式
var jsonData = JSON.stringify(data);
InsertDisplayViewer({
report: urlAddRandomNo("php/grf/test111.grf"),
data: jsonData,
// data: "php/data/DataCenter.php?data=Customer"
});
</script>
</body>
</html>
确认数据对应