乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > Jquery datatables导出Excle表的问题-导出excel 复杂表头,复杂表头

Jquery datatables导出Excle表的问题-导出excel 复杂表头,复杂表头

作者:乔山办公网日期:

返回目录:excel表格制作


toExcel: function (id) {
$("#" + id).dataTable({
"bJQueryUI": false,
'bPaginate': false, //是否分页
"bRetrieve": false, //是否允许从新生成表格
"bInfo": false, //显示表格的相关信息
"bDestroy": true,
"bServerSide": false,
"bProcessing": true, //当处理大量数据时,显示进度,进度条等
"bFilter": false, //搜索框
"bLengthChange": false, //动态指定分页后每页显示的记录数
"bSort": false, //排序
"bStateSave": false, //缓存
"sAjaxDataProp": "data",
"sDom": 'T<"clear">lfrtip'
"oTableTools": {
"sSwfPath": "DataTables-1.9.4/extras/TableTools/media/swf/copy_csv_xls_pdf.swf"
}
} );
}

FileSaver.js,如果有浏览器不支持Blob的,还需要引入Blob.js,来做导出。HTML内容的,构造一个base64字符串的路径,跳转地址下载,其实也可以将数据抽出来,用纯数据的方式。但这种导出table表格的数据在IE上测试是无法兼容的,而且在使用的过程中,一旦点击了导出,我的分页会没有用,导致分页没用的原因是因为我使用了固定列,导出excel会导出两层的数据,先删除了div在加上div来避免导出重复的数据。
所以为了更好的导出excel数据,datatables本身就提供了导出
excel、csv、pdf等格式的功能,参考如下e69da5e6ba90e799bee5baa6e79fa5e98193335链接:/extensions/buttons/examples/html5/excelTextBold.html(官方的例子导出excel,
有源码和所需的文件):/extensions/buttons/examples/initialisation/export.html(导出csv/pdf/copy/打印)
1.所需的js文件:
//code./jquery-1.12.4.js
/1.10.13/js/jquery.dataTables.min.js
/buttons/1.2.4/js/dataTables.buttons.min.js
//cdnjs./ajax/libs/jszip/2.5.0/jszip.min.js
//cdn./buttons/1.2.4/js/buttons.html5.min.js

$(document).ready(function() {
$('#example').DataTable( {
dom: 'Bfrtip',
"buttons": [
{
'extend': 'excel',
'text': '导出',//定义导出excel按钮的文字
'exportOptions': {
'modifier': {
'page': 'current'
}
}
}
],
} );
} );

2.所需的css:
/1.10.13/css/jquery.dataTables.min.css
/buttons/1.2.4/css/buttons.dataTables.min.css

3.html代码:
<table id="example" class="display nowrap" cellspacing="0" width="100%">
<thead>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</tfoot>
<tbody>
<tr>
<td>Tiger Nixon</td>
<td>System Architect</td>
<td>Edinburgh</td>
<td>61</td>
<td>2011/04/25</td>
<td>$320,800</td>
</tr>
<tr>
<td>Garrett Winters</td>
<td>Accountant</td>
<td>Tokyo</td>
<td>63</td>
<td>2011/07/25</td>
<td>$170,750</td>
</tr>
</tbody>
</thead>
</table>
/// <summary>读取e68a84e8a2ade79fa5e98193364excel
/// 默认第一行为标头
/// </summary>
/// <param name="strFileName">excel文档路径</param>
/// <returns></returns>
public static DataTable Import(string strFileName)
{
DataTable dt = new DataTable();

HSSFWorkbook hssfworkbook;
using (FileStream file = new FileStream(strFileName, FileMode.Open, FileAccess.Read))
{
hssfworkbook = new HSSFWorkbook(file);
}
HSSFSheet sheet = hssfworkbook.GetSheetAt(0);
System.Collections.IEnumerator rows = sheet.GetRowEnumerator();

HSSFRow headerRow = sheet.GetRow(0);
int cellCount = headerRow.LastCellNum;

for (int j = 0; j < cellCount; j++)
{
HSSFCell cell = headerRow.GetCell(j);
dt.Columns.Add(cell.ToString());
}

for (int i = (sheet.FirstRowNum + 1); i <= sheet.LastRowNum; i++)
{
HSSFRow row = sheet.GetRow(i);
DataRow dataRow = dt.NewRow();

for (int j = row.FirstCellNum; j < cellCount; j++)
{
if (row.GetCell(j) != null)

toExcel: function (id) {
copy$("#" + id).dataTable({
"bJQueryUI": false,
'bPaginate': false, //是否分页
"bRetrieve": false, //是否允许从新生成表格
"bInfo": false, //显示表格的相关信息
"bDestroy": true,
"bServerSide": false,
"bProcessing": true, //当处理大量数据时,显示进度,进度条等
"bFilter": false, //搜索框
"bLengthChange": false, //动态指定分页后每页显示的记录数
"bSort": false, //排序
"bStateSave": false, //缓存
"sAjaxDataProp": "data",
"sDom": 'T<"clear">lfrtip'
"oTableTools": {
"sSwfPath": "DataTables-1.9.4/extras/TableTools/media/swf/copy_csv_xls_pdf.swf"
}
} );
}

相关阅读

关键词不能为空
极力推荐

ppt怎么做_excel表格制作_office365_word文档_365办公网