我试图将数据表导出到excel工作表。Datatable有一个具有十进制值和%符号的列。导出后,十进制值将被舍入。但是,我需要有%符号的十进制值才能出现在excel表中。有人能帮我吗。谢谢。JSFiddle
表:
9.09% 7.88% 9.99% 8.38% 8.13% 7.57% 9.30% 8.95% -0.37%
9.09% 7.88% 9.99% 8.38% 8.13% 7.57% 9.30% 8.95% -0.37%
33.41% 30.42% 29.14% 34.44% 31.85% 32.14% 34.12% 34.98% 1.48%
8.98% 8.09% 7.70% 9.87% 9.35% 6.93% 8.98% 6.08% -0.78%
10.30% 11.38% 13.32% 12.40% 11.80% 13.64% 16.15% 13.42% 1.92%
导出excel
9% 8% 10% 8% 8% 9% 9% 0%
9% 8% 10% 8% 8% 9% 9% 0%
33% 30% 29% 34% 32% 32% 34% 35% 1%
9% 8 8% 10% 9% 7% 9% 6% -1%
10% 11% 13% 12% 12% 14% 16% 13% 2%
发布于 2021-12-27 17:58:53
生成的.xlsx文件中的数据包括来自DataTable的完全十进制精度。这可以通过将生成的.xlsx容器作为.zip文件打开,然后进入xl
然后打开worksheets
目录并打开sheet1.xml
就可以看出这一点。第3行的数据以以下方式开始:
<row r="3">
<c r="A3" s="56">
<v>0.1223</v>
</c>
<c r="B3" s="56">
<v>1.6022999999999998</v>
</c>
<c r="C3" s="56">
<v>0.10263</v>
</c>
...
在此数据结构中,属性s
表示正在使用的样式,在本例中设置为"56“。样式包含在.xls容器中,在xl
目录中名为styles.xml
的文件中。数字是cellXfs
中样式的索引,此文件中样式数据的示例如下:
<cellXfs count="68">
<xf numFmtId="0" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1" />
<xf numFmtId="0" fontId="1" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1" />
...
默认情况下,DataTables excelHtml5按钮添加的样式列在文档这里中。这显示"56“是'56 -百分比整数值(自动检测和使用按钮-自1.2.3)‘。excelHtml5为百分比添加了内置样式,将1小数位作为数字"60",但是它没有包含小数点2位的百分比。
要获得两个小数位,必须添加一个新样式。OpenXML文件格式已经有了一个数字格式ID,表示百分比有两个小数位,基于文档这里,它是numFmtId="10“。这种数字格式可以在新样式中使用(否则,还必须添加新的数字格式)。
必须将使用numFmtId="10“的新样式添加到styles.xml文档中,然后将新添加的样式的索引添加到单元格中。实现这一目标的示例代码如下:
$('#example').DataTable({
dom: 'Bfrtip',
filename: "abc",
buttons: [{
extend: 'excelHtml5',
text: 'Excel',
customize: function(xlsx) {
//Get the styles sheet
let styles = xlsx.xl['styles.xml'];
// Create a style which uses numFmtId=10, this is the format for percentage with 2 decimal places
let style = '<xf numFmtId="10" fontId="0" fillId="0" borderId="0" xfId="0" applyNumberFormat="1"/>';
// Get the current cellXfs
let cellXfs = $('cellXfs', styles);
// Append the style to cellXfs, and update the cellXfs count attribute
cellXfs.append(style);
cellXfs.attr('count', $('xf', cellXfs).length);
// Get sheet 1 and apply the new style, skipping first two rows.
// The last element in cellXfs is the ID which is used as the value of s
let sheet = xlsx.xl.worksheets['sheet1.xml'];
$('row:gt(1) c', sheet).attr('s', $('xf', cellXfs).length - 1);
}
}]
});
根据您在问题中提供的示例,可以在https://jsfiddle.net/sg94ec2u/找到一个工作示例。
https://stackoverflow.com/questions/70494542
复制相似问题