thymeleaf制作表格的模板
1.后台
/**
* @author cqh
* @create 2022/5/18 15:42
*/
@Controller
@RequestMapping("/system/demo")//前台包地址前缀
public class demo extends BaseController {
private String prefix = "/system/demo";
@Autowired
ISysDeptService sysDeptService;
@GetMapping("list")
public String list(ModelMap mmap) {
mmap.put("user", getSysUser());
mmap.put("dept", sysDeptService.getCachedDeptById(getSysUser().getDeptId()));
mmap.put("year", DateUtils.dateTimeNow("yyyy"));
return prefix + "/list";
}
@RequestMapping("list/data")
public String listData(ModelMap mmap) {
String sql="SELECT * FROM `table`";
List<Record> records = Db.find(sql);
mmap.put("records",records);
return prefix + "/list_data";
}
}
2.导出功能list.html
<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
<meta charset="UTF-8">
<th:block th:include="include :: header('项目名称')" />
</head>
<body class="gray-bg">
<div class="container-div">
<div class="row">
<div class="col-sm-12 search-collapse">
<form id="formId">
<div class="select-list">
<ul>
<li>
<a class="btn btn-info btn-rounded btn-sm" onclick="toExcel()"><i class="fa fa-download"></i> 导出</a>
<a id="dlink" style="display:none;"></a>
</li>
</ul>
</div>
</form>
</div>
<div class="btn-group-sm" id="toolbar" role="group"></div>
<div class="col-sm-12 select-table" style="overflow-x: scroll"></div>
</div>
</div>
<th:block th:include="include :: footer" />
<script type="text/javascript" th:inline="javascript">
function reset(){
$.modal.loading("正在查询,请稍后...");
var url = ctx + "/system/demo/list/data";
$(".select-table").load(url, function (responseTxt,statusTxt,xhr) {
if (statusTxt === 'success') {
$.modal.closeLoading();
}
});
}
function load(){
$.modal.loading("正在查询,请稍后...");
var url = ctx + "/system/demo/list/data";
$(".select-table").load(url, function (responseTxt,statusTxt,xhr) {
if (statusTxt === 'success') {
$.modal.closeLoading();
}
});
}
function toExcel(){
$.widget.tableToExcel('report','reportHead','reportFoot',
$("#reportYear").text()+"年",
"Excel表名");
}
$(function(){
reset();
})
</script>
</body>
</html>
3.表格模板list_data.html
<div id="table" class="wrapper wrapper-content animated fadeInRight" xmlns:th="http://www.thymeleaf.org">
<meta charset="UTF-8">
<style type="text/css">
/* TABLES */
.excelTableHead {
color: black;
margin-bottom: 6px;
}
.excelTable {
width: auto;
font-size: 13px;
color: black;
border: 1px solid #333333;
}
.excelTable > thead > tr > th, .excelTable > thead > tr > td {
background-color: #333333;
border-bottom-width: 1px;
}
.excelTable > thead > tr > th, .excelTable > tbody > tr > th, .excelTable > tfoot > tr > th, .excelTable > thead > tr > td, .excelTable > tbody > tr > td, .excelTable > tfoot > tr > td {
border: 1px solid #333333;
}
.excelTableFoot {
color: black;
}
@media print {
#alert_danger {
display: none;
}
}
table td {
max-width: 200px;
max-height: 50px;
word-wrap: break-word;
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
page: true;
}
table td:hover {
white-space: normal;
overflow: auto;
}
</style>
<table id="reportHead" class="excelTableHead" style="width: 100%" th:with="maxcols=41">
<tr>
<td th:colspan="${maxcols}" align="center" style="font-size: 26px; font-weight: 700; padding-bottom: 6px">
统计表表名
</td>
</tr>
<tr>
<td colspan="6">地址:[[${dept.deptName}]]</td>
<td th:colspan="${maxcols-7}" align="right">时间:[[${today}]]</td>
<td align="right">单位:</td>
</tr>
</table>
<table id="report" class="table excelTable" billstandardborder="1" cellspacing="0" cellpadding="0"
style="width: 100%">
<tr>
<td style="font-weight: 700;"rowspan="2" align="right">序号</td>
<td style="font-weight: 700;white-space: nowrap" rowspan="2" colspan="2" align="center">1</td>
<td style="font-weight: 700;white-space: nowrap;" rowspan="2" colspan="2" align="center">2</td>
<td style="font-weight: 700;white-space: nowrap;" rowspan="2" colspan="2" align="center">3</td>
<td style="font-weight: 700;white-space: nowrap;" rowspan="2" colspan="2" align="center">4</td>
<td style="font-weight: 700;white-space: nowrap;" rowspan="2" colspan="2" align="center">5</td>
<td style="font-weight: 700;white-space: nowrap;" rowspan="2" colspan="2" align="center">6</td>
<td style="font-weight: 700;white-space: nowrap;" rowspan="2" colspan="2" align="center">7</td>
<td style="font-weight: 700;white-space: nowrap;" colspan="4" align="center">8</td>
<td style="font-weight: 700;white-space: nowrap;" colspan="8" align="center">9</td>
<td style="font-weight: 700;white-space: nowrap;" colspan="10" align="center">10</td>
<td style="font-weight: 700;white-space: nowrap;" rowspan="2" colspan="2" align="center">11</td>
<td style="font-weight: 700;white-space: nowrap;" rowspan="2" colspan="2" align="center">12</td>
</tr>
<tr>
<td style="font-weight: 700;white-space: nowrap;" align="center">13</td>
<td style="font-weight: 700;white-space: nowrap;" align="center">14</td>
<td style="font-weight: 700;white-space: nowrap;" align="center">15</td>
<td style="font-weight: 700;white-space: nowrap;" align="center">16</td>
<td style="font-weight: 700;white-space: nowrap;" align="center">17</td>
<td style="font-weight: 700;white-space: nowrap;" align="center">18</td>
<td style="font-weight: 700;white-space: nowrap;" align="center">19</td>
<td style="font-weight: 700;white-space: nowrap;" align="center">20</td>
<td style="font-weight: 700;white-space: nowrap;" align="center">21</td>
<td style="font-weight: 700;white-space: nowrap;" align="center">22</td>
<td style="font-weight: 700;white-space: nowrap;" align="center">23</td>
<td style="font-weight: 700;white-space: nowrap;" align="center">24</td>
<td style="font-weight: 700;white-space: nowrap;" align="center">25</td>
<td style="font-weight: 700;white-space: nowrap;" align="center">26</td>
<td style="font-weight: 700;white-space: nowrap;" align="center">27</td>
<td style="font-weight: 700;white-space: nowrap;" align="center">28</td>
<td style="font-weight: 700;white-space: nowrap;" align="center">29</td>
<td style="font-weight: 700;white-space: nowrap;" align="center">30</td>
<td style="font-weight: 700;white-space: nowrap;" align="center">31</td>
<td style="font-weight: 700;white-space: nowrap;" align="center">32</td>
<td style="font-weight: 700;"align="center">33</td>
<td style="font-weight: 700;white-space: nowrap;" align="center">34</td>
</tr>
<tr th:each="item:${list}" >
<td th:text="${itemStat.count}" ></td>
<td align="center" colspan="2" >[[${item.get('1')}]]</td>
<td align="center" colspan="2" >[[${item.get('2')}]]</td>
<td colspan="2" >[[${item.get('3')}]]</td>
<td colspan="2" >[[${item.get('4')}]]</td>
<td colspan="2">[[${item.get('5')}]]</td>
<td colspan="2">[[${#numbers.formatDecimal(item.get('6'),1,'COMMA',0,'POINT')}]]</td>
<td colspan="2">[[${#numbers.formatDecimal(item.get('7'),1,'COMMA',0,'POINT')}]]</td>
<td >[[${item.get('13')}]]</td>
<td > [[${item.get('14')}]]</td>
<td >[[${item.get('15')}]]</td>
<td >
[[${#numbers.formatDecimal(item.get('16'),1,'COMMA',0,'POINT')}]]
</td>
<td >[[${item.get('17')}]]</td>
<td >[[${item.get('18')}]]</td>
<td >[[${item.get('19')}]]</td>
<td >[[${item.get('20')}]]</td>
<td >[[${item.get('21')}]]</td>
<td >[[${item.get('22')}]]</td>
<td >[[${item.get('23')}]]</td>
<td >[[${item.get('24')}]]</td>
<td >[[${item.get('25')}]]</td>
<td >[[${item.get('26')}]]</td>
<td >[[${item.get('27')}]]</td>
<td >[[${item.get('28')}]]</td>
<td >[[${item.get('29')}]]</td>
<td >[[${item.get('30')}]]</td>
<td >[[${item.get('31')}]]</td>
<td >[[${item.get('32')}]]</td>
<td >[[${item.get('33')}]]</td>
<td >[[${item.get('34')}]]</td>
<td colspan="2">[[${item.get('11')}]]</td>
<td colspan="2">[[${item.get('12')}]]</td>
</tr>
<tr th:each="item:${Sumlist}">
<td style="font-weight: 700;white-space: nowrap;" align="right">合计</td>
<td align="right" colspan="2"></td>
<td align="right" colspan="2"></td>
<td align="right" colspan="2"></td>
<td align="right" colspan="2"></td>
<td align="right" colspan="2"></td>
<td align="right" colspan="2">[[${#numbers.formatDecimal(item.get('Sum6'),1,'COMMA',0,'POINT')}]]</td>
<td align="right" colspan="2">[[${#numbers.formatDecimal(item.get('Sum7'),1,'COMMA',0,'POINT')}]]</td>
<td align="right"></td>
<td align="right"></td>
<td align="right"></td>
<td align="right">[[${#numbers.formatDecimal(item.get('Sum16'),1,'COMMA',0,'POINT')}]]</td>
<td align="right"></td>
<td align="right"></td>
<td align="right"></td>
<td align="right"></td>
<td align="right"></td>
<td align="right"></td>
<td align="right"></td>
<td align="right"></td>
<td align="right"></td>
<td align="right"></td>
<td align="right"></td>
<td align="right"></td>
<td align="right"></td>
<td align="right"></td>
<td align="right"></td>
<td align="right"></td>
<td align="right"></td>
<td align="right"></td>
<td align="right" colspan="2"></td>
<td align="right" colspan="2"></td>
</tr>
</table>
<table id="reportFoot" class="excelTableFoot" style="width: 100%">
<tr>
<td style="width:80px;" colspan="2"></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td colspan="3">负责人:</td>
<td colspan="3">审核人:</td>
<td colspan="3">填报人:</td>
</tr>
</table>
<br/>
</div>