我开始学习如何使用dataTables,并用它来显示我从Excel文件导入的一些JSON数据 我有桌子显示,但它不是标题的中心。 我一直在寻找解决方案,但我还没有找到解决方案。 代码包含在代码段中,以及输出的屏幕截图。 如果我做错了,请帮助我。
谢谢! :)
/* set up XMLHttpRequest */
var url = "LICENCIAMENTOS.xlsx";
var oReq = new XMLHttpRequest();
oReq.open("GET", url, true);
oReq.responseType = "arraybuffer";
oReq.send();
oReq.onload = function(e) {
var arraybuffer = oReq.response;
/* convert data to binary string */
var data = new Uint8Array(arraybuffer);
var arr = new Array();
for (var i = 0; i != data.length; ++i) arr[i] = String.fromCharCode(data[i]);
var bstr = arr.join("");
/* Call XLSX */
var workbook = XLSX.read(bstr, {
type: "binary"
});
/* DO SOMETHING WITH workbook HERE */
var first_sheet_name = workbook.SheetNames[5];
//var address_of_cell='A5';
/* Get worksheet */
var worksheet = workbook.Sheets[first_sheet_name];
v = XLSX.utils.sheet_to_json(worksheet);
//DataTable
$('#data-table').DataTable({
"aaData": v,
"aoColumns": [{
"mData": "Cenas",
sDefaultContent: ''
},
{
"mDataProp": "PT",
sDefaultContent: ''
},
{
"mDataProp": "kV",
sDefaultContent: ''
},
{
"mDataProp": "S/A",
sDefaultContent: ''
},
{
"mDataProp": "NOME",
sDefaultContent: ''
},
{
"mDataProp": "SGD",
sDefaultContent: ''
},
{
"mDataProp": "Comprimento (m)",
sDefaultContent: ''
},
{
"mDataProp": "EDIS",
sDefaultContent: ''
},
{
"mDataProp": "Licenciamento Externo",
sDefaultContent: ''
},
{
"mDataProp": "Data Execucao",
sDefaultContent: ''
},
{
"mDataProp": "APE",
sDefaultContent: ''
},
{
"mDataProp": "DREC/DRELVT",
sDefaultContent: ''
},
{
"mDataProp": "A-P-E-",
sDefaultContent: ''
},
{
"mDataProp": "Editos - GBCI",
sDefaultContent: ''
},
{
"mDataProp": "GBCI-Recortes",
sDefaultContent: ''
},
{
"mDataProp": "Editos - ME",
sDefaultContent: ''
},
{
"mDataProp": "Estradas",
sDefaultContent: ''
},
{
"mDataProp": "Refer",
sDefaultContent: ''
},
{
"mDataProp": "ICN",
sDefaultContent: ''
},
{
"mDataProp": "Txas-NI",
sDefaultContent: ''
},
{
"mDataProp": "Taxas-Pag.",
sDefaultContent: ''
},
{
"mDataProp": "Licenciamento",
sDefaultContent: ''
},
{
"mDataProp": "Pedido de Vistoria",
sDefaultContent: ''
},
{
"mDataProp": "Licenca de Exploracao",
sDefaultContent: ''
},
{
"mDataProp": "DRE 161.10.03.----",
sDefaultContent: ''
},
{
"mDataProp": "Avisos",
sDefaultContent: ''
},
{
"mDataProp": "Observacoes",
sDefaultContent: ''
},
{
"mDataProp": "IF",
sDefaultContent: ''
},
{
"mDataProp": "SGD",
sDefaultContent: ''
},
{
"mDataProp": "DTA PGT",
sDefaultContent: ''
}
]
});
}

<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.10.8/xlsx.full.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/dt/dt-1.10.15/datatables.min.css" />
<script type="text/javascript" src="https://cdn.datatables.net/r/bs-3.3.5/jqc-1.11.3,dt-1.10.8/datatables.min.js"></script>
<div class="container">
<h1 align="center">LICENCIAMENTOS</h3>
<h3 align="center">Ansiao</h3>
<table id="data-table" style="text-align:center;">
<thead>
<tr>
<th>X</th>
<th>PT</th>
<th>kV</th>
<th>S/A</th>
<th>NOME</th>
<th>SGD</th>
<th>Comprimento</th>
<th>EDIS</th>
<th>Licenciamento externo</th>
<th>Data Execucao</th>
<th>APE</th>
<th>DREC/DRELVT</th>
<th>A-P-E-</th>
<th>Editos</th>
<th>GBCI-Recortes</th>
<th>Editos-ME</th>
<th>Estradas</th>
<th>Refer</th>
<th>ICN</th>
<th>Txas-NI</th>
<th>Taxas-Pag.</th>
<th>Licenciamento</th>
<th>Pedido de vistoria</th>
<th>Licença de exploracao</th>
<th>DRE 161.10.03----</th>
<th>Avisos</th>
<th>Obeservacoes</th>
<th>if</th>
<th>SGD</th>
<th>DTA PGT</th>
</tr>
</thead>
</table>
</div>
&#13;
答案 0 :(得分:1)
将margin: 0 auto;
添加到您的表格style
<table id="data-table" style="text-align:center; margin: 0 auto;" >
它会自动调整水平边距,使您的桌子居中